fix(validate): normalizeDateTime of min/max dates
This commit is contained in:
parent
955e524ff2
commit
9945f91deb
2 changed files with 15 additions and 3 deletions
|
|
@ -1,3 +1,5 @@
|
|||
import { normalizeDateTime } from '@lion/localize';
|
||||
|
||||
export const isString = value => typeof value === 'string';
|
||||
export const isStringValidator = () => [(...params) => ({ isString: isString(...params) })];
|
||||
|
||||
|
|
@ -61,20 +63,20 @@ export const isDate = value =>
|
|||
Object.prototype.toString.call(value) === '[object Date]' && !Number.isNaN(value.getTime());
|
||||
export const isDateValidator = () => [(...params) => ({ isDate: isDate(...params) })];
|
||||
|
||||
export const minDate = (value, min) => isDate(value) && value >= min;
|
||||
export const minDate = (value, min) => isDate(value) && value >= normalizeDateTime(min);
|
||||
export const minDateValidator = (...factoryParams) => [
|
||||
(...params) => ({ minDate: minDate(...params) }),
|
||||
...factoryParams,
|
||||
];
|
||||
|
||||
export const maxDate = (value, max) => isDate(value) && value <= max;
|
||||
export const maxDate = (value, max) => isDate(value) && value <= normalizeDateTime(max);
|
||||
export const maxDateValidator = (...factoryParams) => [
|
||||
(...params) => ({ maxDate: maxDate(...params) }),
|
||||
...factoryParams,
|
||||
];
|
||||
|
||||
export const minMaxDate = (value, { min = 0, max = 0 }) =>
|
||||
isDate(value) && value >= min && value <= max;
|
||||
isDate(value) && value >= normalizeDateTime(min) && value <= normalizeDateTime(max);
|
||||
export const minMaxDateValidator = (...factoryParams) => [
|
||||
(...params) => ({ minMaxDate: minMaxDate(...params) }),
|
||||
...factoryParams,
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import { expect } from '@open-wc/testing';
|
||||
import { normalizeDateTime } from '@lion/localize';
|
||||
import { smokeTestValidator } from '../test-helpers.js';
|
||||
|
||||
import {
|
||||
|
|
@ -134,11 +135,17 @@ describe('LionValidate', () => {
|
|||
it('provides minDate() to allow only dates after min', () => {
|
||||
expect(minDate(new Date('2018-02-03'), new Date('2018/02/02'))).to.be.true;
|
||||
expect(minDate(new Date('2018-02-01'), new Date('2018/02/02'))).to.be.false;
|
||||
const today = new Date();
|
||||
const todayFormatted = normalizeDateTime(today);
|
||||
expect(minDate(todayFormatted, today)).to.be.true;
|
||||
});
|
||||
|
||||
it('provides maxDate() to allow only dates before max', () => {
|
||||
expect(maxDate(new Date('2018-02-01'), new Date('2018/02/02'))).to.be.true;
|
||||
expect(maxDate(new Date('2018-02-03'), new Date('2018/02/02'))).to.be.false;
|
||||
const today = new Date();
|
||||
const todayFormatted = normalizeDateTime(today);
|
||||
expect(maxDate(todayFormatted, today)).to.be.true;
|
||||
});
|
||||
|
||||
it('provides minMaxDate() to allow only dates between min and max', () => {
|
||||
|
|
@ -149,6 +156,9 @@ describe('LionValidate', () => {
|
|||
expect(minMaxDate(new Date('2018/02/03'), minMaxSetting)).to.be.true;
|
||||
expect(minMaxDate(new Date('2018/02/01'), minMaxSetting)).to.be.false;
|
||||
expect(minMaxDate(new Date('2018/02/05'), minMaxSetting)).to.be.false;
|
||||
const today = new Date();
|
||||
const todayFormatted = normalizeDateTime(today);
|
||||
expect(minMaxDate(todayFormatted, { min: today, max: today })).to.be.true;
|
||||
});
|
||||
|
||||
it('provides isDateDisabled() to disable dates matching specified condition', () => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue