From c5c4d4ba7c4a79e4de79d2f587a7b2e40ae7fa66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Lara?= Date: Wed, 11 Nov 2020 12:18:10 +0100 Subject: [PATCH] fix(form-core): normalize input value in Date Validators --- .changeset/famous-ducks-kneel.md | 5 +++++ .../src/validate/validators/DateValidators.js | 10 +++++++--- .../form-core/test/validate/DateValidators.test.js | 12 ++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 .changeset/famous-ducks-kneel.md diff --git a/.changeset/famous-ducks-kneel.md b/.changeset/famous-ducks-kneel.md new file mode 100644 index 000000000..0655adba7 --- /dev/null +++ b/.changeset/famous-ducks-kneel.md @@ -0,0 +1,5 @@ +--- +'@lion/form-core': patch +--- + +Normalize input values in date comparison validators diff --git a/packages/form-core/src/validate/validators/DateValidators.js b/packages/form-core/src/validate/validators/DateValidators.js index faf8c632d..1dfb35fe9 100644 --- a/packages/form-core/src/validate/validators/DateValidators.js +++ b/packages/form-core/src/validate/validators/DateValidators.js @@ -40,7 +40,7 @@ export class MinDate extends Validator { */ execute(value, min = this.param) { let hasError = false; - if (!isDate(value) || value < normalizeDateTime(min)) { + if (!isDate(value) || normalizeDateTime(value) < normalizeDateTime(min)) { hasError = true; } return hasError; @@ -57,7 +57,7 @@ export class MaxDate extends Validator { */ execute(value, max = this.param) { let hasError = false; - if (!isDate(value) || value > normalizeDateTime(max)) { + if (!isDate(value) || normalizeDateTime(value) > normalizeDateTime(max)) { hasError = true; } return hasError; @@ -74,7 +74,11 @@ export class MinMaxDate extends Validator { */ execute(value, { min = 0, max = 0 } = this.param) { let hasError = false; - if (!isDate(value) || value < normalizeDateTime(min) || value > normalizeDateTime(max)) { + if ( + !isDate(value) || + normalizeDateTime(value) < normalizeDateTime(min) || + normalizeDateTime(value) > normalizeDateTime(max) + ) { hasError = true; } return hasError; diff --git a/packages/form-core/test/validate/DateValidators.test.js b/packages/form-core/test/validate/DateValidators.test.js index 305d74a9a..3d6e90098 100644 --- a/packages/form-core/test/validate/DateValidators.test.js +++ b/packages/form-core/test/validate/DateValidators.test.js @@ -36,6 +36,9 @@ describe('Date Validation', () => { isEnabled = validator.execute(new Date('2018-02-01')); expect(isEnabled).to.be.true; + isEnabled = validator.execute(new Date('2018/02/02 10:00:00')); + expect(isEnabled).to.be.false; + const today = new Date(); const todayFormatted = normalizeDateTime(today); const todayValidator = new MinDate(today); @@ -51,6 +54,9 @@ describe('Date Validation', () => { isEnabled = validator.execute(new Date('2018-02-01')); expect(isEnabled).to.be.false; + isEnabled = validator.execute(new Date('2018/02/02 10:00:00')); + expect(isEnabled).to.be.false; + isEnabled = validator.execute(new Date('2018-02-03')); expect(isEnabled).to.be.true; @@ -78,6 +84,12 @@ describe('Date Validation', () => { isEnabled = validator.execute(new Date('2018/02/05')); expect(isEnabled).to.be.true; + isEnabled = validator.execute(new Date('2018/02/02 10:00:00')); + expect(isEnabled).to.be.false; + + isEnabled = validator.execute(new Date('2018/02/04 10:00:00')); + expect(isEnabled).to.be.false; + const today = new Date(); const todayFormatted = normalizeDateTime(today); const todayValidator = new MinMaxDate({ min: today, max: today });