From 497c8c43c3d8eca9147085dccc684c072598b1a1 Mon Sep 17 00:00:00 2001 From: Ciprian Stoleru Date: Fri, 26 May 2023 12:53:03 +0300 Subject: [PATCH 1/5] Fix date declaration to support different systems --- package.json | 5 +++-- src/lib/isDate.js | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 4a1034945..e9f587240 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "validator", "description": "String validation and sanitization", - "version": "13.9.0", + "version": "13.10.0", "sideEffects": false, "homepage": "https://github.com/validatorjs/validator.js", "files": [ @@ -66,7 +66,8 @@ "build:node": "babel src -d .", "build": "run-p build:*", "pretest": "npm run build && npm run lint", - "test": "nyc --reporter=cobertura --reporter=text-summary mocha --require @babel/register --reporter dot --recursive" + "test": "nyc --reporter=cobertura --reporter=text-summary mocha --require @babel/register --reporter dot --recursive", + "postinstall": "npx run-p build:*" }, "engines": { "node": ">= 0.10" diff --git a/src/lib/isDate.js b/src/lib/isDate.js index 8b7862e8b..106fa3586 100644 --- a/src/lib/isDate.js +++ b/src/lib/isDate.js @@ -47,7 +47,7 @@ export default function isDate(input, options) { dateObj[formatWord.charAt(0)] = dateWord; } - return new Date(`${dateObj.m}/${dateObj.d}/${dateObj.y}`).getDate() === +dateObj.d; + return new Date(parseInt(dateObj.y), parseInt(dateObj.m)-1, parseInt(dateObj.d)).getDate() === +dateObj.d; } if (!options.strictMode) { From ec8b039deae17a0a5206d22d4a4bd28f239c47e1 Mon Sep 17 00:00:00 2001 From: Ciprian Stoleru Date: Fri, 26 May 2023 14:09:19 +0300 Subject: [PATCH 2/5] Add postinstall --- package.json | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index e9f587240..4a1034945 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "validator", "description": "String validation and sanitization", - "version": "13.10.0", + "version": "13.9.0", "sideEffects": false, "homepage": "https://github.com/validatorjs/validator.js", "files": [ @@ -66,8 +66,7 @@ "build:node": "babel src -d .", "build": "run-p build:*", "pretest": "npm run build && npm run lint", - "test": "nyc --reporter=cobertura --reporter=text-summary mocha --require @babel/register --reporter dot --recursive", - "postinstall": "npx run-p build:*" + "test": "nyc --reporter=cobertura --reporter=text-summary mocha --require @babel/register --reporter dot --recursive" }, "engines": { "node": ">= 0.10" From 21b07a678b3a49673c5fbb2defb4b8256b74ec0a Mon Sep 17 00:00:00 2001 From: Ciprian Stoleru Date: Tue, 6 Jun 2023 15:29:52 +0300 Subject: [PATCH 3/5] Support YY format --- src/lib/isDate.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/lib/isDate.js b/src/lib/isDate.js index 106fa3586..601db960a 100644 --- a/src/lib/isDate.js +++ b/src/lib/isDate.js @@ -47,7 +47,19 @@ export default function isDate(input, options) { dateObj[formatWord.charAt(0)] = dateWord; } - return new Date(parseInt(dateObj.y), parseInt(dateObj.m)-1, parseInt(dateObj.d)).getDate() === +dateObj.d; + let fullYear = dateObj.y; + + if (dateObj.y.length === 2) { + const currentYearLastTwoDigits = new Date().getFullYear() % 100; + + if (parseInt(dateObj.y, 10) < currentYearLastTwoDigits) { + fullYear = `20${dateObj.y}`; + } else { + fullYear = `19${dateObj.y}`; + } + } + + return new Date(`${fullYear}-${dateObj.m}-${dateObj.d}`).getDate() === +dateObj.d; } if (!options.strictMode) { From af7e4039b21d4d2cb5b0468c65505b4142bbbb6a Mon Sep 17 00:00:00 2001 From: Ciprian Stoleru Date: Tue, 6 Jun 2023 16:03:55 +0300 Subject: [PATCH 4/5] Cover edge case --- src/lib/isDate.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/lib/isDate.js b/src/lib/isDate.js index 601db960a..9f1c6926b 100644 --- a/src/lib/isDate.js +++ b/src/lib/isDate.js @@ -50,9 +50,15 @@ export default function isDate(input, options) { let fullYear = dateObj.y; if (dateObj.y.length === 2) { + const parsedYear = parseInt(dateObj.y, 10); + + if (isNaN(parsedYear)) { + return false; + } + const currentYearLastTwoDigits = new Date().getFullYear() % 100; - if (parseInt(dateObj.y, 10) < currentYearLastTwoDigits) { + if (parsedYear < currentYearLastTwoDigits) { fullYear = `20${dateObj.y}`; } else { fullYear = `19${dateObj.y}`; From 81d78d50d64712d045bfc49afcddbf6d2fb9e460 Mon Sep 17 00:00:00 2001 From: Ciprian Stoleru Date: Tue, 6 Jun 2023 16:40:25 +0300 Subject: [PATCH 5/5] Add tests --- test/validators.test.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/validators.test.js b/test/validators.test.js index 937b52a26..655f370b2 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -12803,6 +12803,7 @@ describe('Validators', () => { '15/7/2002', '15-7-2002', '15/07-02', + '30/04/--', ], }); test({ @@ -12817,6 +12818,7 @@ describe('Validators', () => { '15/7/02', '15-7-02', '5/7-02', + '3/4/aa', ], }); test({