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

isISO8601 strict mode bug: it fails if month and day are both 2 digits #932

Merged
merged 2 commits into from
Nov 13, 2018

Conversation

natesilva
Copy link
Contributor

@natesilva natesilva commented Nov 13, 2018

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.

  • In the first case (the month or day are 1 digit), Node/Chrome interprets the date as being midnight in the local timezone.
  • If both month and day are 2 digits, Node/Chrome interprets the date as being midnight in UTC.
  • Firefox 63 treats both as UTC.
  • Safari 12 rejects the first one.

The lines following that one use getFullYear, getMonth, and getDate which work using the local timezone. Therefore it works (on Node/Chrome) if either the month or day are 1 digit.

  • All of the previous regression tests used dates where either the month or day was 1 digit. Since the tests run on Node, they passed.

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, and getUTCDate in the comparison.

@chriso
Copy link
Collaborator

chriso commented Nov 13, 2018

Good catch, and thanks for the detailed write-up.

@chriso chriso merged commit bf5b2dc into validatorjs:master Nov 13, 2018
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 PR adds support for Dutch (nl-NL) locale for isMobilePhone.

closes #923 
====
* fix(isMobilePhone): add dutch phone number format

* fix: clean-up PR #932

- add tests
- update README
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.

2 participants