Skip to content

Commit 24aa0fe

Browse files
authored
feat: allow passing dynamic date to MinDate and MaxDate decorators (#1692)
1 parent 3b07014 commit 24aa0fe

File tree

4 files changed

+74
-8
lines changed

4 files changed

+74
-8
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -816,8 +816,8 @@ isBoolean(value);
816816
| `@Min(min: number)` | Checks if the given number is greater than or equal to given number. |
817817
| `@Max(max: number)` | Checks if the given number is less than or equal to given number. |
818818
| **Date validation decorators** |
819-
| `@MinDate(date: Date)` | Checks if the value is a date that's after the specified date. |
820-
| `@MaxDate(date: Date)` | Checks if the value is a date that's before the specified date. |
819+
| `@MinDate(date: Date | (() => Date))` | Checks if the value is a date that's after the specified date. |
820+
| `@MaxDate(date: Date | (() => Date))` | Checks if the value is a date that's before the specified date. |
821821
| **String-type validation decorators** | |
822822
| `@IsBooleanString()` | Checks if a string is a boolean (e.g. is "true" or "false"). |
823823
| `@IsDateString()` | Alias for `@IsISO8601()`. |

src/decorator/date/MaxDate.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ export const MAX_DATE = 'maxDate';
66
/**
77
* Checks if the value is a date that's before the specified date.
88
*/
9-
export function maxDate(date: unknown, maxDate: Date): boolean {
10-
return date instanceof Date && date.getTime() <= maxDate.getTime();
9+
export function maxDate(date: unknown, maxDate: Date | (() => Date)): boolean {
10+
return date instanceof Date && date.getTime() <= (maxDate instanceof Date ? maxDate : maxDate()).getTime();
1111
}
1212

1313
/**
1414
* Checks if the value is a date that's after the specified date.
1515
*/
16-
export function MaxDate(date: Date, validationOptions?: ValidationOptions): PropertyDecorator {
16+
export function MaxDate(date: Date | (() => Date), validationOptions?: ValidationOptions): PropertyDecorator {
1717
return ValidateBy(
1818
{
1919
name: MAX_DATE,

src/decorator/date/MinDate.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ export const MIN_DATE = 'minDate';
66
/**
77
* Checks if the value is a date that's after the specified date.
88
*/
9-
export function minDate(date: unknown, minDate: Date): boolean {
10-
return date instanceof Date && date.getTime() >= minDate.getTime();
9+
export function minDate(date: unknown, minDate: Date | (() => Date)): boolean {
10+
return date instanceof Date && date.getTime() >= (minDate instanceof Date ? minDate : minDate()).getTime();
1111
}
1212

1313
/**
1414
* Checks if the value is a date that's after the specified date.
1515
*/
16-
export function MinDate(date: Date, validationOptions?: ValidationOptions): PropertyDecorator {
16+
export function MinDate(date: Date | (() => Date), validationOptions?: ValidationOptions): PropertyDecorator {
1717
return ValidateBy(
1818
{
1919
name: MIN_DATE,

test/functional/validation-functions-and-decorators.spec.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,72 @@ describe('MaxDate', () => {
12111211
});
12121212
});
12131213

1214+
describe('MinDate function constraint', () => {
1215+
const constraint = () => new Date(1995, 11, 17);
1216+
const validValues = [new Date()];
1217+
const invalidValues = [new Date(1994, 11, 17)];
1218+
1219+
class MyClass {
1220+
@MinDate(constraint)
1221+
someProperty: Date;
1222+
}
1223+
1224+
it('should not fail if validator.validate said that its valid', () => {
1225+
return checkValidValues(new MyClass(), validValues);
1226+
});
1227+
1228+
it('should fail if validator.validate said that its invalid', () => {
1229+
return checkInvalidValues(new MyClass(), invalidValues);
1230+
});
1231+
1232+
it('should not fail if method in validator said that its valid', () => {
1233+
validValues.forEach(value => expect(minDate(value, constraint)).toBeTruthy());
1234+
});
1235+
1236+
it('should fail if method in validator said that its invalid', () => {
1237+
invalidValues.forEach(value => expect(minDate(value, constraint)).toBeFalsy());
1238+
});
1239+
1240+
it('should return error object with proper data', () => {
1241+
const validationType = 'minDate';
1242+
const message = 'minimal allowed date for someProperty is ' + constraintToString(constraint);
1243+
return checkReturnedError(new MyClass(), invalidValues, validationType, message);
1244+
});
1245+
});
1246+
1247+
describe('MaxDate function constraint', () => {
1248+
const constraint = () => new Date(1995, 11, 17);
1249+
const validValues = [new Date(1994, 11, 17)];
1250+
const invalidValues = [new Date()];
1251+
1252+
class MyClass {
1253+
@MaxDate(constraint)
1254+
someProperty: Date;
1255+
}
1256+
1257+
it('should not fail if validator.validate said that its valid', () => {
1258+
return checkValidValues(new MyClass(), validValues);
1259+
});
1260+
1261+
it('should fail if validator.validate said that its invalid', () => {
1262+
return checkInvalidValues(new MyClass(), invalidValues);
1263+
});
1264+
1265+
it('should not fail if method in validator said that its valid', () => {
1266+
validValues.forEach(value => expect(maxDate(value, constraint)).toBeTruthy());
1267+
});
1268+
1269+
it('should fail if method in validator said that its invalid', () => {
1270+
invalidValues.forEach(value => expect(maxDate(value, constraint)).toBeFalsy());
1271+
});
1272+
1273+
it('should return error object with proper data', () => {
1274+
const validationType = 'maxDate';
1275+
const message = 'maximal allowed date for someProperty is ' + constraintToString(constraint);
1276+
return checkReturnedError(new MyClass(), invalidValues, validationType, message);
1277+
});
1278+
});
1279+
12141280
describe('IsBooleanString', () => {
12151281
const validValues = ['1', '0', 'true', 'false'];
12161282
const invalidValues = ['2', '3', 'falze'];

0 commit comments

Comments
 (0)