fix(form-core): normalize input value in Date Validators

This commit is contained in:
Víctor Lara 2020-11-11 12:18:10 +01:00 committed by GitHub
parent 3a4919cac6
commit c5c4d4ba7c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
'@lion/form-core': patch
---
Normalize input values in date comparison validators

View file

@ -40,7 +40,7 @@ export class MinDate extends Validator {
*/ */
execute(value, min = this.param) { execute(value, min = this.param) {
let hasError = false; let hasError = false;
if (!isDate(value) || value < normalizeDateTime(min)) { if (!isDate(value) || normalizeDateTime(value) < normalizeDateTime(min)) {
hasError = true; hasError = true;
} }
return hasError; return hasError;
@ -57,7 +57,7 @@ export class MaxDate extends Validator {
*/ */
execute(value, max = this.param) { execute(value, max = this.param) {
let hasError = false; let hasError = false;
if (!isDate(value) || value > normalizeDateTime(max)) { if (!isDate(value) || normalizeDateTime(value) > normalizeDateTime(max)) {
hasError = true; hasError = true;
} }
return hasError; return hasError;
@ -74,7 +74,11 @@ export class MinMaxDate extends Validator {
*/ */
execute(value, { min = 0, max = 0 } = this.param) { execute(value, { min = 0, max = 0 } = this.param) {
let hasError = false; 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; hasError = true;
} }
return hasError; return hasError;

View file

@ -36,6 +36,9 @@ describe('Date Validation', () => {
isEnabled = validator.execute(new Date('2018-02-01')); isEnabled = validator.execute(new Date('2018-02-01'));
expect(isEnabled).to.be.true; 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 today = new Date();
const todayFormatted = normalizeDateTime(today); const todayFormatted = normalizeDateTime(today);
const todayValidator = new MinDate(today); const todayValidator = new MinDate(today);
@ -51,6 +54,9 @@ describe('Date Validation', () => {
isEnabled = validator.execute(new Date('2018-02-01')); isEnabled = validator.execute(new Date('2018-02-01'));
expect(isEnabled).to.be.false; 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')); isEnabled = validator.execute(new Date('2018-02-03'));
expect(isEnabled).to.be.true; expect(isEnabled).to.be.true;
@ -78,6 +84,12 @@ describe('Date Validation', () => {
isEnabled = validator.execute(new Date('2018/02/05')); isEnabled = validator.execute(new Date('2018/02/05'));
expect(isEnabled).to.be.true; 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 today = new Date();
const todayFormatted = normalizeDateTime(today); const todayFormatted = normalizeDateTime(today);
const todayValidator = new MinMaxDate({ min: today, max: today }); const todayValidator = new MinMaxDate({ min: today, max: today });