From b29fd2232e1e76c2d9d8fe70529bc32fef0655bd Mon Sep 17 00:00:00 2001 From: orcas Date: Mon, 28 Oct 2019 21:20:21 +0800 Subject: [PATCH] Resolves #114 dates are validated for short months and leap years --- src/types/date.js | 4 ++-- src/types/datetime.js | 4 ++-- test/types/date.js | 3 +++ 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/types/date.js b/src/types/date.js index f396b73..7bf2743 100644 --- a/src/types/date.js +++ b/src/types/date.js @@ -14,7 +14,7 @@ function castDate(format, value) { } try { if (format === 'default') { - value = moment(timeParse(_DEFAULT_PATTERN)(value)) + value = moment(value, _DEFAULT_PATTERN, true) } else if (format === 'any') { value = moment(value) } else { @@ -45,4 +45,4 @@ module.exports = { // Internal -const _DEFAULT_PATTERN = '%Y-%m-%d' +const _DEFAULT_PATTERN = 'YYYY-MM-DD' diff --git a/src/types/datetime.js b/src/types/datetime.js index 41c4c70..8ea9f25 100644 --- a/src/types/datetime.js +++ b/src/types/datetime.js @@ -14,7 +14,7 @@ function castDatetime(format, value) { } try { if (format === 'default') { - value = moment(timeParse(_DEFAULT_PATTERN)(value)) + value = moment(value, _DEFAULT_PATTERN, true) } else if (format === 'any') { value = moment(value) } else { @@ -45,4 +45,4 @@ module.exports = { // Internal -const _DEFAULT_PATTERN = '%Y-%m-%dT%H:%M:%SZ' +const _DEFAULT_PATTERN = 'YYYY-MM-DDTHH:mm:ss[Z]' diff --git a/test/types/date.js b/test/types/date.js index 9395bb1..3e061fc 100644 --- a/test/types/date.js +++ b/test/types/date.js @@ -32,6 +32,9 @@ const TESTS = [ ['%d/%m/%y', true, ERROR], ['%d/%m/%y', '', ERROR], ['invalid', '21/11/06 16:30', ERROR], + ['default', '1999-11-31', ERROR], + ['default', '1999-02-29', ERROR], + ['default', '2000-02-29', date(2000, 2, 29)], // Deprecated ['fmt:%d/%m/%y', date(2019, 1, 1), date(2019, 1, 1)], ['fmt:%d/%m/%y', '21/11/06', date(2006, 11, 21)],