-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator.ts
More file actions
284 lines (265 loc) · 7.86 KB
/
validator.ts
File metadata and controls
284 lines (265 loc) · 7.86 KB
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
/**
* High-performance, schema-based validation library.
*
* Uses pre-compiled functions (via `new Function`) for validation speeds
* comparable to raw JavaScript, with full TypeScript inference.
*
* @module Validator
*
* @example
* ```ts
* import { OBJ, STR, NUM, ARR, optional, type Asserted } from '@01edu/validator'
*
* // 1. Define Schema
* const User = OBJ({
* id: NUM('internal user database ID'),
* name: STR('user full name, may includes spaces'),
* tags: ARR(STR('tags descriptive labels'), 'list of tags associated to the user'),
* meta: optional(OBJ({
* active: NUM('treating 1/0 as boolean'),
* }))
* })
*
* // 2. Infer Type
* type User = Asserted<typeof User>
* // { id: number; name: string; tags: string[]; meta?: { active: number } }
*
* // 3. Validate
* const data = await req.json()
* try {
* const user = User.assert(data) // Returns typed 'User' or throws
* } catch (e) {
* const errors = User.report(data) // Returns array of failures
* console.log(errors)
* }
* ```
*/
import type {
Asserted,
Def,
DefArray,
DefBoolean,
DefList,
DefNumber,
DefObject,
DefString,
DefUnion,
Optional,
OptionalAssert,
ValidatorFailure,
} from '@01edu/types/validator'
export type { Asserted, Def }
const reportObject = <T extends Record<string, Def>>(properties: T) => {
const body = [
'if (!o || typeof o !== "object") return [{ path: p, type: "object", value: o }]',
'const failures = []',
...Object.entries(properties).map(([key, def], i) => {
const k = JSON.stringify(key)
const path = `[...p, ${k}]`
if (def.type === 'object' || def.type === 'array') {
const check = `
const _${i} = v[${k}].report(o[${k}], ${path});
_${i}.length && failures.push(..._${i})
`
return def.optional ? `if (o[${k}] !== undefined) {${check}}` : check
}
const opt = def.optional ? `o[${k}] === undefined || ` : ''
return (`${opt}typeof o[${k}] === "${def.type}" || failures.push({ ${
[`path: ${path}`, `type: "${def.type}"`, `value: o[${k}]`].join(', ')
} })`)
}),
'return failures',
].join('\n')
return new Function('v, o, p = []', body).bind(
globalThis,
properties,
) as DefObject<T>['report']
}
const assertObject = <T extends Record<string, Def>>(properties: T) => {
const body = [
'if (!o || typeof o !== "object") throw Error("type assertion failed")',
...Object.entries(properties).map(([key, def]) => {
const k = JSON.stringify(key)
return `${
def.optional ? `v[${k}] === undefined ||` : ''
}v[${k}].assert(o[${k}])`
}),
'return o',
].join('\n')
return new Function('v, o', body).bind(globalThis, properties) as DefObject<
T
>['assert']
}
const reportArray = (def: Def) => {
const body = [
'if (!Array.isArray(a)) return [{ path: p, type: "array", value: a }]',
'const failures = []',
'let i = -1; const max = a.length',
'while (++i < max) {',
' const e = a[i]',
def.type === 'object' || def.type === 'array'
? `const _ = v.report(e, [...p, i]); (_.length && failures.push(..._))`
: `${
def.optional ? 'e === undefined ||' : ''
}typeof e === "${def.type}" || failures.push({ ${
[
`path: [...p, i]`,
`type: "${def.type}"`,
`value: e`,
].join(', ')
} })`,
' if (failures.length > 9) return failures',
'}',
'return failures',
].join('\n')
return new Function('v, a, p = []', body)
}
const assertArray = <T extends Def['assert']>(assert: T) => (a: unknown) => {
if (!Array.isArray(a)) throw Error('type assertion failed')
a.forEach(assert)
return a as ReturnType<T>[]
}
const assertNumber = (value: unknown): number => {
if (typeof value === 'number' && !isNaN(value)) return value
throw Error(`type assertion failed`)
}
const assertString = (value: unknown): string => {
if (typeof value === 'string') return value
throw Error(`type assertion failed`)
}
const assertBoolean = (value: unknown): boolean => {
if (typeof value === 'boolean') return value
throw Error(`type assertion failed`)
}
/**
* Creates a number validator.
* @param description - An optional description of the number.
*/
export const NUM = (description?: string): DefNumber => ({
type: 'number',
assert: assertNumber,
description,
report: (value: unknown) => [{ type: 'number', value, path: [] }],
})
/**
* Creates a string validator.
* @param description - An optional description of the string.
*/
export const STR = (description?: string): DefString => ({
type: 'string',
assert: assertString,
description,
report: (value: unknown) => [{ type: 'string', value, path: [] }],
})
/**
* Creates a boolean validator.
* @param description - An optional description of the boolean.
*/
export const BOOL = (description?: string): DefBoolean => ({
type: 'boolean',
assert: assertBoolean,
description,
report: (value: unknown) => [{ type: 'boolean', value, path: [] }],
})
/**
* Makes a validator optional.
* @param def - The validator to make optional.
*/
export const optional = <T extends Def>(def: T): Optional<T> => {
const { assert, description, ...rest } = def
const optionalAssert: OptionalAssert<typeof assert> = (value: unknown) =>
value == null ? undefined : assert(value)
return {
...rest,
description,
optional: true,
assert: optionalAssert,
} as Optional<T>
}
/**
* Creates an object validator.
* @param properties - An object of validators for the object's properties.
* @param description - An optional description of the object.
*/
export const OBJ = <T extends Record<string, Def>>(
properties: T,
description?: string,
): DefObject<T> => {
const report = reportObject(properties)
const assert = assertObject(properties)
return { type: 'object', properties, report, assert, description }
}
/**
* Creates an array validator.
* @param def - The validator for the array's elements.
* @param description - An optional description of the array.
*/
export const ARR = <T extends Def>(
def: T,
description?: string,
): DefArray<T> => ({
type: 'array',
of: def,
report: reportArray(def).bind(globalThis, def),
assert: assertArray(def.assert) as DefArray<T>['assert'],
description,
})
/**
* Creates a validator for a list of predefined values.
* @param possibleValues - An array of allowed string or number values.
* @param description - An optional description of the list.
*/
export const LIST = <const T extends readonly (string | number)[]>(
possibleValues: T,
description?: string,
): DefList<T> => ({
type: 'list',
of: possibleValues,
report: (value: unknown, path: (string | number)[] = []) => {
if (possibleValues.includes(value as T[number])) return []
return [{
path,
type: 'list',
value,
expected: possibleValues,
}]
},
assert: (value: unknown): T[number] => {
if (possibleValues.includes(value as T[number])) {
return value as T[number]
}
throw Error(
`Invalid value. Expected one of: ${possibleValues.join(', ')}`,
)
},
description,
})
/**
* Creates a validator for a union of types.
* @param types - The validators to include in the union.
*/
export const UNION = <T extends readonly Def[]>(...types: T): DefUnion<T> => ({
type: 'union',
of: types,
report: (value: unknown, path: (string | number)[] = []) => {
const failures: ValidatorFailure<DefUnion<T>>[] = []
for (const type of types) {
const result = type.report(value, path)
if (result.length === 0) return []
failures.push(...result)
}
return failures
},
assert: (value: unknown): ReturnType<T[number]['assert']> => {
for (const type of types) {
try {
return type.assert(value)
} catch {
// Ignore
}
}
throw Error(
`Invalid value. Expected one of: ${types.map((t) => t.type).join(', ')}`,
)
},
})