isISO8601 strict mode bug: it fails if month and day are both 2 digits #932
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.
isISO8601
in strict mode fails for dates where the month and day are both 2 digits.isISO8601('2018-11-01', { strict: true }); // true
isISO8601('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
Date
constructor 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
, andgetDate
which 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
, andgetUTCDate
in the comparison.