Skip to content

Commit

Permalink
feat(): #1 add isEqual, isBeforeOrEqual, isAfterOrEqual validators
Browse files Browse the repository at this point in the history
  • Loading branch information
horprogs committed Jun 15, 2022
1 parent 4d74559 commit 22706d6
Show file tree
Hide file tree
Showing 3 changed files with 298 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,13 @@ validator.addField('#date', [
},
]);
```

## Validators

- isAfter
- isBefore
- isBeforeOrEqual
- isAfterOrEqual
- isEqual


72 changes: 72 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import isMatch from 'date-fns/isMatch';
import isDate from 'date-fns/isDate';
import isAfter from 'date-fns/isAfter';
import isBefore from 'date-fns/isAfter';
import isEqual from 'date-fns/isEqual';
import isValid from 'date-fns/isValid';
import parse from 'date-fns/parse';

Expand All @@ -13,7 +14,10 @@ type PluginFieldsType = {
interface ConfigInterface {
format?: string;
isAfter?: string | Date;
isAfterOrEqual?: string | Date;
isBefore?: string | Date;
isBeforeOrEqual?: string | Date;
isEqual?: string | Date;
required?: boolean;
}

Expand Down Expand Up @@ -49,6 +53,19 @@ const getComparedDate = (
return comparedDate as Date;
};

const checkIsEqual = (
configValue: string | Date,
sourceDate?: Date | typeof NaN | null,
format?: string
) => {
const comparedDate = getComparedDate(sourceDate, configValue, format);
if (comparedDate === null) {
return false;
}

return isEqual(comparedDate, sourceDate as Date);
};

const checkIsBefore = (
configValue: string | Date,
sourceDate?: Date | typeof NaN | null,
Expand All @@ -62,6 +79,22 @@ const checkIsBefore = (
return isBefore(comparedDate, sourceDate as Date);
};

const checkIsBeforeOrEqual = (
configValue: string | Date,
sourceDate?: Date | typeof NaN | null,
format?: string
) => {
const comparedDate = getComparedDate(sourceDate, configValue, format);
if (comparedDate === null) {
return false;
}

return (
isEqual(comparedDate, sourceDate as Date) ||
isBefore(comparedDate, sourceDate as Date)
);
};

const checkIsAfter = (
configValue: string | Date,
sourceDate?: Date | typeof NaN | null,
Expand All @@ -75,6 +108,22 @@ const checkIsAfter = (
return isAfter(sourceDate as Date, comparedDate);
};

const checkIsAfterOrEqual = (
configValue: string | Date,
sourceDate?: Date | typeof NaN | null,
format?: string
) => {
const comparedDate = getComparedDate(sourceDate, configValue, format);
if (comparedDate === null) {
return false;
}

return (
isEqual(comparedDate, sourceDate as Date) ||
isAfter(sourceDate as Date, comparedDate)
);
};

const pluginDate =
(func: (fields: PluginFieldsType) => ConfigInterface) =>
(value: PluginValueType, fields: PluginFieldsType) => {
Expand All @@ -84,6 +133,9 @@ const pluginDate =
isAfter: true,
isBefore: true,
required: true,
isBeforeOrEqual: true,
isAfterOrEqual: true,
isEqual: true,
};

if (typeof value !== 'string') {
Expand Down Expand Up @@ -119,10 +171,30 @@ const pluginDate =
);
}

if (config.isBeforeOrEqual !== undefined) {
valid.isBeforeOrEqual = checkIsBeforeOrEqual(
config.isBeforeOrEqual,
sourceDate,
config.format
);
}

if (config.isAfter !== undefined) {
valid.isAfter = checkIsAfter(config.isAfter, sourceDate, config.format);
}

if (config.isAfterOrEqual !== undefined) {
valid.isAfter = checkIsAfterOrEqual(
config.isAfterOrEqual,
sourceDate,
config.format
);
}

if (config.isEqual !== undefined) {
valid.isEqual = checkIsEqual(config.isEqual, sourceDate, config.format);
}

return Object.values(valid).every((item) => item);
};

Expand Down
216 changes: 216 additions & 0 deletions src/tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ describe('plugin date', () => {
format: 'zzzxc',
}))('2021-10-05', {});
}).toThrow();

expect(
pluginDate(() => ({
format: 'dd/MM/yyyy',
isAfterOrEqual: '2021-10-06',
isBeforeOrEqual: '2021-10-07',
}))('2021-10-05', {})
).toBeFalsy();

expect(
pluginDate(() => ({
format: 'dd/MM/yyyy',
isEqual: '2021-10-06',
}))('2021-10-06', {})
).toBeFalsy();
});

test('should be able to validate required date', async () => {
Expand Down Expand Up @@ -150,6 +165,49 @@ describe('plugin date', () => {
// ).toBeFalsy();
});

test('should be able to validate isEqual', async () => {
const elem = document.createElement('input');
elem.setAttribute('type', 'text');
elem.setAttribute('id', 'date_start');
elem.setAttribute('value', '11/12/2021');

expect(
pluginDate((fields) => ({
format: 'dd/MM/yyyy',
isEqual: fields['#date_start'].elem.value,
}))('10/12/2021', {
'#date_start': {
elem,
rules: [],
},
})
).toBeFalsy();

expect(
pluginDate((fields) => ({
format: 'dd/MM/yyyy',
isEqual: fields['#date_start'].elem.value,
}))('12/12/2021', {
'#date_start': {
elem,
rules: [],
},
})
).toBeFalsy();

expect(
pluginDate((fields) => ({
format: 'dd/MM/yyyy',
isEqual: fields['#date_start'].elem.value,
}))('11/12/2021', {
'#date_start': {
elem,
rules: [],
},
})
).toBeTruthy();
});

test('should be able to validate isBefore', async () => {
const elem = document.createElement('input');
elem.setAttribute('type', 'text');
Expand Down Expand Up @@ -229,6 +287,85 @@ describe('plugin date', () => {
).toBeFalsy();
});

test('should be able to validate isBeforeOrEqual', async () => {
const elem = document.createElement('input');
elem.setAttribute('type', 'text');
elem.setAttribute('id', 'date_start');
elem.setAttribute('value', '11/12/2021');

expect(
pluginDate((fields) => ({
format: 'dd/MM/yyyy',
isBeforeOrEqual: fields['#date_start'].elem.value,
}))('10/12/2021', {
'#date_start': {
elem,
rules: [],
},
})
).toBeTruthy();

expect(
pluginDate((fields) => ({
format: 'dd/MM/yyyy',
isBeforeOrEqual: fields['#date_start'].elem.value,
}))('10/11/2021', {
'#date_start': {
elem,
rules: [],
},
})
).toBeTruthy();

expect(
pluginDate((fields) => ({
format: 'dd/MM/yyyy',
isBeforeOrEqual: fields['#date_start'].elem.value,
}))('10/12/2020', {
'#date_start': {
elem,
rules: [],
},
})
).toBeTruthy();

expect(
pluginDate((fields) => ({
format: 'dd/MM/yyyy',
isBeforeOrEqual: fields['#date_start'].elem.value,
}))('12/12/2021', {
'#date_start': {
elem,
rules: [],
},
})
).toBeFalsy();

expect(
pluginDate((fields) => ({
format: 'dd/MM/yyyy',
isBeforeOrEqual: fields['#date_start'].elem.value,
}))('01/01/2022', {
'#date_start': {
elem,
rules: [],
},
})
).toBeFalsy();

expect(
pluginDate((fields) => ({
format: 'dd/MM/yyyy',
isBeforeOrEqual: fields['#date_start'].elem.value,
}))('11/12/2021', {
'#date_start': {
elem,
rules: [],
},
})
).toBeTruthy();
});

test('should be able to validate isAfter', async () => {
const elem = document.createElement('input');
elem.setAttribute('type', 'text');
Expand Down Expand Up @@ -308,6 +445,85 @@ describe('plugin date', () => {
).toBeFalsy();
});

test('should be able to validate isAfterOrEqual', async () => {
const elem = document.createElement('input');
elem.setAttribute('type', 'text');
elem.setAttribute('id', 'date_start');
elem.setAttribute('value', '11/12/2021');

expect(
pluginDate((fields) => ({
format: 'dd/MM/yyyy',
isAfterOrEqual: fields['#date_start'].elem.value,
}))('10/12/2021', {
'#date_start': {
elem,
rules: [],
},
})
).toBeFalsy();

expect(
pluginDate((fields) => ({
format: 'dd/MM/yyyy',
isAfterOrEqual: fields['#date_start'].elem.value,
}))('10/11/2021', {
'#date_start': {
elem,
rules: [],
},
})
).toBeFalsy();

expect(
pluginDate((fields) => ({
format: 'dd/MM/yyyy',
isAfterOrEqual: fields['#date_start'].elem.value,
}))('10/12/2020', {
'#date_start': {
elem,
rules: [],
},
})
).toBeFalsy();

expect(
pluginDate((fields) => ({
format: 'dd/MM/yyyy',
isAfterOrEqual: fields['#date_start'].elem.value,
}))('12/12/2021', {
'#date_start': {
elem,
rules: [],
},
})
).toBeTruthy();

expect(
pluginDate((fields) => ({
format: 'dd/MM/yyyy',
isAfterOrEqual: fields['#date_start'].elem.value,
}))('01/01/2022', {
'#date_start': {
elem,
rules: [],
},
})
).toBeTruthy();

expect(
pluginDate((fields) => ({
format: 'dd/MM/yyyy',
isAfterOrEqual: fields['#date_start'].elem.value,
}))('11/12/2021', {
'#date_start': {
elem,
rules: [],
},
})
).toBeTruthy();
});

test('should be able to validate isBefore and isAfter with different format', async () => {
const elem = document.createElement('input');

Expand Down

0 comments on commit 22706d6

Please sign in to comment.