-
Notifications
You must be signed in to change notification settings - Fork 767
/
ArrayMinSize.ts
33 lines (30 loc) · 1.09 KB
/
ArrayMinSize.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { ValidationOptions } from '../../ValidationOptions';
import { buildMessage, ValidateBy } from '../common/ValidateBy';
export const ARRAY_MIN_SIZE = 'arrayMinSize';
/**
* Checks if the array's length is greater than or equal to the specified number.
* If null or undefined is given then this function returns false.
*/
export function arrayMinSize(array: unknown, min: number): boolean {
return Array.isArray(array) && array.length >= min;
}
/**
* Checks if the array's length is greater than or equal to the specified number.
* If null or undefined is given then this function returns false.
*/
export function ArrayMinSize(min: number, validationOptions?: ValidationOptions): PropertyDecorator {
return ValidateBy(
{
name: ARRAY_MIN_SIZE,
constraints: [min],
validator: {
validate: (value, args): boolean => arrayMinSize(value, args?.constraints[0]),
defaultMessage: buildMessage(
eachPrefix => eachPrefix + '$property must contain at least $constraint1 elements',
validationOptions
),
},
},
validationOptions
);
}