-
Notifications
You must be signed in to change notification settings - Fork 2
/
validator-2.ts
465 lines (377 loc) · 11.4 KB
/
validator-2.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// An item validator with static typing support.
// #region result
export type Ok<T> = {
readonly ok: true
readonly errors?: undefined
readonly value: T
}
export type Error = {
readonly ok: false
readonly errors: readonly string[]
readonly value?: undefined
}
export type Result<T> = Ok<T> | Error
export function ok<T>(value: T): Result<T> {
return { ok: true, value }
}
export function error(reason: string, ...other: string[]): Error
export function error(...errors: readonly [string, ...string[]]): Error {
return { ok: false, errors }
}
// #endregion
// #region AnyValidator, Infer, Modifier, and joinErrors
export type AnyValidator = Validator<any>
export type Infer<T extends AnyValidator> = T extends Validator<infer U>
? U
: never
export type Modifier<I, O> = (value: I) => Result<O>
function joinErrors(first: string, rest: readonly string[]): readonly string[] {
if (rest.length == 0) {
return [first]
}
return [first + rest[0]]!.concat(rest.slice(1))
}
// #endregion
class Validator<T> {
protected readonly core: readonly Modifier<unknown, unknown>[] = []
constructor(protected readonly modifiers: readonly Modifier<any, any>[]) {}
protected duplicate(newModifiers: readonly Modifier<any, any>[]): this {
return new (this.constructor as typeof Validator)(newModifiers) as this
}
as<T extends new (modifiers: readonly Modifier<any, any>[]) => Validator<T>>(
type: T,
): InstanceType<T> {
const output = new type(this.modifiers)
;(output.core as Modifier<unknown, unknown>[]).unshift(...this.core)
return output as InstanceType<T>
}
parse(data: unknown): Result<T> {
for (const modifier of this.core) {
const result = modifier(data)
if (!result.ok) {
return result
}
data = result.value
}
for (const modifier of this.modifiers) {
const result = modifier(data)
if (!result.ok) {
return result
}
data = result.value
}
return ok(data as T)
}
parseOrThrow(data: unknown): T {
const result = this.parse(data)
if (!result.ok) {
throw new AggregateError(result.errors, `Validation failed.`)
}
return result.value
}
chain<U>(modifier: Modifier<T, U>): Validator<U> {
return new Validator(this.modifiers.concat(modifier))
}
transform<U>(transformer: (data: T) => U): Validator<U> {
return this.chain((data) => ok(transformer(data)))
}
refine<U extends T>(
refiner: (data: T) => data is U,
reason?: string,
): Validator<U> & Omit<this, keyof Validator<T>>
refine(refiner: (data: T) => boolean, reason?: string): this
refine(refiner: (data: T) => boolean, reason = "Refinement failed."): this {
return this.duplicate(
this.modifiers.concat((data) =>
refiner(data) ? ok(data) : error(reason),
),
)
}
or<U>(other: Validator<U>): Validator<T | U> {
return z.chain<T | U>((data) => {
const result1 = this.parse(data)
if (result1.ok) {
return result1
}
const result2 = other.parse(data)
if (result2.ok) {
return result2
}
return error(
`'or' failed to match any of its inputs. The last validator errored with`,
...result2.errors,
)
})
}
array(): ArrayValidator<T, false> {
return array.item(this)
}
optional(): Validator<T | undefined> {
return this.or(undefined)
}
}
// #region transform and refine
export function transform<T, U>(fn: (data: T) => U): Modifier<T, U> {
return (data) => ok(fn(data))
}
export function refine<T, U extends T>(
fn: (data: T) => data is U,
reason?: string,
): Modifier<T, U>
export function refine<T>(
fn: (data: T) => boolean,
reason?: string,
): Modifier<T, T>
export function refine<T>(
fn: (data: T) => boolean,
reason = "Refinement failed.",
): Modifier<T, T> {
return (data) => (fn(data) ? ok(data) : error(reason))
}
// #endregion
export const z = new Validator([])
class StringValidator<
Start extends string = "",
End extends string = "",
> extends Validator<`${Start}${string}${End}`> {
protected readonly core = [
refine(
(data): data is string => typeof data == "string",
`Expected a string.`,
),
] as const
min(length: number) {
return this.refine((data) => data.length >= length)
}
max(length: number) {
return this.refine((data) => data.length <= length)
}
regex(regex: RegExp) {
return this.refine((data) => !!data.match(regex))
}
startsWith<T extends `${Start}${string}`>(text: T): StringValidator<T, End> {
return this.refine((data): data is `${T}${string}${End}` & typeof data =>
data.startsWith(text),
) as unknown as StringValidator<T, End>
}
endsWith<T extends `${string}${End}`>(text: T): StringValidator<Start, T> {
return this.refine((data): data is `${Start}${string}${T}` & typeof data =>
data.endsWith(text),
) as unknown as StringValidator<Start, T>
}
}
export const string = new StringValidator([])
class NumberValidator extends Validator<number> {
protected readonly core = [
refine(
(data): data is number => typeof data == "number",
`Expected a number.`,
),
] as const
min(value: number) {
return this.refine((data) => data >= value)
}
max(value: number) {
return this.refine((data) => data <= value)
}
}
export const number = new NumberValidator([])
class BigIntValidator extends Validator<bigint> {
protected readonly core = [
refine(
(data): data is bigint => typeof data == "bigint",
`Expected a bigint.`,
),
] as const
min(value: bigint) {
return this.refine((data) => data >= value)
}
max(value: bigint) {
return this.refine((data) => data <= value)
}
}
export const bigint = new BigIntValidator([])
export const boolean = z.refine(
(data): data is boolean => typeof data == "boolean",
`Expected a boolean.`,
)
export const symbol = z.refine(
(data): data is symbol => typeof data == "symbol",
`Expected a symbol.`,
)
const Null = z.refine((data): data is null => data === null, `Expected null.`)
export { Null as null }
export const undefined = z.refine(
(data): data is undefined => typeof data == "undefined",
`Expected undefined.`,
)
class ArrayValidator<T, NonEmpty extends boolean = false> extends Validator<
NonEmpty extends true ? readonly [T, ...(readonly T[])] : readonly T[]
> {
protected readonly core = [
refine(
(data): data is readonly T[] => Array.isArray(data),
`Expected an array.`,
),
transform((data: readonly T[]) => [...data]),
] as any
item<U>(item: Validator<U>): ArrayValidator<U> {
return this.chain((data) => {
let ok = true
const errors = []
const value = []
// `for-of` loops skip over empty elements, so we have to use an indexed loop.
for (let index = 0; index < data.length; index++) {
const result = item.parse(data[index])
if (!result.ok) {
ok = false
errors.push(...result.errors)
} else {
value.push(result.value)
}
}
return ok ? { ok, value } : { ok, errors }
}) as unknown as ArrayValidator<U>
}
nonEmpty(): ArrayValidator<T, true> {
return this.refine((data) => data.length > 0) as ArrayValidator<T, true>
}
min(length: number) {
return this.refine((data) => data.length >= length)
}
max(length: number) {
return this.refine((data) => data.length <= length)
}
length(length: number) {
return this.refine((data) => data.length == length)
}
}
export const array = new ArrayValidator([])
class TupleValidator<
I extends readonly unknown[] = [],
R extends readonly unknown[] = [],
> extends Validator<[...I, ...R]> {
protected readonly core = [
refine(
(data): data is readonly unknown[] => Array.isArray(data),
`Expected an array.`,
),
transform((data: readonly unknown[]) => [...data]),
(data: readonly unknown[]): Result<[...I, ...R]> => {
let ok = true
const errors: string[] = []
const value = []
// `for-of` loops skip over empty elements, so we have to use an indexed loop.
for (let index = 0; index < this.tupleItems.length; index++) {
const result = this.tupleItems[index]!.parse(data[index])
if (!result.ok) {
ok = false
errors.push(
...joinErrors(
`Found error at index ${index} of array: `,
result.errors,
),
)
} else {
value.push(result.value)
}
}
if (this.tupleRest) {
for (let index = this.tupleItems.length; index < data.length; index++) {
const result = this.tupleRest.parse(data[index])
if (!result.ok) {
ok = false
errors.push(
...joinErrors(
`Found error at index ${index} of array: `,
result.errors,
),
)
} else {
value.push(result.value)
}
}
} else if (data.length > this.tupleItems.length) {
return {
ok: false,
errors: errors.concat(
`Found ${data.length} items; expected ${this.tupleItems.length}`,
),
}
}
return ok
? { ok, value: value as readonly unknown[] as [...I, ...R] }
: { ok, errors }
},
] as any
constructor(
modifiers: readonly Modifier<any, any>[],
protected readonly tupleItems: readonly AnyValidator[],
protected readonly tupleRest?: AnyValidator,
) {
super(modifiers)
}
protected duplicate(
newModifiers: readonly Modifier<any, any>[],
newTupleRest = this.tupleRest,
): this {
return new (this.constructor as typeof TupleValidator)(
newModifiers,
this.tupleItems,
newTupleRest,
) as this
}
rest<U>(rest: Validator<U>): TupleValidator<I, readonly U[]> {
return this.duplicate(this.modifiers, rest) as TupleValidator<
I,
readonly U[]
>
}
}
export const tuple = <T extends readonly AnyValidator[]>(...tupleItems: T) =>
new TupleValidator<{ readonly [K in keyof T]: Infer<T[K]> }>([], tupleItems)
class ObjectValidator<T extends object = {}> extends Validator<T> {
protected readonly core = [
refine(
(data): data is object => typeof data == "object",
`Expected an object.`,
),
transform((data: object) => ({ ...data })),
] as any
shape<U extends Record<string, AnyValidator>>(
shape: U,
): ObjectValidator<{ readonly [K in keyof U]: Infer<U[K]> }> {
return this.chain<{ readonly [K in keyof U]: Infer<U[K]> }>((data) => {
let ok = true
const errors = []
const value: { [K in keyof U]: Infer<U[K]> } = Object.create(null)
for (const key in shape) {
const dataAtKey = data[key as Extract<keyof U, string> & keyof T]
const result = shape[key]!.parse(dataAtKey)
if (!result.ok) {
ok = false
if (typeof dataAtKey == "undefined") {
errors.push(`Missing required key ${key}.`)
} else {
errors.push(...result.errors)
}
} else {
value[key] = result.value
}
}
return ok ? { ok, value } : { ok, errors }
}) as any
}
}
export const object = new ObjectValidator([])
export const lazy = <T>(getter: () => Validator<T>): Validator<T> => {
let validator: Validator<T> | undefined
return new Validator<T>([
(value) => {
if (!validator) {
validator = getter()
}
return validator.parse(value)
},
])
}