-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.ts
360 lines (322 loc) · 9.47 KB
/
validate.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
import { VStruct } from "./vstruct.ts";
const IS_UNION = Symbol("is_union");
const IS_OPTIONAL = Symbol("is_optional");
const IS_NULLABLE = Symbol("is_nullable");
const IS_ANY = Symbol("is_any");
const IS_UNKNOWN = Symbol("is_unknown");
const IS_NEVER = Symbol("is_never");
type Nullable<T> = {
[IS_NULLABLE]: true;
type: T;
};
type Optional<T> = {
[IS_OPTIONAL]: true;
type: T;
};
type Union<T extends any[]> = {
[IS_UNION]: true;
types: T;
};
type Any = {
[IS_ANY]: true;
};
type Unknown = {
[IS_UNKNOWN]: true;
};
type Never = {
[IS_NEVER]: true;
};
export type SchemaPrimitives =
| string
| number
| boolean
| bigint
| undefined
| null
| symbol;
type SchemaTypeConstructors =
| StringConstructor
| NumberConstructor
| BooleanConstructor
| BigIntConstructor
| SymbolConstructor
| DateConstructor
| ObjectConstructor
| FunctionConstructor
| MapConstructor
| SetConstructor
| WeakMapConstructor
| WeakSetConstructor
| ArrayBufferConstructor
| DataViewConstructor
| Int8ArrayConstructor
| Uint8ArrayConstructor
| Uint8ClampedArrayConstructor
| Int16ArrayConstructor
| Uint16ArrayConstructor
| Int32ArrayConstructor
| Uint32ArrayConstructor
| Float32ArrayConstructor
| Float64ArrayConstructor;
export type Schema =
| {
[key: string | number]:
| SchemaPrimitives
| SchemaTypeConstructors
| Schema
| Schema[];
}
| SchemaPrimitives
| SchemaTypeConstructors
| Schema[];
type OptionalKeys<T> = {
[K in keyof T]: T[K] extends Optional<any> ? K
: T[K] extends Never ? K : never;
}[keyof T];
type NonOptional<T> = Exclude<keyof T, OptionalKeys<T>>;
export type SchemaToInferredTypes<T> = T extends StringConstructor ? string
: T extends NumberConstructor ? number
: T extends BooleanConstructor ? boolean
: T extends Nullable<infer U>
? SchemaToInferredTypes<U> | null | undefined | void
: T extends Optional<infer U> ? SchemaToInferredTypes<U>
: T extends Union<infer U> ? SchemaToInferredTypes<U[number]>
: T extends Any ? any
: T extends Unknown ? unknown
: T extends Never ? never
: T extends Array<infer U> ? SchemaToInferredTypes<U>[]
: T extends Record<string | number | symbol, any> ? MapSchemaTypes<T>
: T;
type MapSchemaTypes<T extends Schema> =
& {
[K in NonOptional<T>]: SchemaToInferredTypes<T[K]>;
}
& {
[K in OptionalKeys<T>]?: SchemaToInferredTypes<T[K]>;
};
export type InferSchemaType<T extends Schema> = MapSchemaTypes<T>;
const proxyCache = new WeakMap();
export function createDeepOnChangeProxy<T>(
target: T,
schema: Schema,
quiet = false,
) {
//@ts-ignore
return new Proxy(target, {
get(target, property) {
//@ts-ignore
const item = target[property];
if (item && typeof item === "object") {
if (proxyCache.has(item)) return proxyCache.get(item);
//@ts-ignore
const proxy = createDeepOnChangeProxy(item, schema[property]);
proxyCache.set(item, proxy);
return proxy;
}
return item;
},
set(target, property, newValue) {
try {
//@ts-ignore
validate(schema[property], newValue);
} catch {
return quiet;
}
//@ts-ignore
target[property] = newValue;
return true;
},
});
}
const schemaConstructorMap = {
String: (input: unknown) => typeof input === "string",
Number: (input: unknown) => typeof input === "number",
Boolean: (input: unknown) => typeof input === "boolean",
BigInt: (input: unknown) => typeof input === "bigint",
Symbol: (input: unknown) => typeof input === "symbol",
Date: (input: unknown) => input instanceof Date,
Object: (input: unknown) => typeof input === "object",
Function: (input: unknown) => typeof input === "function",
Map: (input: unknown) => input instanceof Map,
Set: (input: unknown) => input instanceof Set,
WeakMap: (input: unknown) => input instanceof WeakMap,
WeakSet: (input: unknown) => input instanceof WeakSet,
ArrayBuffer: (input: unknown) => input instanceof ArrayBuffer,
DataView: (input: unknown) => input instanceof DataView,
Int8Array: (input: unknown) => input instanceof Int8Array,
Uint8Array: (input: unknown) => input instanceof Uint8Array,
Uint8ClampedArray: (input: unknown) => input instanceof Uint8ClampedArray,
Int16Array: (input: unknown) => input instanceof Int16Array,
Uint16Array: (input: unknown) => input instanceof Uint16Array,
Int32Array: (input: unknown) => input instanceof Int32Array,
Uint32Array: (input: unknown) => input instanceof Uint32Array,
Float32Array: (input: unknown) => input instanceof Float32Array,
Float64Array: (input: unknown) => input instanceof Float64Array,
};
export function createSchema<T extends Schema>(schema: T): T {
return schema;
}
export function isSchemaPrimitive(input: unknown): input is SchemaPrimitives {
return typeof input === "string" || typeof input === "number" ||
typeof input === "boolean" || typeof input === "bigint";
}
export function isNull(input: unknown): input is null {
return input === null;
}
export function isFunction(input: unknown): input is Function {
return typeof input === "function";
}
function isNullableHelper(input: unknown): input is Nullable<unknown> {
return typeof input === "object" && input !== null && IS_NULLABLE in input;
}
function isOptionalHelper(input: unknown): input is Optional<unknown> {
return typeof input === "object" && input !== null && IS_OPTIONAL in input;
}
function isUnionHelper(input: unknown): input is Union<unknown[]> {
return typeof input === "object" && input !== null && IS_UNION in input;
}
export function isAny(input: unknown): input is Any {
return typeof input === "object" && input !== null && IS_ANY in input;
}
export function isUnknown(input: unknown): input is Unknown {
return typeof input === "object" && input !== null && IS_UNKNOWN in input;
}
export function isNever(input: unknown): input is Never {
return typeof input === "object" && input !== null && IS_NEVER in input;
}
export function isArray(input: unknown): input is unknown[] {
return Array.isArray(input);
}
export function isObject(input: unknown): input is object {
return typeof input === "object" && input !== null;
}
export function Nullable<T>(type: T) {
return {
[IS_NULLABLE]: true,
type,
} as Nullable<T>;
}
export function Optional<T>(type: T) {
return {
[IS_OPTIONAL]: true,
type,
} as Optional<T>;
}
export function Union<T extends any[]>(...types: T) {
return {
[IS_UNION]: true,
types,
} as Union<T>;
}
export const Any: Any = {
[IS_ANY]: true,
};
export const Unknown: Unknown = {
[IS_UNKNOWN]: true,
};
export const Never: Never = {
[IS_NEVER]: true,
};
export function validate<T>(schema: Schema, input: T) {
function validateImpl(schema: unknown, input: unknown) {
if (typeof schema === "undefined" && typeof input !== "undefined") {
throw new Error(
`Validation error: Expected undefined but got ${typeof input}`,
);
}
if (isNull(schema)) {
if (!isNull(input)) {
throw new Error(
`Validation error: Expected null but got ${typeof input}`,
);
}
return;
}
if (isFunction(schema)) {
// @ts-ignore yah yah whatever
if (!schemaConstructorMap[schema.name]?.(input)) {
throw new Error(
`Validation error: Expected ${schema.name} but got ${typeof input}`,
);
}
return;
}
if (isArray(schema)) {
if (!isArray(input)) {
throw new Error(
`Validation error: Expected array but got ${typeof input}`,
);
}
if (input.length === 0) return;
for (const item of input) {
validateImpl(schema[0], item);
}
return;
}
if (isObject(schema)) {
if (isNullableHelper(schema)) {
if (typeof input === "undefined") {
throw new Error(
`Validation error: Expected ${schema.type} but got undefined`,
);
}
if (input !== null) {
validateImpl(schema.type, input);
}
return;
}
if (isOptionalHelper(schema)) {
input && validateImpl(schema.type, input);
return;
}
if (isUnionHelper(schema)) {
let found = false;
for (const type of schema.types) {
try {
validateImpl(type, input);
found = true;
break;
} catch {}
}
if (!found) {
throw new Error(
`Validation error: Expected ${
schema.types.map((t) => t).join(", ")
} but got ${typeof input}`,
);
}
return;
}
if (isAny(schema)) return;
if (isUnknown(schema)) return;
if (isNever(schema)) {
throw new Error("Validation error: Expected never but got something");
}
for (const [key, value] of Object.entries(schema)) {
// @ts-ignore key now exists in input
validateImpl(value, input[key]);
}
return;
}
if (isSchemaPrimitive(schema)) {
if (typeof input !== typeof schema) {
throw new Error(
`Validation error: Expected ${typeof schema} but got ${typeof input}`,
);
}
return;
}
}
validateImpl(schema, input);
return input;
}
export function safeValidate<T extends object>(schema: Schema, input: T) {
try {
return validate(schema, input);
} catch {
return null;
}
}
export function schemaToProxy(schema: Schema, input: unknown, quiet = false) {
return createDeepOnChangeProxy(input, schema, quiet);
}