Date type is giving error - Error: schema is invalid: data/properties/updated_at/type must be equal to one of the allowed values
Dhruv-Garg79 opened this issue · 4 comments
Prerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the bug has not already been reported
Fastify version
I am using fast-json-stringify directly
Plugin version
5.14.1
Node.js version
v20.12.0
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
14.4.1
Description
{
type: 'object',
properties: {
name: { type: 'string', [Symbol(TypeBox.Kind)]: 'String' },
props_int: {
type: 'object',
patternProperties: [Object],
[Symbol(TypeBox.Kind)]: 'Record'
},
updated_at: { type: 'Date', [Symbol(TypeBox.Kind)]: 'Date' },
created_at: { type: 'Date', [Symbol(TypeBox.Kind)]: 'Date' }
},
required: [
'name',
'props_int',
'updated_at',
'created_at'
],
[Symbol(TypeBox.Kind)]: 'Object'
}
I am getting this error for js date type
Error: schema is invalid: data/properties/updated_at/type must be equal to one of the allowed values
at isValidSchema (/node_modules/fast-json-stringify/index.js:40:17)
at build (/node_modules/fast-json-stringify/index.js:83:3
Steps to Reproduce
pass typebox schema with Date type to the fast-json-stringify
Expected Behavior
This shouldn't cause errors, earlier it was working perfectly
Thanks for reporting!
Can you provide steps to reproduce? We often need a reproducible example, e.g. some code that allows someone else to recreate your problem by just copying and pasting it. If it involves more than a couple of different file, create a new repository on GitHub and add a link to that.
The problem here is that Type.Date()
is an extension type in TypeBox which is unknown to the Json Schema specification (and by consequence can't be reasonably serialized). To resolve this issue, you will need to limit your types to only those serializable as Json.
Alternative to Date
The following are recommended alternative types to use when transferring Dates.
const T = Type.Integer() // (or Number) passing numeric timestamps
const T = Type.String({ format: 'date-time' }) // passing iso8601 strings
Json and JavaScript types
You can find a list of standard [Json]
and non-standard [JavaScript]
types in the TypeBox readme.
Hope this helps
S
Understood, thanks for the help
@sinclairzx81 can I write a custom type in TypeBox, which can take js Date() as input, but converts to Type.String({ format: 'date-time' })
when passing to fast-json-stringify