|
1 | 1 | import { |
2 | | - StringValidator, |
3 | | - NumberValidator, |
| 2 | + O, |
| 3 | + R, |
4 | 4 | BooleanValidator, |
5 | | - Validator, |
6 | 5 | ListValidator, |
7 | 6 | ListofValidator, |
| 7 | + NumberValidator, |
8 | 8 | RecordValidator, |
9 | 9 | RecordofValidator, |
| 10 | + Schema, |
| 11 | + StringValidator, |
| 12 | + Validator, |
| 13 | + BooleanOptions, |
| 14 | + ListOptions, |
| 15 | + ListofOptions, |
| 16 | + NumberOptions, |
| 17 | + RecordOptions, |
| 18 | + RecordofOptions, |
| 19 | + StringOptions, |
10 | 20 | } from './validatorTypes'; |
11 | | -import { $string, $number, $boolean, $list, $listof, $record, $recordof } from './constants'; |
12 | | - |
13 | | -type WithoutType<T> = Omit<T, 'type'>; |
14 | | - |
15 | | -type Helpers = |
16 | | - | typeof $string |
17 | | - | typeof $number |
18 | | - | typeof $boolean |
19 | | - | typeof $list |
20 | | - | typeof $listof |
21 | | - | typeof $record |
22 | | - | typeof $recordof; |
23 | 21 |
|
24 | | -const optional = (v?: boolean) => (typeof v == 'boolean' ? v : false); |
25 | | - |
26 | | -const base = <T extends Helpers>(type: T, optional: boolean) => ({ type, optional }); |
27 | | - |
28 | | -export type StringOptions = Partial<WithoutType<StringValidator>>; |
29 | | -export type NumberOptions = Partial<WithoutType<NumberValidator>>; |
30 | | -export type BooleanOptions = Partial<WithoutType<BooleanValidator>>; |
31 | | -export type ListOptions = { optional?: boolean }; |
32 | | -export type ListofOptions = { optional?: boolean }; |
33 | | -export type RecordOptions = { optional?: boolean }; |
34 | | -export type RecordofOptions = { optional?: boolean }; |
35 | | - |
36 | | -export function string(opts?: StringOptions): StringValidator { |
| 22 | +export function string(config: { optional: true } & Omit<StringOptions, 'type'>): O<StringOptions>; |
| 23 | +export function string(config: { optional: false } & Omit<StringOptions, 'type'>): R<StringOptions>; |
| 24 | +export function string( |
| 25 | + config: { optional?: boolean } & Omit<StringOptions, 'type'> |
| 26 | +): StringValidator; |
| 27 | +export function string(config: Omit<StringOptions, 'type'>): R<StringOptions>; |
| 28 | +export function string(): R<StringOptions>; |
| 29 | +export function string( |
| 30 | + config?: { optional?: boolean } & Omit<StringOptions, 'type'> |
| 31 | +): StringValidator { |
37 | 32 | return { |
38 | | - ...opts, |
39 | | - ...base($string, optional(opts?.optional)), |
| 33 | + type: 'string', |
| 34 | + optional: Boolean(config?.optional), |
| 35 | + ...config, |
40 | 36 | }; |
41 | 37 | } |
42 | 38 |
|
43 | | -export function boolean(opts?: BooleanOptions): BooleanValidator { |
44 | | - return base($boolean, optional(opts?.optional)); |
45 | | -} |
46 | | - |
47 | | -export function number(opts?: NumberOptions): NumberValidator { |
| 39 | +export function number(config: { optional: true } & Omit<NumberOptions, 'type'>): O<NumberOptions>; |
| 40 | +export function number(config: { optional: false } & Omit<NumberOptions, 'type'>): R<NumberOptions>; |
| 41 | +export function number( |
| 42 | + config: { optional?: boolean } & Omit<NumberOptions, 'type'> |
| 43 | +): NumberValidator; |
| 44 | +export function number(config: Omit<NumberOptions, 'type'>): R<NumberOptions>; |
| 45 | +export function number(): R<NumberOptions>; |
| 46 | +export function number( |
| 47 | + config?: { optional?: boolean } & Omit<NumberOptions, 'type'> |
| 48 | +): NumberValidator { |
48 | 49 | return { |
49 | | - ...opts, |
50 | | - ...base($number, optional(opts?.optional)), |
| 50 | + type: 'number', |
| 51 | + optional: Boolean(config?.optional), |
| 52 | + ...config, |
51 | 53 | }; |
52 | 54 | } |
53 | 55 |
|
54 | | -export function list<V extends Validator[]>(shape: V, opts?: ListOptions): ListValidator { |
| 56 | +export function boolean(config: { optional: true }): O<BooleanOptions>; |
| 57 | +export function boolean(config: { optional: false }): R<BooleanOptions>; |
| 58 | +export function boolean(): R<BooleanOptions>; |
| 59 | +export function boolean(config?: { optional: boolean }): BooleanValidator { |
55 | 60 | return { |
56 | | - ...base($list, optional(opts?.optional)), |
57 | | - shape, |
| 61 | + type: 'boolean', |
| 62 | + optional: Boolean(config?.optional), |
58 | 63 | }; |
59 | 64 | } |
60 | 65 |
|
61 | | -export function listof<V extends Validator>(of: V, opts?: ListofOptions): ListofValidator { |
| 66 | +export function list<T extends Validator[]>(list: T): R<ListOptions<T>>; |
| 67 | +export function list<T extends Validator[]>( |
| 68 | + list: T, |
| 69 | + config: { optional: false } |
| 70 | +): R<ListOptions<T>>; |
| 71 | +export function list<T extends Validator[]>(list: T, config: { optional: true }): O<ListOptions<T>>; |
| 72 | +export function list<T extends Validator[]>( |
| 73 | + list: T, |
| 74 | + config?: { optional: boolean } |
| 75 | +): ListValidator<T> { |
| 76 | + return { type: 'list', optional: Boolean(config?.optional), shape: list }; |
| 77 | +} |
| 78 | + |
| 79 | +export function listof<T extends Validator>(v: T): R<ListofOptions<T>>; |
| 80 | +export function listof<T extends Validator>(v: T, config: { optional: false }): R<ListofOptions<T>>; |
| 81 | +export function listof<T extends Validator>(v: T, config: { optional: true }): O<ListofOptions<T>>; |
| 82 | +export function listof<T extends Validator>( |
| 83 | + v: T, |
| 84 | + config?: { optional: boolean } |
| 85 | +): ListofValidator<T> { |
62 | 86 | return { |
63 | | - ...base($listof, optional(opts?.optional)), |
64 | | - of, |
| 87 | + type: 'listof', |
| 88 | + optional: Boolean(config?.optional), |
| 89 | + of: v, |
65 | 90 | }; |
66 | 91 | } |
67 | 92 |
|
68 | | -export function record<S extends { [key: string]: Validator }>( |
69 | | - shape: S, |
70 | | - opts?: RecordOptions |
71 | | -): RecordValidator { |
| 93 | +export function record<T extends Schema>(s: T): R<RecordOptions<T>>; |
| 94 | +export function record<T extends Schema>(s: T, config: { optional: false }): R<RecordOptions<T>>; |
| 95 | +export function record<T extends Schema>(s: T, config: { optional: true }): O<RecordOptions<T>>; |
| 96 | +export function record<T extends Schema>(s: T, config?: { optional: boolean }): RecordValidator<T> { |
72 | 97 | return { |
73 | | - ...base($record, optional(opts?.optional)), |
74 | | - shape, |
| 98 | + type: 'record', |
| 99 | + optional: Boolean(config?.optional), |
| 100 | + shape: s, |
75 | 101 | }; |
76 | 102 | } |
77 | 103 |
|
78 | | -export function recordof<V extends Validator>(of: V, opts?: RecordofOptions): RecordofValidator { |
| 104 | +export function recordof<T extends Validator>(v: T): R<RecordofOptions<T>>; |
| 105 | +export function recordof<T extends Validator>( |
| 106 | + v: T, |
| 107 | + config: { optional: false } |
| 108 | +): R<RecordofOptions<T>>; |
| 109 | +export function recordof<T extends Validator>( |
| 110 | + v: T, |
| 111 | + config: { optional: true } |
| 112 | +): O<RecordofOptions<T>>; |
| 113 | +export function recordof<T extends Validator>( |
| 114 | + v: T, |
| 115 | + config?: { optional: boolean } |
| 116 | +): RecordofValidator<T> { |
79 | 117 | return { |
80 | | - ...base($recordof, optional(opts?.optional)), |
81 | | - of, |
| 118 | + type: 'recordof', |
| 119 | + of: v, |
| 120 | + optional: Boolean(config?.optional), |
82 | 121 | }; |
83 | 122 | } |
0 commit comments