Skip to content

Commit

Permalink
fix(isDate): fix thrown error on certain invalid cases (validatorjs#2443
Browse files Browse the repository at this point in the history
)
  • Loading branch information
pano9000 authored Aug 25, 2024
1 parent 96ff3b2 commit ff56dcf
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/lib/isDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function isValidFormat(format) {

function zip(date, format) {
const zippedArr = [],
len = Math.min(date.length, format.length);
len = Math.max(date.length, format.length);

for (let i = 0; i < len; i++) {
zippedArr.push([date[i], format[i]]);
Expand Down Expand Up @@ -40,7 +40,7 @@ export default function isDate(input, options) {
const dateObj = {};

for (const [dateWord, formatWord] of dateAndFormat) {
if (dateWord.length !== formatWord.length) {
if (!dateWord || !formatWord || dateWord.length !== formatWord.length) {
return false;
}

Expand Down
118 changes: 118 additions & 0 deletions test/validators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13680,6 +13680,7 @@ describe('Validators', () => {
new Date([2014, 2, 15]),
new Date('2014-03-15'),
'2020/02/29',
'2020-02-19',
],
invalid: [
'',
Expand All @@ -13695,6 +13696,18 @@ describe('Validators', () => {
'2020/03-15', // mixed delimiter
'-2020-04-19',
'-2023/05/24',
'abc-2023/05/24',
'2024',
'2024-',
'2024-05',
'2024-05-',
'2024-05-01-',
'2024-05-01-abc',
'2024/',
'2024/05',
'2024/05/',
'2024/05/01/',
'2024/05/01/abc',
],
});
test({
Expand All @@ -13710,6 +13723,21 @@ describe('Validators', () => {
'15/7/02',
'15-7-02',
'15-07/2002',
'2024',
'2024-',
'2024-05',
'2024-05-',
'2024-05-01-',
'2024-05-01-abc',
'2024/',
'2024/05',
'2024/05/',
'2024/05/01/',
'2024/05/01/abc',
'15/',
'15/07',
'15/07/',
'15/07/2024/',
],
});
test({
Expand All @@ -13725,6 +13753,21 @@ describe('Validators', () => {
'15/7/02',
'15-7-02',
'15-07/2002',
'2024',
'2024-',
'2024-05',
'2024-05-',
'2024-05-01-',
'2024-05-01-abc',
'2024/',
'2024/05',
'2024/05/',
'2024/05/01/',
'2024/05/01/abc',
'15/',
'15/07',
'15/07/',
'15/07/2024/',
],
});
test({
Expand All @@ -13739,6 +13782,22 @@ describe('Validators', () => {
'15-7-2002',
'15/07-02',
'30/04/--',
'2024',
'2024-',
'2024-05',
'2024-05-',
'2024-05-01-',
'2024-05-01-abc',
'2024/',
'2024/05',
'2024/05/',
'2024/05/01/',
'2024/05/01/abc',
'15/',
'15/07',
'15/07/',
'15/07/2024/',
'15/07/24/',
],
});
test({
Expand All @@ -13754,6 +13813,16 @@ describe('Validators', () => {
'15-7-02',
'5/7-02',
'3/4/aa',
'2024/',
'2024/05',
'2024/05/',
'2024/05/01/',
'2024/05/01/abc',
'15/',
'15/07',
'15/07/',
'15/07/2024/',
'15/07/24/',
],
});
test({
Expand All @@ -13769,6 +13838,16 @@ describe('Validators', () => {
'15/7/02',
'15-7-02',
'15-07/2002',
'2024/',
'2024/05',
'2024/05/',
'2024/05/01/',
'2024/05/01/abc',
'15/',
'15/07',
'15/07/',
'15/07/2024/',
'15/07/24/',
],
});
test({
Expand All @@ -13787,6 +13866,20 @@ describe('Validators', () => {
new Date(),
new Date([2014, 2, 15]),
new Date('2014-03-15'),
'-2020-04-19',
'-2023/05/24',
'abc-2023/05/24',
'2024',
'2024-',
'2024-05',
'2024-05-',
'2024-05-01-',
'2024-05-01-abc',
'2024/',
'2024/05',
'2024/05/',
'2024/05/01/',
'2024/05/01/abc',
],
});
test({
Expand All @@ -13812,6 +13905,21 @@ describe('Validators', () => {
'2019/02/29',
'2020/04/31',
'2020/03-15',
'-2020-04-19',
'-2023/05/24',
'abc-2023/05/24',
'2024',
'2024-',
'2024-05',
'2024-05-',
'2024-05-01-',
'2024-05-01-abc',
'2024/',
'2024/05',
'2024/05/',
'2024/05/01/',
'2024/05/01/abc',
'2024 05 01 abc',
],
});
test({
Expand All @@ -13831,6 +13939,16 @@ describe('Validators', () => {
new Date([2014, 2, 15]),
new Date('2014-03-15'),
'29.02.2020',
'2024-',
'2024-05',
'2024-05-',
'2024-05-01',
'-2020-04-19',
'-2023/05/24',
'abc-2023/05/24',
'04.05.2024.',
'04.05.2024.abc',
'abc.04.05.2024',
],
});
// emulating Pacific time zone offset & time
Expand Down

0 comments on commit ff56dcf

Please sign in to comment.