Skip to content
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

Merged

Conversation

ivan-tymoshenko
Copy link
Member

@ivan-tymoshenko ivan-tymoshenko commented May 30, 2022

❗ Depends on BREAKING CHANGE PR #442

Fixed #325

index.js Outdated
ajvInstance.addKeyword({
keyword: 'fjs_date_type',
validate: (schema, date) => {
if (date && typeof date.toISOString === 'function') {
Copy link
Member

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
>

Copy link
Member Author

@ivan-tymoshenko ivan-tymoshenko May 30, 2022

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.

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

} else if (date && typeof date.toISOString === 'function') {

index.js Outdated Show resolved Hide resolved
index.js Show resolved Hide resolved
index.js Show resolved Hide resolved
@ivan-tymoshenko ivan-tymoshenko force-pushed the fix-date-time-ajv-validation branch from 9f212b6 to 36e277c Compare May 31, 2022 11:15
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

oneOf with Date results to null
4 participants