-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidation.ts
293 lines (277 loc) · 6.76 KB
/
validation.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
export type ValidationIssueCode =
| "generic"
| "required"
| "wrong_type"
| "invalid_key"
// async
| "async_validation_required"
// array
| "includes"
| "items"
| "maxItems"
| "minItems"
| "uniqueItems"
// string
| "minLength"
| "maxLength"
| "length"
| "pattern"
| "startsWith"
| "endsWith"
// number
| "isNaN"
| "integer"
| "positive"
| "nonPositive"
| "negative"
| "nonNegative"
| "multipleOf"
| "exclusiveMaximum"
| "exclusiveMinimum"
| "maximum"
| "minimum"
// object
| "additionalProperty"
// fun
| "invalid_arguments"
| "invalid_return"
| "arity"
// promise
| "invalid_promise"
// enum
| "enum"
// date
| "invalid_date"
| "before"
| "after"
// literal/s
| "literal"
// logic
| "xor"
| "not"
| "unknownDiscrimate";
export class ValidationIssue extends Error {
public readonly args?: unknown[];
constructor(
public readonly code: ValidationIssueCode,
message: string | undefined,
public readonly value: unknown,
...args: unknown[]
) {
super(message);
if (args?.length > 0) {
this.args = args;
}
}
}
export function isValidationIssue(v: unknown): v is ValidationIssue {
return v instanceof ValidationIssue;
}
export type Validation<T, E = ValidationIssue> = T extends Set<infer U>
? Set<Validation<U, E>> | E
: T extends Map<infer K, infer U>
? Map<K, Validation<U, E>> | E
: T extends Record<string, unknown>
? { [P in keyof T]?: Validation<T[P], E> } | E
: T extends unknown[]
? (Validation<T[number], E> | undefined)[] | E
: E;
export type ValidationResult<T, E = ValidationIssue> =
| Validation<T, E>
| undefined;
export function isSuccess<T>(
validation: ValidationResult<T>
): validation is undefined {
return validation === undefined;
}
export function isFailure<T>(
validation: ValidationResult<T>
): validation is Validation<T> {
return validation !== undefined;
}
export function mergeValidations<S, T>(
left: ValidationResult<S>,
right: ValidationResult<T>
): ValidationResult<S & T> {
if (isValidationIssue(left)) {
return left as Validation<S & T>;
}
if (isValidationIssue(right)) {
return right as Validation<S & T>;
}
// Array
if (Array.isArray(left)) {
if (Array.isArray(right)) {
return [...left, ...right] as Validation<S & T>;
}
return left as Validation<S & T>;
}
if (Array.isArray(right)) {
return right as Validation<S & T>;
}
// Set
if (left instanceof Set) {
if (right instanceof Set) {
const validation = new Set();
for (const value of left.values()) {
validation.add(value);
}
for (const value of right.values()) {
validation.add(value);
}
return validation as Validation<S & T>;
}
return left as Validation<S & T>;
}
if (right instanceof Set) {
return right as Validation<S & T>;
}
// Map
if (left instanceof Map) {
if (right instanceof Map) {
const validation = new Map();
for (const key of left.keys()) {
if (right.has(key)) {
validation.set(key, mergeValidations(left.get(key), right.get(key)));
} else {
validation.set(key, left.get(key));
}
}
for (const key of right.keys()) {
if (!left.has(key)) {
validation.set(key, right.get(key));
}
}
return validation as Validation<S & T>;
}
return left as Validation<S & T>;
}
if (right instanceof Map) {
return right as Validation<S & T>;
}
// object
if (typeof left === "object") {
if (typeof right === "object") {
const validation = {};
for (const key of Object.keys(left)) {
validation[key] =
right[key] === undefined
? left[key]
: mergeValidations(left[key], right[key]);
}
for (const key of Object.keys(right)) {
if (left[key] === undefined) {
validation[key] = right[key];
}
}
return validation as Validation<S & T>;
}
return left as Validation<S & T>;
}
return right as Validation<S & T>;
}
export function simplifyValidation<T>(
v: ValidationResult<T>
): ValidationResult<T> {
if (isValidationIssue(v)) {
return v;
}
if (Array.isArray(v)) {
if (v.every((inner) => inner === undefined)) {
return undefined;
}
return v.map((inner) => simplifyValidation(inner)) as ValidationResult<T>;
}
if (typeof v === "object") {
for (const key of Object.keys(v)) {
const k = key as keyof typeof v;
v[k] = simplifyValidation(
v[k] as ValidationResult<unknown>
) as Validation<T, ValidationIssue>[keyof Validation<T, ValidationIssue>];
if (v[k] === undefined) {
delete v[k];
}
}
if (Object.keys(v).length === 0) {
return undefined;
}
return v;
}
}
export function defaultTranslateError({
message,
code,
value,
args,
}: ValidationIssue): string {
if (message) {
return message;
}
const innerError = () =>
args && args.length > 0
? defaultTranslateError(args[0] as ValidationIssue)
: "";
switch (code) {
case "required": {
return "value is required";
}
case "wrong_type": {
return `value was of type ${
Array.isArray(value) ? "array" : typeof value
} expected ${(args ?? []).join(" or ")}`;
}
case "invalid_arguments":
case "invalid_return":
case "invalid_key": {
return `${code}: ${innerError()}`;
}
default: {
if (args && args.length > 0) {
return `${code}(${args.join(",")})`;
}
return code;
}
}
}
export function translate<T>(
validation: ValidationResult<T, ValidationIssue>,
translateError: (
validation: ValidationIssue
) => string = defaultTranslateError
): ValidationResult<T, string> {
if (isSuccess(validation)) {
return;
}
if (isValidationIssue(validation)) {
return translateError(validation) as Validation<T, string>;
}
if (Array.isArray(validation)) {
return validation.map((v) => translate(v)) as Validation<T, string>;
}
if (validation instanceof Set) {
const result = new Set();
for (const value of validation) {
result.add(
translate(value as ValidationResult<unknown, ValidationIssue>)
);
}
return result as Validation<T, string>;
}
if (validation instanceof Map) {
const result = new Map();
for (const [key, value] of validation.entries()) {
result.set(
key,
translate(value as ValidationResult<unknown, ValidationIssue>)
);
}
return result as Validation<T, string>;
}
const result = {};
for (const [key, value] of Object.entries(validation)) {
result[key] = translate(
value as ValidationResult<unknown, ValidationIssue>
);
}
return result as Validation<T, string>;
}