-
-
Notifications
You must be signed in to change notification settings - Fork 208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: date/time ajv validation #441
fix: date/time ajv validation #441
Conversation
index.js
Outdated
ajvInstance.addKeyword({ | ||
keyword: 'fjs_date_type', | ||
validate: (schema, date) => { | ||
if (date && typeof date.toISOString === 'function') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check is not sufficient:
❯ node
Welcome to Node.js v16.15.0.
Type ".help" for more information.
> var d = new Date({})
undefined
> typeof d.toISOString === 'function'
true
>
A proper way to validate if something is a Date
looks like:
❯ node
Welcome to Node.js v16.15.0.
Type ".help" for more information.
> var d = new Date({})
undefined
> typeof d.toISOString === 'function'
true
> isNaN(d.valueOf()) === false
false
>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO if someone passes the wrong date object, it should still be validated as a date object. It will fail during serialization. But we can discuss it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I understand, fjs not specifies that date should be the Date type object. It says that it should be an object with toISOString or format method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it says it should be a string or a Date
object, where in the case of the Date
object it will be serialized through the .toISOString
method -- https://github.com/fastify/fast-json-stringify#specific-use-cases
Simply because an object has a .toISOString
method does not mean it is a valid Date
object. I showed this above. So if there is going to be code to verify that an object is a valid Date
object instance, it should be more complete.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line 130 in 074a120
} else if (date && typeof date.toISOString === 'function') { |
3383f4a
to
9f212b6
Compare
9f212b6
to
36e277c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
❗ Depends on BREAKING CHANGE PR #442
Fixed #325