fix(form-core): normalize input value in Date Validators
This commit is contained in:
parent
3a4919cac6
commit
c5c4d4ba7c
3 changed files with 24 additions and 3 deletions
5
.changeset/famous-ducks-kneel.md
Normal file
5
.changeset/famous-ducks-kneel.md
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'@lion/form-core': patch
|
||||
---
|
||||
|
||||
Normalize input values in date comparison validators
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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 });
|
||||
|
|
|
|||
Loading…
Reference in a new issue