forked from TCMiranda/joi-extract-type
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
580 lines (495 loc) · 19.5 KB
/
index.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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/** @format */
import * as Joi from '@hapi/joi';
/**
* Helpers
*/
type ArrayType<T> = T extends (infer U)[] ? U : never;
declare module '@hapi/joi' {
/**
* Allow extend() to use Joi types by default
*/
export function extend(
extension: Extension | Extension[],
...extensions: Array<Extension | Extension[]>
): typeof Joi;
/**
* Field requirements interface
*/
interface Box<T, R extends boolean> {
/** Type the schema holds */
T: T;
/** If this attribute is required when inside an object */
R: R;
}
// Operators
type BoxType<B, nT> = B extends Box<infer oT, infer oR> ? Box<nT, oR> : B;
type BoxUnion<B, nT> = B extends Box<infer oT, infer oR> ? Box<oT | nT, oR> : B;
type BoxIntersection<B, nT> = B extends Box<infer oT, infer oR> ? Box<oT & nT, oR> : B;
type BoxReq<B, nR extends boolean> = B extends Box<infer oT, infer oR> ? Box<oT, nR> : B;
type BoxSchema = Box<any, boolean>;
/**
* Every Schema that implements the Box to allow the extraction
*/
type BoxedPrimitive<T extends BoxSchema = any> =
| BoxAnySchema<T>
| BoxStringSchema<T>
| BoxNumberSchema<T>
| BoxBooleanSchema<T>
| BoxDateSchema<T>
| BoxFunctionSchema<T>
| BoxArraySchema<T>
| BoxObjectSchema<T>
| BoxAlternativesSchema<T>;
// Base types
type primitiveType = string | number | boolean | Function | Date | undefined | null | void;
export type mappedSchema = SchemaLike | BoxedPrimitive;
export type mappedSchemaMap = { [K: string]: mappedSchema };
export type extendsGuard<T, S> = S extends T ? S : T;
/**
* Validation: extraction decorated methods
*/
export function validate<T, S extends mappedSchemaMap>(
value: T,
schema: S
): ValidationResult<extendsGuard<T, extractType<S>>>;
export function validate<T, S extends mappedSchemaMap>(
value: T,
schema: S,
options: ValidationOptions
): ValidationResult<extendsGuard<T, extractType<S>>>;
export function validate<T, R, S extends mappedSchemaMap>(
value: T,
schema: S,
options: ValidationOptions,
callback: (err: ValidationError, value: extendsGuard<T, extractType<S>>) => R
): R;
export function validate<T, R, S extends mappedSchemaMap>(
value: T,
schema: S,
callback: (err: ValidationError, value: extendsGuard<T, extractType<S>>) => R
): R;
// TODO: concat
// concat(schema: this): this;
// TODO: when
// when(ref: string, options: WhenOptions): BoxAlternativesSchema;
// when(ref: Reference, options: WhenOptions): BoxAlternativesSchema;
// when(ref: Schema, options: WhenSchemaOptions): BoxAlternativesSchema;
// TODO: see if .default union makes sense;
export interface BoxAnySchema<N extends Box<any, boolean>> extends AnySchema {
__schemaTypeLiteral: 'BoxAnySchema';
default<T>(
value: T,
description?: string
): this extends BoxAnySchema<infer B> ? BoxAnySchema<BoxUnion<B, T>> : never;
default(value: any, description?: string): this;
default(): this;
allow<T>(
...values: T[]
): this extends BoxAnySchema<infer B> ? BoxAnySchema<BoxUnion<B, T>> : never;
allow<T>(
values: T[]
): this extends BoxAnySchema<infer B> ? BoxAnySchema<BoxUnion<B, T>> : never;
allow(...values: any[]): this;
allow(values: any[]): this;
valid<T>(
...values: T[]
): this extends BoxAnySchema<infer B> ? BoxAnySchema<BoxType<B, T>> : never;
valid<T>(values: T[]): this extends BoxAnySchema<infer B> ? BoxAnySchema<BoxType<B, T>> : never;
valid(...values: any[]): this;
valid(values: any[]): this;
required(): this extends BoxAnySchema<infer B> ? BoxAnySchema<BoxReq<B, true>> : never;
required(): this;
exist(): this extends BoxAnySchema<infer B> ? BoxAnySchema<BoxReq<B, true>> : never;
exist(): this;
optional(): this extends BoxAnySchema<infer B> ? BoxAnySchema<BoxReq<B, false>> : never;
optional(): this;
}
/**
* String: extraction decorated schema
*/
export interface BoxStringSchema<N extends BoxSchema> extends StringSchema {
__schemaTypeLiteral: 'BoxStringSchema';
default<T extends string>(
value: T,
description?: string
): this extends BoxStringSchema<infer B> ? BoxStringSchema<BoxUnion<B, T>> : never;
default(value: any, description?: string): this;
default(): this;
allow<T>(
...values: T[]
): this extends BoxStringSchema<infer B> ? BoxStringSchema<BoxUnion<B, T>> : never;
allow<T>(
values: T[]
): this extends BoxStringSchema<infer B> ? BoxStringSchema<BoxUnion<B, T>> : never;
allow(...values: any[]): this;
allow(values: any[]): this;
valid<T extends string>(
...values: T[]
): this extends BoxStringSchema<infer B> ? BoxStringSchema<BoxType<B, T>> : never;
valid<T extends string>(
values: T[]
): this extends BoxStringSchema<infer B> ? BoxStringSchema<BoxType<B, T>> : never;
valid(...values: any[]): this;
valid(values: any[]): this;
required(): this extends BoxStringSchema<infer B> ? BoxStringSchema<BoxReq<B, true>> : never;
required(): this;
exist(): this extends BoxStringSchema<infer B> ? BoxStringSchema<BoxReq<B, true>> : never;
exist(): this;
optional(): this extends BoxStringSchema<infer B> ? BoxStringSchema<BoxReq<B, false>> : never;
optional(): this;
}
/**
* Number: extraction decorated schema
*/
export interface BoxNumberSchema<N extends BoxSchema> extends NumberSchema {
__schemaTypeLiteral: 'BoxNumberSchema';
default<T extends number>(
value: T,
description?: string
): this extends BoxNumberSchema<infer B> ? BoxNumberSchema<BoxUnion<B, T>> : never;
default(value: any, description?: string): this;
default(): this;
allow<T>(
...values: T[]
): this extends BoxNumberSchema<infer B> ? BoxNumberSchema<BoxUnion<B, T>> : never;
allow<T>(
values: T[]
): this extends BoxNumberSchema<infer B> ? BoxNumberSchema<BoxUnion<B, T>> : never;
allow(...values: any[]): this;
allow(values: any[]): this;
valid<T extends string>(
...values: T[]
): this extends BoxNumberSchema<infer B> ? BoxNumberSchema<BoxType<B, T>> : never;
valid<T extends string>(
values: T[]
): this extends BoxNumberSchema<infer B> ? BoxNumberSchema<BoxType<B, T>> : never;
valid(...values: any[]): this;
valid(values: any[]): this;
required(): this extends BoxNumberSchema<infer B> ? BoxNumberSchema<BoxReq<B, true>> : never;
required(): this;
exist(): this extends BoxNumberSchema<infer B> ? BoxNumberSchema<BoxReq<B, true>> : never;
exist(): this;
optional(): this extends BoxNumberSchema<infer B> ? BoxNumberSchema<BoxReq<B, false>> : never;
optional(): this;
}
/**
* Boolean: extraction decorated schema
*/
export interface BoxBooleanSchema<N extends BoxSchema> extends BooleanSchema {
__schemaTypeLiteral: 'BoxBooleanSchema';
default<T extends boolean>(
value: T,
description?: string
): this extends BoxBooleanSchema<infer B> ? BoxBooleanSchema<BoxUnion<B, T>> : never;
default(value: any, description?: string): this;
default(): this;
allow<T>(
...values: T[]
): this extends BoxBooleanSchema<infer B> ? BoxBooleanSchema<BoxUnion<B, T>> : never;
allow<T>(
values: T[]
): this extends BoxBooleanSchema<infer B> ? BoxBooleanSchema<BoxUnion<B, T>> : never;
allow(...values: any[]): this;
allow(values: any[]): this;
valid<T extends string>(
...values: T[]
): this extends BoxBooleanSchema<infer B> ? BoxBooleanSchema<BoxType<B, T>> : never;
valid<T extends string>(
values: T[]
): this extends BoxBooleanSchema<infer B> ? BoxBooleanSchema<BoxType<B, T>> : never;
valid(...values: any[]): this;
valid(values: any[]): this;
required(): this extends BoxBooleanSchema<infer B> ? BoxBooleanSchema<BoxReq<B, true>> : never;
required(): this;
exist(): this extends BoxBooleanSchema<infer B> ? BoxBooleanSchema<BoxReq<B, true>> : never;
exist(): this;
optional(): this extends BoxBooleanSchema<infer B> ? BoxBooleanSchema<BoxReq<B, false>> : never;
optional(): this;
}
/**
* Date: extraction decorated schema
*/
export interface BoxDateSchema<N extends BoxSchema> extends DateSchema {
__schemaTypeLiteral: 'BoxDateSchema';
default<T extends Date>(
value: T,
description?: string
): this extends BoxDateSchema<infer B> ? BoxDateSchema<BoxUnion<B, T>> : never;
default(value: any, description?: string): this;
default(): this;
allow<T>(
...values: T[]
): this extends BoxDateSchema<infer B> ? BoxDateSchema<BoxUnion<B, T>> : never;
allow<T>(
values: T[]
): this extends BoxDateSchema<infer B> ? BoxDateSchema<BoxUnion<B, T>> : never;
allow(...values: any[]): this;
allow(values: any[]): this;
valid<T extends string>(
...values: T[]
): this extends BoxDateSchema<infer B> ? BoxDateSchema<BoxType<B, T>> : never;
valid<T extends string>(
values: T[]
): this extends BoxDateSchema<infer B> ? BoxDateSchema<BoxType<B, T>> : never;
valid(...values: any[]): this;
valid(values: any[]): this;
required(): this extends BoxDateSchema<infer B> ? BoxDateSchema<BoxReq<B, true>> : never;
required(): this;
exist(): this extends BoxDateSchema<infer B> ? BoxDateSchema<BoxReq<B, true>> : never;
exist(): this;
optional(): this extends BoxDateSchema<infer B> ? BoxDateSchema<BoxReq<B, false>> : never;
optional(): this;
}
/**
* Function: extraction decorated schema
*/
export interface BoxFunctionSchema<N extends BoxSchema> extends FunctionSchema {
__schemaTypeLiteral: 'BoxFunctionSchema';
allow<T>(
...values: T[]
): this extends BoxFunctionSchema<infer B> ? BoxFunctionSchema<BoxUnion<B, T>> : never;
allow<T>(
values: T[]
): this extends BoxFunctionSchema<infer B> ? BoxFunctionSchema<BoxUnion<B, T>> : never;
allow(...values: any[]): this;
allow(values: any[]): this;
required(): this extends BoxFunctionSchema<infer B>
? BoxFunctionSchema<BoxReq<B, true>>
: never;
required(): this;
exist(): this extends BoxFunctionSchema<infer B> ? BoxFunctionSchema<BoxReq<B, true>> : never;
exist(): this;
optional(): this extends BoxFunctionSchema<infer B>
? BoxFunctionSchema<BoxReq<B, false>>
: never;
optional(): this;
}
/**
* Array: extraction decorated schema
*/
export interface BoxArraySchema<N extends BoxSchema> extends ArraySchema {
__schemaTypeLiteral: 'BoxArraySchema';
default<T extends any[]>(
value: T,
description?: string
): this extends BoxArraySchema<infer B> ? BoxArraySchema<BoxUnion<B, ArrayType<T>>> : never;
default(value: any, description?: string): this;
default(): this;
allow<T>(
...values: T[]
): this extends BoxArraySchema<infer B> ? BoxArraySchema<BoxUnion<B, T>> : never;
allow<T>(
values: T[]
): this extends BoxArraySchema<infer B> ? BoxArraySchema<BoxUnion<B, T>> : never;
allow(...values: any[]): this;
allow(values: any[]): this;
items<T extends mappedSchema>(
type: T
): this extends BoxArraySchema<infer B> ? BoxArraySchema<BoxUnion<B, extractType<T>>> : never;
items(...types: SchemaLike[]): this;
items(types: SchemaLike[]): this;
required(): this extends BoxArraySchema<infer B> ? BoxArraySchema<BoxReq<B, true>> : never;
required(): this;
exist(): this extends BoxArraySchema<infer B> ? BoxArraySchema<BoxReq<B, true>> : never;
exist(): this;
optional(): this extends BoxArraySchema<infer B> ? BoxArraySchema<BoxReq<B, false>> : never;
optional(): this;
}
/**
* Object: extraction decorated schema
*/
export interface BoxObjectSchema<N extends BoxSchema> extends ObjectSchema {
__schemaTypeLiteral: 'BoxObjectSchema';
default<T extends mappedSchemaMap>(
value: T,
description?: string
): this extends BoxObjectSchema<infer B> ? BoxObjectSchema<BoxUnion<B, extractType<T>>> : never;
default(value: any, description?: string): this;
default(): this;
allow<T>(
...values: T[]
): this extends BoxObjectSchema<infer B> ? BoxObjectSchema<BoxUnion<B, T>> : never;
allow<T>(
values: T[]
): this extends BoxObjectSchema<infer B> ? BoxObjectSchema<BoxUnion<B, T>> : never;
allow(...values: any[]): this;
allow(values: any[]): this;
keys<T extends mappedSchemaMap>(
schema: T
): this extends BoxObjectSchema<infer B>
? BoxObjectSchema<BoxIntersection<B, extractMap<T>>>
: never;
keys(schema?: SchemaMap): this;
append<T extends mappedSchemaMap>(
schema: T
): this extends BoxObjectSchema<infer B>
? BoxObjectSchema<BoxIntersection<B, extractMap<T>>>
: never;
append(schema?: SchemaMap): this;
pattern<S extends BoxStringSchema<any>, T extends mappedSchema>(
pattern: S,
schema: T
): this extends BoxObjectSchema<infer B>
? BoxObjectSchema<BoxIntersection<B, extractMap<{ [key in extractType<S>]: T }>>>
: never;
pattern<T extends mappedSchema>(
pattern: RegExp,
schema: T
): this extends BoxObjectSchema<infer B>
? BoxObjectSchema<BoxIntersection<B, extractMap<{ [key: string]: T }>>>
: never;
pattern(pattern: RegExp | SchemaLike, schema: SchemaLike): this;
required(): this extends BoxObjectSchema<infer B> ? BoxObjectSchema<BoxReq<B, true>> : never;
required(): this;
exist(): this extends BoxObjectSchema<infer B> ? BoxObjectSchema<BoxReq<B, true>> : never;
exist(): this;
optional(): this extends BoxObjectSchema<infer B> ? BoxObjectSchema<BoxReq<B, false>> : never;
optional(): this;
}
/**
* Alternatives: extraction decorated schema
*/
export interface BoxAlternativesSchema<N extends BoxSchema> extends AlternativesSchema {
__schemaTypeLiteral: 'BoxAlternativesSchema';
allow<T>(
...values: T[]
): this extends BoxAlternativesSchema<infer B> ? BoxAlternativesSchema<BoxUnion<B, T>> : never;
allow<T>(
values: T[]
): this extends BoxAlternativesSchema<infer B> ? BoxAlternativesSchema<BoxUnion<B, T>> : never;
allow(...values: any[]): this;
allow(values: any[]): this;
try<T extends mappedSchema[]>(
...values: T
): this extends BoxAlternativesSchema<infer O>
? O extends Box<infer oT, infer oR>
? BoxAlternativesSchema<BoxType<O, oT | extractType<T>>>
: BoxAlternativesSchema<Box<extractType<T>, false>>
: BoxAlternativesSchema<Box<extractType<T>, false>>;
try<T extends mappedSchema[]>(
values: T
): this extends BoxAlternativesSchema<infer O>
? O extends Box<infer oT, infer oR>
? BoxAlternativesSchema<BoxType<O, oT | extractType<T>>>
: BoxAlternativesSchema<Box<extractType<T>, false>>
: BoxAlternativesSchema<Box<extractType<T>, false>>;
try(...types: SchemaLike[]): this;
try(types: SchemaLike[]): this;
required(): this extends BoxAlternativesSchema<infer B>
? BoxAlternativesSchema<BoxReq<B, true>>
: never;
required(): this;
exist(): this extends BoxAlternativesSchema<infer B>
? BoxAlternativesSchema<BoxReq<B, true>>
: never;
exist(): this;
optional(): this extends BoxAlternativesSchema<infer B>
? BoxAlternativesSchema<BoxReq<B, false>>
: never;
optional(): this;
when<
R,
T1 extends mappedSchema,
T2 extends mappedSchema,
T extends { then: T1; otherwise: T2 }
>(
ref: R,
defs: T
): this extends BoxAlternativesSchema<infer O>
? O extends Box<infer oT, infer oR>
? BoxAlternativesSchema<
BoxType<O, oT | extractType<T['then']> | extractType<T['otherwise']>>
>
: BoxAlternativesSchema<Box<extractType<T['then']> | extractType<T['otherwise']>, false>>
: BoxAlternativesSchema<Box<extractType<T['then']> | extractType<T['otherwise']>, false>>;
when(ref: string | Reference, options: WhenOptions): this;
when(ref: Schema, options: WhenSchemaOptions): this;
}
// Factory methods.
export function any<T extends any>(): BoxAnySchema<Box<T, false>>;
export function string<T extends string>(): BoxStringSchema<Box<T, false>>;
export function number<T extends number>(): BoxNumberSchema<Box<T, false>>;
export function boolean<T extends boolean>(): BoxBooleanSchema<Box<T, false>>;
export function date<T extends Date>(): BoxDateSchema<Box<T, false>>;
export function func<T extends Function>(): BoxFunctionSchema<Box<T, false>>;
export function array(): BoxArraySchema<Box<never, false>>;
export function object<T extends mappedSchemaMap>(
schema?: T
): BoxObjectSchema<Box<extractMap<T>, false>>;
export function alternatives<T extends mappedSchema[]>(
...alts: T
): BoxAlternativesSchema<Box<extractType<typeof alts[number]>, false>>;
export function alternatives<T extends mappedSchema[]>(
alts: T
): BoxAlternativesSchema<Box<extractType<typeof alts[number]>, false>>;
export function alt<T extends mappedSchema[]>(
...alts: T
): BoxAlternativesSchema<Box<extractType<typeof alts[number]>, false>>;
export function alt<T extends mappedSchema[]>(
alts: T
): BoxAlternativesSchema<Box<extractType<typeof alts[number]>, false>>;
// Required | Optional properties engine
// prettier-ignore
type Required<T, K = keyof T> = {
[j in K extends keyof T
? T[K] extends BoxedPrimitive<infer B> ? B['R'] extends true ? K : never : never
: never]: true
};
// prettier-ignore
type Optional<T, K = keyof T> = {
[j in K extends keyof T
? T[K] extends BoxedPrimitive<infer B> ? B['R'] extends false ? K : never : never
: never]: true
};
// prettier-ignore
type extractMap<T extends mappedSchemaMap> =
{ [K in keyof Optional<T>]?: extractType<T[K]> } &
{ [K in keyof Required<T>]: extractType<T[K]> };
type maybeExtractBox<T> = T extends Box<infer O, infer R> ? O : T;
// prettier-ignore
type extractOne<T extends mappedSchema> =
/** Primitive types */
T extends primitiveType ? T :
/** Holds the extracted type */
T extends BoxAnySchema<infer O> ? maybeExtractBox<O> :
T extends BoxBooleanSchema<infer O> ? maybeExtractBox<O> :
T extends BoxStringSchema<infer O> ? maybeExtractBox<O> :
T extends BoxNumberSchema<infer O> ? maybeExtractBox<O> :
T extends BoxDateSchema<infer O> ? maybeExtractBox<O> :
T extends BoxFunctionSchema<infer O> ? maybeExtractBox<O> :
T extends BoxArraySchema<infer O> ? maybeExtractBox<O>[] :
T extends BoxObjectSchema<infer O> ? maybeExtractBox<O> :
T extends BoxAlternativesSchema<infer O> ? maybeExtractBox<O> :
T extends AnySchema ? any :
any;
// prettier-ignore
export type extractType<T extends mappedSchema> =
/**
* Hack to support [Schema1, Schema2, ...N] alternatives notation
* Can't use extractType directly here because of cycles:
* ```
* T extends Array<infer O> ? extractType<O> :
* ^ cycle
* ```
*/
T extends Array<infer O> ? (
O extends SchemaLike ? extractOne<O> :
O extends BoxedPrimitive ? extractOne<O> :
O
) :
/**
* Handle Objects as schemas, without Joi.object at the root.
* It needs to come first than mappedSchema.
* It is difficult to avoid it to be inferred from extends clause.
*/
T extends mappedSchemaMap ? extractMap<T> :
/**
* This is the base case for every schema implemented
*/
T extends SchemaLike ? extractOne<T> :
T extends BoxedPrimitive ? extractOne<T> :
/**
* Default case to handle primitives and schemas
*/
extractOne<T>;
}