-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.ts
400 lines (329 loc) · 9.88 KB
/
types.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
import { type BaseType, type Infer, type LiteralKey, Type } from "./base";
import type { ParseContext, ParseOptions } from "./context";
import { internalContext, internalType } from "./internal";
import { CORE_ISSUES, type Issue } from "./issue";
import { isObject } from "./util";
const { invalid_type, required } = CORE_ISSUES;
type StringTypeConfig = {
/** Trim strings (default true) */
trim?: boolean;
/** Coerce input to string (default false) */
coerce?: boolean;
/**
* Allow empty strings (default true)
*
* If false, empty strings will raise a required issue
*/
empty?: boolean;
};
export class StringType extends Type<string> {
private trim?: boolean;
private empty?: boolean;
private _coerce?: boolean;
constructor(config?: StringTypeConfig) {
super();
this.trim = config?.trim;
this.empty = config?.empty;
this._coerce = config?.coerce;
}
protected override get defaultValue() {
return "";
}
protected override check(ctx: ParseContext, input: unknown) {
if (typeof input !== "string") {
return ctx.issue(invalid_type("string", input, ctx.path));
}
const allowEmpty = this.empty ?? ctx.options.string?.empty ?? true;
const value = this.trimValue(ctx.options, input);
if (!allowEmpty && value.length === 0) {
return ctx.issue(required(input, ctx.path));
}
return true;
}
protected override coerce(ctx: ParseContext, input: unknown) {
const coerce = this._coerce ?? ctx.options.string?.coerce ?? false;
if (coerce && input != null && typeof input !== "object") {
return String(input);
}
}
protected override clean(ctx: ParseContext, value: string) {
return this.trimValue(ctx.options, value);
}
private trimValue(options: ParseOptions, value: string) {
const trim = this.trim ?? options.string?.trim ?? true;
return trim ? value.trim() : value;
}
}
export class NumberType extends Type<number> {
_coerce?: boolean;
constructor(config?: { coerce?: boolean }) {
super();
this._coerce = config?.coerce;
}
protected override get defaultValue() {
return 0;
}
protected override check(ctx: ParseContext, input: unknown) {
return (
typeof input === "number" ||
ctx.issue(invalid_type("number", input, ctx.path))
);
}
protected override coerce(ctx: ParseContext, input: unknown) {
const coerce = this._coerce ?? ctx.options.number?.coerce ?? false;
if (coerce) {
const num = Number(input);
return Number.isNaN(num) ? undefined : num;
}
}
}
export class BooleanType extends Type<boolean> {
_coerce?: boolean;
constructor(config?: { coerce?: boolean }) {
super();
this._coerce = config?.coerce;
}
protected override get defaultValue() {
return false;
}
protected override check(ctx: ParseContext, input: unknown) {
return (
typeof input === "boolean" ||
ctx.issue(invalid_type("boolean", input, ctx.path))
);
}
protected override coerce(ctx: ParseContext, input: unknown) {
const coerce = this._coerce ?? ctx.options.boolean?.coerce ?? false;
if (coerce && input != null) {
return Boolean(input);
}
}
}
export class EnumType<S extends string> extends Type<S> {
constructor(readonly values: S[]) {
super();
}
protected override get defaultValue() {
return "" as S;
}
protected override check(ctx: ParseContext, input: unknown) {
return (
this.values.includes(input as any) ||
ctx.issue(invalid_type(`One of [${this.values}]`, input, ctx.path))
);
}
}
abstract class LiteralType<
L extends string | number | boolean,
> extends Type<L> {
constructor(readonly value: L) {
super();
}
protected override get isLiteral() {
return true;
}
protected override get literalValue() {
return this.value;
}
protected override get defaultValue() {
return this.value;
}
protected override check(ctx: ParseContext, input: unknown) {
return (
input === this.value ||
ctx.issue(invalid_type(`${this.value}`, input, ctx.path))
);
}
}
export class StringLiteralType<S extends string> extends LiteralType<S> {}
export class NumberLiteralType<N extends number> extends LiteralType<N> {}
export class BooleanLiteralType<B extends boolean> extends LiteralType<B> {}
export class FileType extends Type<File> {
protected override get defaultValue() {
return new File([], "");
}
protected override check(ctx: ParseContext, input: unknown) {
return (
input instanceof File || ctx.issue(invalid_type("file", input, ctx.path))
);
}
}
export class RecordType<V extends BaseType> extends Type<
Infer<Record<string, V>>
> {
public constructor(readonly valueType: V) {
super();
}
protected override get defaultValue() {
return {} as Infer<Record<string, V>>;
}
protected override check(ctx: ParseContext, input: unknown) {
return (
isObject(input) || ctx.issue(invalid_type("record", input, ctx.path))
);
}
protected override getChildKeys(value: Infer<Record<string, V>>) {
return Object.entries(value).map(([key]) => ({
key,
type: this.valueType,
}));
}
protected override get hasAsyncValidators() {
return (
super.hasAsyncValidators ||
internalType(this.valueType).hasAsyncValidators
);
}
}
export class ObjectType<R extends Record<string, BaseType>> extends Type<
Infer<R>
> {
private childKeys: { key: string | number; type: BaseType }[];
private literalKeys: LiteralKey[];
private _defaultValue: Infer<R>;
constructor(readonly properties: R) {
super();
this.childKeys = Object.entries(this.properties).map(([key, type]) => ({
key,
type,
}));
this.literalKeys = Object.entries(this.properties)
.filter(([key, type]) => internalType(type).isLiteral)
.map(([key, type]) => ({
key,
type,
value: internalType(type).literalValue,
}));
this._defaultValue = Object.fromEntries(
this.childKeys.map(({ key, type }) => [
key,
internalType(type).defaultValue,
]),
) as Infer<R>;
}
protected override get defaultValue() {
return this._defaultValue;
}
protected override check(ctx: ParseContext, input: unknown) {
return (
isObject(input) || ctx.issue(invalid_type("object", input, ctx.path))
);
}
protected override clean(ctx: ParseContext, value: Infer<R>) {
const extraKeys = Object.keys(value).filter((key) => !this.properties[key]);
for (const key of extraKeys) {
delete value[key];
}
return value;
}
protected override getChildKeys() {
return this.childKeys;
}
protected override getLiteralKeys() {
return this.literalKeys;
}
protected override get hasAsyncValidators() {
return (
super.hasAsyncValidators ||
Object.values(this.properties).some(
(type) => internalType(type).hasAsyncValidators,
)
);
}
}
export class ArrayType<I extends BaseType> extends Type<Infer<I[]>> {
private _coerce: boolean | undefined;
constructor(
readonly itemType: I,
config?: { coerce?: boolean },
) {
super();
this._coerce = config?.coerce;
}
protected override check(ctx: ParseContext, input: unknown) {
return (
Array.isArray(input) || ctx.issue(invalid_type("array", input, ctx.path))
);
}
protected override coerce(ctx: ParseContext, input: unknown) {
if (input != null && this._coerce) {
return [input] as Infer<I[]>;
}
}
protected override get defaultValue() {
return [] as Infer<I[]>;
}
protected override getChildKeys(array: Infer<I>[]) {
const list = [] as { key: number; type: BaseType }[];
// handle sparse arrays
for (let key = 0; key < array.length; ++key) {
if (key in array) {
list.push({ key, type: this.itemType });
}
}
return list;
}
protected override get hasAsyncValidators() {
return (
super.hasAsyncValidators || internalType(this.itemType).hasAsyncValidators
);
}
}
export class UnionType<U extends BaseType[]> extends Type<Infer<U>[number]> {
private discriminatedTypes: { type: BaseType; keys: LiteralKey[] }[];
constructor(readonly types: U) {
super();
if (types.length === 0) {
throw new Error("UnionType options cannot be empty");
}
this.discriminatedTypes = this.types
.map((type) => ({
type,
keys: internalType(type).getLiteralKeys(),
}))
.filter(({ keys }) => keys.length > 0);
}
/* v8 ignore start */
protected override check(ctx: ParseContext, input: unknown): Issue {
throw new Error("Check will be invoked on shadowed type");
}
/* v8 ignore stop */
protected override get defaultValue() {
return internalType(this.types[0]).defaultValue as Infer<U>[number];
}
protected override getShadowedType(ctx: ParseContext, input: unknown) {
const context = internalContext(ctx);
const match = this.getDiscriminatedMatch(input);
const typesToCheck = match
? [match]
: this.types.length === this.discriminatedTypes.length // if all types are discriminated
? [this.types[0]] // default to first since we didn't find a match
: this.types; // else check all types
for (const type of typesToCheck) {
const clone = context.clone();
internalType(type).parseInput(clone, input);
if (clone.issueCount === context.issueCount) {
return type;
}
}
return typesToCheck[0];
}
private getDiscriminatedMatch(input: unknown) {
if (!isObject(input)) {
return;
}
const match = this.discriminatedTypes.find(({ keys }) => {
return (
keys.length > 0 &&
keys.every(({ key, value }) => (input as any)[key] === value)
);
});
return match?.type;
}
protected override get hasAsyncValidators() {
return (
super.hasAsyncValidators ||
this.types.some((type) => internalType(type).hasAsyncValidators)
);
}
}