-
-
Notifications
You must be signed in to change notification settings - Fork 370
Expand file tree
/
Copy pathnotValue.ts
More file actions
136 lines (131 loc) 路 2.96 KB
/
Copy pathnotValue.ts
File metadata and controls
136 lines (131 loc) 路 2.96 KB
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
130
131
132
133
134
135
136
import type {
BaseIssue,
BaseValidation,
ErrorMessage,
} from '../../types/index.ts';
import {
_addIssue,
_isValueRequirementMatch,
_stringify,
} from '../../utils/index.ts';
import type { ValueInput } from '../types.ts';
/**
* Not value issue interface.
*/
export interface NotValueIssue<
TInput extends ValueInput,
TRequirement extends TInput,
> extends BaseIssue<TInput> {
/**
* The issue kind.
*/
readonly kind: 'validation';
/**
* The issue type.
*/
readonly type: 'not_value';
/**
* The expected property.
*/
readonly expected: `!${string}`;
/**
* The not required value.
*/
readonly requirement: TRequirement;
}
/**
* Not value action interface.
*/
export interface NotValueAction<
TInput extends ValueInput,
TRequirement extends TInput,
TMessage extends
| ErrorMessage<NotValueIssue<TInput, TRequirement>>
| undefined,
> extends BaseValidation<TInput, TInput, NotValueIssue<TInput, TRequirement>> {
/**
* The action type.
*/
readonly type: 'not_value';
/**
* The action reference.
*/
readonly reference: typeof notValue;
/**
* The expected property.
*/
readonly expects: `!${string}`;
/**
* The not required value.
*/
readonly requirement: TRequirement;
/**
* The error message.
*/
readonly message: TMessage;
}
/**
* Creates a not value validation action.
*
* @param requirement The not required value.
*
* @returns A not value action.
*/
export function notValue<
TInput extends ValueInput,
const TRequirement extends TInput,
>(requirement: TRequirement): NotValueAction<TInput, TRequirement, undefined>;
/**
* Creates a not value validation action.
*
* @param requirement The not required value.
* @param message The error message.
*
* @returns A not value action.
*/
export function notValue<
TInput extends ValueInput,
const TRequirement extends TInput,
const TMessage extends
| ErrorMessage<NotValueIssue<TInput, TRequirement>>
| undefined,
>(
requirement: TRequirement,
message: TMessage
): NotValueAction<TInput, TRequirement, TMessage>;
// @__NO_SIDE_EFFECTS__
export function notValue(
requirement: ValueInput,
message?: ErrorMessage<NotValueIssue<ValueInput, ValueInput>>
): NotValueAction<
ValueInput,
ValueInput,
ErrorMessage<NotValueIssue<ValueInput, ValueInput>> | undefined
> {
return {
kind: 'validation',
type: 'not_value',
reference: notValue,
async: false,
expects:
requirement instanceof Date
? `!${requirement.toJSON()}`
: `!${_stringify(requirement)}`,
requirement,
message,
'~run'(dataset, config) {
if (
dataset.typed &&
_isValueRequirementMatch(this.requirement, dataset.value)
) {
_addIssue(this, 'value', dataset, config, {
received:
dataset.value instanceof Date
? dataset.value.toJSON()
: _stringify(dataset.value),
});
}
return dataset;
},
};
}