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

Leap year and general date validation for isDate(). #431

Merged
merged 6 commits into from
Oct 5, 2015
Merged
Show file tree
Hide file tree
Changes from 3 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
64 changes: 63 additions & 1 deletion test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -854,17 +854,79 @@ describe('Validators', function () {
test({
validator: 'isDate'
, valid: [
//NOTE: null passes as well in the regular function, but fails in the test suite
'2011-08-04'
, '2011-09-30'
, '04. 08. 2011.'
, '08/04/2011'
, '08/31/2011'
, '2011.08.04'
, '2/29/24'
, '2-29-24'
, '4. 8. 2011. GMT'
, '2. 28. 2011. GMT'
, '2. 29. 2008. GMT'
, '2011-08-04 12:00'
]
, '2/22/23'
, '2-23-22'
, '12'
, '11/2/23 12:24'
, new Date()
, 'Mon Aug 17 2015 00:24:56 GMT-0500 (CDT)'
, '2/22/23 23:24:26'
//valid ISO 8601 dates below
, '2009-12T12:34'
, '2009'
, '2009-05-19'
, '2009-05-19'
, '2009-05'
, '2009-001'
, '2009-05-19'
, '2009-05-19 00:00'
, '2009-05-19 14:31'
, '2009-05-19 14:39:22'
, '2009-05-19T14:39Z'
, '2009-05-19 14:39:22-06:00'
, '2009-05-19 14:39:22+0600'
, '2009-05-19 14:39:22-01'
, '2007-04-06T00:00'
, '2010-02-18T16:23:48.5'
, '200905'
, '2009-'
, '2009-05-19 14:'
, '200912-01'
]
, invalid: [
'foo'
, '2011-foo-04'
, '2011-09-31'
, '2. 29. 1987. GMT'
, '2. 29. 2011. GMT'
, '2/29/25'
, '2-29-25'
, 'GMT'
//invalid ISO 8601 dates below
, '2009367'
, '2007-04-05T24:50'
, '2009-000'
, '2009-M511'
, '2009M511'
, '2009-05-19T14a39r'
, '2009-05-19T14:3924'
, '2009-0519'
, '2009-05-1914:39'
, '2009-05-19r14:39'
, '2009-05-19 14a39a22'
, '2009-05-19 14:39:22+06a00'
, '2009-05-19 146922.500'
, '2010-02-18T16.5:23.35:48'
, '2010-02-18T16:23.35:48'
, '2010-02-18T16:23.35:48.45'
, '2009-05-19 14.5.44'
, '2010-02-18T16:23.33.600'
, '2010-02-18T16,25:23:48,444'
, '2009-02-30 14:'
, '200912-32'
]
});
});
Expand Down
32 changes: 31 additions & 1 deletion validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,37 @@
};

validator.isDate = function (str) {
return !isNaN(Date.parse(str));
var normalizedDate = new Date((new Date(str)).toUTCString());
var regularDay = String(normalizedDate.getDate());
var utcDay = String(normalizedDate.getUTCDate());
var dayOrYear, dayOrYearMatches, year;
if (isNaN(Date.parse(normalizedDate))) {
return false;
}
if (typeof str !== 'string') {
return true; //if Date.parse() passes, it's the current time
Copy link
Collaborator

Choose a reason for hiding this comment

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

This isn't necessary. The first argument is always coerced to a string (1, 2, 3) based on these rules: https://github.com/chriso/validator.js#strings-only

}
//check for valid double digits that could be late days
//check for all matches since a string like '12/23' is a valid date
//ignore everything with nearby colons
dayOrYearMatches = str.match(/(^|[^:])[23]\d([^:\d]|$)/g);
if (!dayOrYearMatches) {
return true;
}
dayOrYear = dayOrYearMatches.map(function(digitString) {
return digitString.match(/\d+/g)[0];
}).join('/');
year = String(normalizedDate.getFullYear()).slice(-2);
//local date and UTC date can differ, but both are valid, so check agains both
if (dayOrYear === regularDay || dayOrYear === utcDay || dayOrYear === year) {
return true;
} else if ((dayOrYear === (regularDay + '/' + year)) || (dayOrYear === (year + '/' + regularDay))) {
return true;
} else if ((dayOrYear === (utcDay + '/' + year)) || (dayOrYear === (year + '/' + utcDay))) {
return true;
} else {
return false;
}
};

validator.isAfter = function (str, date) {
Expand Down