From b60c882c4b863b9cf5cc2f4eb1d3f45718b7326b Mon Sep 17 00:00:00 2001 From: Jorge Date: Wed, 20 Dec 2017 09:24:15 +0000 Subject: [PATCH] Set moment in strict mode This will cause moment to return undefined when parsing a date that doesn't match the format For: #584 --- src/addons/MomentLocaleUtils.js | 2 +- test/addons/MomentLocaleUtils.js | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/addons/MomentLocaleUtils.js b/src/addons/MomentLocaleUtils.js index 33af27fd0f..e50caabd7f 100644 --- a/src/addons/MomentLocaleUtils.js +++ b/src/addons/MomentLocaleUtils.js @@ -53,7 +53,7 @@ export function formatDate(date, format = 'L', locale = 'en') { } export function parseDate(str, format = 'L', locale = 'en') { - const m = moment(str, format, locale); + const m = moment(str, format, locale, true); if (m.isValid()) { return m.toDate(); } diff --git a/test/addons/MomentLocaleUtils.js b/test/addons/MomentLocaleUtils.js index 5850d05bb2..0d645f6420 100644 --- a/test/addons/MomentLocaleUtils.js +++ b/test/addons/MomentLocaleUtils.js @@ -101,5 +101,9 @@ describe('MomentLocaleUtils', () => { const parsed = MomentLocaleUtils.parseDate('20 foo 2018', 'LL', 'it'); expect(parsed).toBeUndefined(); }); + it('returns undefined if date does not match the format', () => { + const parsed = MomentLocaleUtils.parseDate('20/11/201', 'DD/MM/YYYY'); + expect(parsed).toBeUndefined(); + }); }); });