-
-
Notifications
You must be signed in to change notification settings - Fork 201
/
minValue.ts
129 lines (124 loc) · 2.83 KB
/
minValue.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import type {
BaseIssue,
BaseValidation,
ErrorMessage,
} from '../../types/index.ts';
import { _addIssue, _stringify } from '../../utils/index.ts';
import type { ValueInput } from '../types.ts';
/**
* Min value issue type.
*/
export interface MinValueIssue<
TInput extends ValueInput,
TRequirement extends ValueInput,
> extends BaseIssue<TInput> {
/**
* The issue kind.
*/
readonly kind: 'validation';
/**
* The issue type.
*/
readonly type: 'min_value';
/**
* The expected property.
*/
readonly expected: `>=${string}`;
/**
* The minimum value.
*/
readonly requirement: TRequirement;
}
/**
* Min value action type.
*/
export interface MinValueAction<
TInput extends ValueInput,
TRequirement extends TInput,
TMessage extends
| ErrorMessage<MinValueIssue<TInput, TRequirement>>
| undefined,
> extends BaseValidation<TInput, TInput, MinValueIssue<TInput, TRequirement>> {
/**
* The action type.
*/
readonly type: 'min_value';
/**
* The action reference.
*/
readonly reference: typeof minValue;
/**
* The expected property.
*/
readonly expects: `>=${string}`;
/**
* The minimum value.
*/
readonly requirement: TRequirement;
/**
* The error message.
*/
readonly message: TMessage;
}
/**
* Creates a min value validation action.
*
* @param requirement The minimum value.
*
* @returns A min value action.
*/
export function minValue<
TInput extends ValueInput,
const TRequirement extends TInput,
>(requirement: TRequirement): MinValueAction<TInput, TRequirement, undefined>;
/**
* Creates a min value validation action.
*
* @param requirement The minimum value.
* @param message The error message.
*
* @returns A min value action.
*/
export function minValue<
TInput extends ValueInput,
const TRequirement extends TInput,
const TMessage extends
| ErrorMessage<MinValueIssue<TInput, TRequirement>>
| undefined,
>(
requirement: TRequirement,
message: TMessage
): MinValueAction<TInput, TRequirement, TMessage>;
export function minValue(
requirement: ValueInput,
message?: ErrorMessage<MinValueIssue<ValueInput, ValueInput>>
): MinValueAction<
ValueInput,
ValueInput,
ErrorMessage<MinValueIssue<ValueInput, ValueInput>> | undefined
> {
return {
kind: 'validation',
type: 'min_value',
reference: minValue,
async: false,
expects: `>=${
requirement instanceof Date
? requirement.toJSON()
: _stringify(requirement)
}`,
requirement,
message,
'~run'(dataset, config) {
if (dataset.typed && !(dataset.value >= this.requirement)) {
_addIssue(this, 'value', dataset, config, {
received:
dataset.value instanceof Date
? dataset.value.toJSON()
: _stringify(dataset.value),
});
}
return dataset;
},
};
}