Skip to content

Commit 2b37f1d

Browse files
123NeNaDmcollina
authored andcommitted
fix: update fjs_type validation to check for toJSON method
1 parent 8d35e26 commit 2b37f1d

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

lib/validator.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class Validator {
2121
keyword: 'fjs_type',
2222
type: 'object',
2323
errors: false,
24-
validate: (_type, date) => {
25-
return date instanceof Date
24+
validate: (_type, data) => {
25+
return data && typeof data.toJSON === 'function'
2626
}
2727
})
2828

@@ -51,8 +51,10 @@ class Validator {
5151
return this.ajv.validate(schemaRef, data)
5252
}
5353

54-
// Ajv does not support js date format. In order to properly validate objects containing a date,
55-
// it needs to replace all occurrences of the string date format with a custom keyword fjs_type.
54+
// Ajv does not natively support JavaScript objects like Date or other types
55+
// that rely on a custom .toJSON() representation. To properly validate schemas
56+
// that may contain such objects (e.g. Date, ObjectId, etc.), we replace all
57+
// occurrences of the string type with a custom keyword fjs_type
5658
// (see https://github.com/fastify/fast-json-stringify/pull/441)
5759
convertSchemaToAjvFormat (schema) {
5860
if (schema === null) return

0 commit comments

Comments
 (0)