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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/isISO8601.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ var isValidDate = function isValidDate(str) {
var match = str.match(/(\d{4})-?(\d{0,2})-?(\d*)/).map(Number);
var year = match[1];
var month = match[2];
var day = match[3]; // create a date object and compare
var day = match[3];
var monthString = month ? "0".concat(month).slice(-2) : month;
var dayString = day ? "0".concat(day).slice(-2) : day; // create a date object and compare

var d = new Date("".concat(year, "-").concat(month || 1, "-").concat(day || 1));
if (isNaN(d.getFullYear())) return false;
var d = new Date("".concat(year, "-").concat(monthString || '01', "-").concat(dayString || '01'));
if (isNaN(d.getUTCFullYear())) return false;

if (month && day) {
return d.getFullYear() === year && d.getMonth() + 1 === month && d.getDate() === day;
return d.getUTCFullYear() === year && d.getUTCMonth() + 1 === month && d.getUTCDate() === day;
}

return true;
Expand Down
13 changes: 8 additions & 5 deletions src/lib/isISO8601.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ const isValidDate = (str) => {
const year = match[1];
const month = match[2];
const day = match[3];
const monthString = month ? `0${month}`.slice(-2) : month;
const dayString = day ? `0${day}`.slice(-2) : day;

// create a date object and compare
const d = new Date(`${year}-${month || 1}-${day || 1}`);
if (isNaN(d.getFullYear())) return false;
const d = new Date(`${year}-${monthString || '01'}-${dayString || '01'}`);
if (isNaN(d.getUTCFullYear())) return false;
if (month && day) {
return d.getFullYear() === year
&& (d.getMonth() + 1) === month
&& d.getDate() === day;
return d.getUTCFullYear() === year
&& (d.getUTCMonth() + 1) === month
&& d.getUTCDate() === day;
}
return true;
};
Expand Down
1 change: 1 addition & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -5636,6 +5636,7 @@ describe('Validators', () => {
'2010-02-18T16,2283',
'2009-05-19 143922.500',
'2009-05-19 1439,55',
'2009-10-10',
];

const invalidISO8601 = [
Expand Down
10 changes: 6 additions & 4 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1575,13 +1575,15 @@ var isValidDate = function isValidDate(str) {
var match = str.match(/(\d{4})-?(\d{0,2})-?(\d*)/).map(Number);
var year = match[1];
var month = match[2];
var day = match[3]; // create a date object and compare
var day = match[3];
var monthString = month ? "0".concat(month).slice(-2) : month;
var dayString = day ? "0".concat(day).slice(-2) : day; // create a date object and compare

var d = new Date("".concat(year, "-").concat(month || 1, "-").concat(day || 1));
if (isNaN(d.getFullYear())) return false;
var d = new Date("".concat(year, "-").concat(monthString || '01', "-").concat(dayString || '01'));
if (isNaN(d.getUTCFullYear())) return false;

if (month && day) {
return d.getFullYear() === year && d.getMonth() + 1 === month && d.getDate() === day;
return d.getUTCFullYear() === year && d.getUTCMonth() + 1 === month && d.getUTCDate() === day;
}

return true;
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.