Skip to content

Commit

Permalink
fix(isDate):breaks on 2 digit strings (validatorjs#2272)
Browse files Browse the repository at this point in the history
  • Loading branch information
olaleykhan committed Aug 25, 2023
1 parent b958bd7 commit 5596b8f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/lib/isDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default function isDate(input, options) {

let fullYear = dateObj.y;

if (dateObj.y.length === 2) {
if (dateObj.y?.length === 2) {
const parsedYear = parseInt(dateObj.y, 10);

if (isNaN(parsedYear)) {
Expand All @@ -67,13 +67,13 @@ export default function isDate(input, options) {

let month = dateObj.m;

if (dateObj.m.length === 1) {
if (dateObj.m?.length === 1) {
month = `0${dateObj.m}`;
}

let day = dateObj.d;

if (dateObj.d.length === 1) {
if (dateObj.d?.length === 1) {
day = `0${dateObj.d}`;
}

Expand Down
20 changes: 20 additions & 0 deletions test/validators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13095,6 +13095,26 @@ describe('Validators', () => {
'29.02.2020',
],
});
test({
validator: 'isDate',
args: [{ format: 'DD/MM/YY' }],
valid: [
'15-07-02',
'15/07/02',
'12/11/28',
],
invalid: [
'7',
'4.5',
'78',
'17',
'20',
'15/7/2002',
'15-7-2002',
'15/07-02',
'30/04/--',
],
});
// emulating Pacific time zone offset & time
// which could potentially result in UTC conversion issues
timezone_mock.register('US/Pacific');
Expand Down

0 comments on commit 5596b8f

Please sign in to comment.