Skip to content
Open
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
13 changes: 12 additions & 1 deletion src/lib/isRFC3339.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,18 @@ const fullTime = new RegExp(`${partialTime.source}${timeOffset.source}`);

const rfc3339 = new RegExp(`^${fullDate.source}[ tT]${fullTime.source}$`);

function daysInMonth(year, month) {
if (month === 2) {
return ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0) ? 29 : 28;
}
return [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month - 1];
}

export default function isRFC3339(str) {
assertString(str);
return rfc3339.test(str);
if (!rfc3339.test(str)) {
return false;
}
const [, year, month, day] = str.match(/^(\d{4})-(\d{2})-(\d{2})/).map(Number);
return day <= daysInMonth(year, month);
}
7 changes: 7 additions & 0 deletions test/validators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12603,9 +12603,16 @@ describe('Validators', () => {
'2010-02-18t00:23:23.33+06:00',
'2010-02-18t00:23:32.33+00:00',
'2010-02-18t00:23:32.33+23:00',
'2020-02-29T14:39:22Z',
'2000-02-29T00:00:00Z',
],
invalid: [
'2010-02-18t00:23:32.33+24:00',
'2021-02-30T00:00:00Z',
'2021-04-31T00:00:00Z',
'2021-06-31T12:00:00Z',
'2021-02-29T00:00:00Z',
'1900-02-29T00:00:00Z',
'2009-05-31 14:60:55Z',
'2010-02-18t24:23.33+0600',
'2009-05-00 1439,55Z',
Expand Down
Loading