/
gte.ts
46 lines (44 loc) · 1.04 KB
/
gte.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
34
35
36
37
38
39
40
41
42
43
44
45
46
import { RuleOptions, Validator, Validation, Values } from "../index.d";
import { toErrorMessage, toLabel } from "../helpers";
/**
* Validates that the specified value is greater than or equal to another.
*
* @example
* ```ts
* useForm(({ gte }) => ({
* age: gte(13)
* }));
* ```
*
* @param {number} reference The comparison value.
* @param {RulesOptions} options The validator options.
* @returns {Validator} The validation object.
*/
export function gte<Params>(
reference: number,
options?: RuleOptions,
): Validator<Params> {
return (
attribute: string,
input: number,
values: Values<Params>,
): Validation<Params> => ({
valid: input >= reference,
type: options?.type || "gte",
input,
values,
args: [reference],
message: toErrorMessage({
defaultMessage: `${toLabel(
attribute,
)} must be greater than or equal to ${reference}`,
args: [reference],
input,
values,
attribute,
options,
}),
attribute,
});
}
export const greaterThanOrEqualTo = gte;