isISO8601 strict mode bug: it fails if month and day are both 2 digits#932
Merged
chriso merged 2 commits intovalidatorjs:masterfrom Nov 13, 2018
Merged
isISO8601 strict mode bug: it fails if month and day are both 2 digits#932chriso merged 2 commits intovalidatorjs:masterfrom
chriso merged 2 commits intovalidatorjs:masterfrom
Conversation
Collaborator
|
Good catch, and thanks for the detailed write-up. |
profnandaa
added a commit
to profnandaa/validator.js
that referenced
this pull request
Jul 2, 2019
- add tests - update README
profnandaa
added a commit
that referenced
this pull request
Jul 2, 2019
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
isISO8601in strict mode fails for dates where the month and day are both 2 digits.isISO8601('2018-11-01', { strict: true }); // trueisISO8601('2018-11-12', { strict: true }); // false!There is a bug here: https://github.com/chriso/validator.js/blob/ef5f7a1657e4ea2a716c323b1e8f0bb0f0b79dd0/src/lib/isISO8601.js#L26
If either the month or day are a number less than 10, then it passes a string to the
Dateconstructor like this:2018-11-1(note no leading 0 for the day). If both are two digits, then it passes something like this:2018-11-12.The lines following that one use
getFullYear,getMonth, andgetDatewhich work using the local timezone. Therefore it works (on Node/Chrome) if either the month or day are 1 digit.The solution is to always build the date using 2 digit month/day values, with a leading 0 if necessary. This ensures the date will be interpreted as being in UTC.
Then, use the UTC functions
getUTCFullYear,getUTCMonth, andgetUTCDatein the comparison.