Skip to content

Commit 9945f91

Browse files
fix(validate): normalizeDateTime of min/max dates
1 parent 955e524 commit 9945f91

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

packages/validate/src/validators.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { normalizeDateTime } from '@lion/localize';
2+
13
export const isString = value => typeof value === 'string';
24
export const isStringValidator = () => [(...params) => ({ isString: isString(...params) })];
35

@@ -61,20 +63,20 @@ export const isDate = value =>
6163
Object.prototype.toString.call(value) === '[object Date]' && !Number.isNaN(value.getTime());
6264
export const isDateValidator = () => [(...params) => ({ isDate: isDate(...params) })];
6365

64-
export const minDate = (value, min) => isDate(value) && value >= min;
66+
export const minDate = (value, min) => isDate(value) && value >= normalizeDateTime(min);
6567
export const minDateValidator = (...factoryParams) => [
6668
(...params) => ({ minDate: minDate(...params) }),
6769
...factoryParams,
6870
];
6971

70-
export const maxDate = (value, max) => isDate(value) && value <= max;
72+
export const maxDate = (value, max) => isDate(value) && value <= normalizeDateTime(max);
7173
export const maxDateValidator = (...factoryParams) => [
7274
(...params) => ({ maxDate: maxDate(...params) }),
7375
...factoryParams,
7476
];
7577

7678
export const minMaxDate = (value, { min = 0, max = 0 }) =>
77-
isDate(value) && value >= min && value <= max;
79+
isDate(value) && value >= normalizeDateTime(min) && value <= normalizeDateTime(max);
7880
export const minMaxDateValidator = (...factoryParams) => [
7981
(...params) => ({ minMaxDate: minMaxDate(...params) }),
8082
...factoryParams,

packages/validate/test/validators.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { expect } from '@open-wc/testing';
2+
import { normalizeDateTime } from '@lion/localize';
23
import { smokeTestValidator } from '../test-helpers.js';
34

45
import {
@@ -134,11 +135,17 @@ describe('LionValidate', () => {
134135
it('provides minDate() to allow only dates after min', () => {
135136
expect(minDate(new Date('2018-02-03'), new Date('2018/02/02'))).to.be.true;
136137
expect(minDate(new Date('2018-02-01'), new Date('2018/02/02'))).to.be.false;
138+
const today = new Date();
139+
const todayFormatted = normalizeDateTime(today);
140+
expect(minDate(todayFormatted, today)).to.be.true;
137141
});
138142

139143
it('provides maxDate() to allow only dates before max', () => {
140144
expect(maxDate(new Date('2018-02-01'), new Date('2018/02/02'))).to.be.true;
141145
expect(maxDate(new Date('2018-02-03'), new Date('2018/02/02'))).to.be.false;
146+
const today = new Date();
147+
const todayFormatted = normalizeDateTime(today);
148+
expect(maxDate(todayFormatted, today)).to.be.true;
142149
});
143150

144151
it('provides minMaxDate() to allow only dates between min and max', () => {
@@ -149,6 +156,9 @@ describe('LionValidate', () => {
149156
expect(minMaxDate(new Date('2018/02/03'), minMaxSetting)).to.be.true;
150157
expect(minMaxDate(new Date('2018/02/01'), minMaxSetting)).to.be.false;
151158
expect(minMaxDate(new Date('2018/02/05'), minMaxSetting)).to.be.false;
159+
const today = new Date();
160+
const todayFormatted = normalizeDateTime(today);
161+
expect(minMaxDate(todayFormatted, { min: today, max: today })).to.be.true;
152162
});
153163

154164
it('provides isDateDisabled() to disable dates matching specified condition', () => {

0 commit comments

Comments
 (0)