ascertain 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/index.cjs +67 -172
- package/build/index.cjs.map +1 -1
- package/build/index.d.ts +26 -32
- package/build/index.js +61 -169
- package/build/index.js.map +1 -1
- package/package.json +8 -10
package/build/index.cjs
CHANGED
|
@@ -12,12 +12,12 @@ _export(exports, {
|
|
|
12
12
|
$keys: function() {
|
|
13
13
|
return $keys;
|
|
14
14
|
},
|
|
15
|
+
$strict: function() {
|
|
16
|
+
return $strict;
|
|
17
|
+
},
|
|
15
18
|
$values: function() {
|
|
16
19
|
return $values;
|
|
17
20
|
},
|
|
18
|
-
AssertError: function() {
|
|
19
|
-
return AssertError;
|
|
20
|
-
},
|
|
21
21
|
and: function() {
|
|
22
22
|
return and;
|
|
23
23
|
},
|
|
@@ -30,6 +30,9 @@ _export(exports, {
|
|
|
30
30
|
compile: function() {
|
|
31
31
|
return compile;
|
|
32
32
|
},
|
|
33
|
+
formatError: function() {
|
|
34
|
+
return formatError;
|
|
35
|
+
},
|
|
33
36
|
fromBase64: function() {
|
|
34
37
|
return fromBase64;
|
|
35
38
|
},
|
|
@@ -43,20 +46,7 @@ _export(exports, {
|
|
|
43
46
|
return tuple;
|
|
44
47
|
}
|
|
45
48
|
});
|
|
46
|
-
class
|
|
47
|
-
value;
|
|
48
|
-
expected;
|
|
49
|
-
path;
|
|
50
|
-
subject;
|
|
51
|
-
constructor(value, expected, path, subject = 'value'){
|
|
52
|
-
super(`Invalid ${subject} ${JSON.stringify(value)} for path ${path}, expected ${expected}.`);
|
|
53
|
-
this.value = value;
|
|
54
|
-
this.expected = expected;
|
|
55
|
-
this.path = path;
|
|
56
|
-
this.subject = subject;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
class Operation {
|
|
49
|
+
class Operator {
|
|
60
50
|
schemas;
|
|
61
51
|
constructor(schemas){
|
|
62
52
|
this.schemas = schemas;
|
|
@@ -65,13 +55,16 @@ class Operation {
|
|
|
65
55
|
}
|
|
66
56
|
}
|
|
67
57
|
}
|
|
68
|
-
|
|
58
|
+
const $keys = Symbol.for('@@keys');
|
|
59
|
+
const $values = Symbol.for('@@values');
|
|
60
|
+
const $strict = Symbol.for('@@strict');
|
|
61
|
+
class Or extends Operator {
|
|
69
62
|
}
|
|
70
63
|
const or = (...schemas)=>new Or(schemas);
|
|
71
|
-
class And extends
|
|
64
|
+
class And extends Operator {
|
|
72
65
|
}
|
|
73
66
|
const and = (...schemas)=>new And(schemas);
|
|
74
|
-
class Optional extends
|
|
67
|
+
class Optional extends Operator {
|
|
75
68
|
constructor(schema){
|
|
76
69
|
super([
|
|
77
70
|
schema
|
|
@@ -79,11 +72,9 @@ class Optional extends Operation {
|
|
|
79
72
|
}
|
|
80
73
|
}
|
|
81
74
|
const optional = (schema)=>new Optional(schema);
|
|
82
|
-
class Tuple extends
|
|
75
|
+
class Tuple extends Operator {
|
|
83
76
|
}
|
|
84
77
|
const tuple = (...schemas)=>new Tuple(schemas);
|
|
85
|
-
const $keys = Symbol.for('@@keys');
|
|
86
|
-
const $values = Symbol.for('@@values');
|
|
87
78
|
const fromBase64 = typeof Buffer === 'undefined' ? (value)=>atob(value) : (value)=>Buffer.from(value, 'base64').toString('utf-8');
|
|
88
79
|
const MULTIPLIERS = {
|
|
89
80
|
ms: 1,
|
|
@@ -130,17 +121,10 @@ const as = {
|
|
|
130
121
|
}
|
|
131
122
|
}
|
|
132
123
|
};
|
|
124
|
+
const formatError = (value, expected, path, subject)=>`Invalid ${subject} ${JSON.stringify(value)} for path ${path}, expected ${expected}.`;
|
|
133
125
|
class Context {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
registry;
|
|
137
|
-
varIndex;
|
|
138
|
-
constructor(options = {}){
|
|
139
|
-
this.options = options;
|
|
140
|
-
this.registry = [];
|
|
141
|
-
this.varIndex = 0;
|
|
142
|
-
this.Error = options.error ?? AssertError;
|
|
143
|
-
}
|
|
126
|
+
registry = [];
|
|
127
|
+
varIndex = 0;
|
|
144
128
|
register(value) {
|
|
145
129
|
if (!this.registry.includes(value)) {
|
|
146
130
|
this.registry.push(value);
|
|
@@ -155,17 +139,17 @@ const codeGen = (schema, context, valuePath, path)=>{
|
|
|
155
139
|
if (schema instanceof And) {
|
|
156
140
|
const valueAlias = context.unique('v');
|
|
157
141
|
const errorsAlias = context.unique('err');
|
|
158
|
-
const code = schema.schemas.map((s)=>`try { ${codeGen(s, context, valueAlias, path)} } catch (e) { ${errorsAlias}.push(e); }`).join('\n');
|
|
142
|
+
const code = schema.schemas.map((s)=>`try { ${codeGen(s, context, valueAlias, path)} } catch (e) { ${errorsAlias}.push(e.message); }`).join('\n');
|
|
159
143
|
return `// And
|
|
160
144
|
const ${errorsAlias} = [];
|
|
161
145
|
const ${valueAlias} = ${valuePath};
|
|
162
146
|
${code}
|
|
163
|
-
if (${errorsAlias}.length !== 0) { throw new
|
|
147
|
+
if (${errorsAlias}.length !== 0) { throw new TypeError(${errorsAlias}.join('\\n')); }
|
|
164
148
|
`;
|
|
165
149
|
} else if (schema instanceof Or) {
|
|
166
150
|
const valueAlias = context.unique('v');
|
|
167
151
|
const errorsAlias = context.unique('err');
|
|
168
|
-
const code = schema.schemas.map((s)=>codeGen(s, context, valueAlias, path)).reduceRight((result, code)=>`try {${code}} catch (e) {${errorsAlias}.push(e);${result}}`, `throw new
|
|
152
|
+
const code = schema.schemas.map((s)=>codeGen(s, context, valueAlias, path)).reduceRight((result, code)=>`try {${code}} catch (e) {${errorsAlias}.push(e.message);${result}}`, `throw new TypeError(${errorsAlias}.join('\\n'));`);
|
|
169
153
|
return `// Or
|
|
170
154
|
const ${errorsAlias} = [];
|
|
171
155
|
const ${valueAlias} = ${valuePath};
|
|
@@ -184,9 +168,12 @@ if (${valueAlias} !== undefined && ${valueAlias} !== null) { ${codeGen(schema.sc
|
|
|
184
168
|
'// Tuple',
|
|
185
169
|
`const ${valueAlias} = ${valuePath};`,
|
|
186
170
|
`const ${errorsAlias} = [];`,
|
|
187
|
-
`if (
|
|
188
|
-
|
|
189
|
-
`if (${
|
|
171
|
+
`if (${valueAlias} === null || ${valueAlias} === undefined) { throw new TypeError(\`Invalid value \${${valueAlias}} for path "${path}", expected non-nullable.\`); }`,
|
|
172
|
+
`if (typeof ${valueAlias} !== 'object') { throw new TypeError(\`Invalid type \${typeof ${valueAlias}} for path "${path}", expected an instance of Array\`); }`,
|
|
173
|
+
`if (!Array.isArray(${valueAlias})) { throw new TypeError(\`Invalid instance of \${${valueAlias}.constructor?.name} for path "${path}", expected an instance of Array.\`); }`,
|
|
174
|
+
`if (${valueAlias}.length > ${schema.schemas.length}) { throw new TypeError(\`Invalid tuple length \${${valueAlias}.length} for path "${path}", expected ${schema.schemas.length}.\`); }`,
|
|
175
|
+
...schema.schemas.map((s, idx)=>`try { ${codeGen(s, context, `${valueAlias}[${idx}]`, `${path}[${idx}]`)} } catch (e) { ${errorsAlias}.push(e.message); }`),
|
|
176
|
+
`if (${errorsAlias}.length !== 0) { throw new TypeError(${errorsAlias}.join('\\n')); }`
|
|
190
177
|
];
|
|
191
178
|
return code.join('\n');
|
|
192
179
|
} else if (typeof schema === 'function') {
|
|
@@ -196,23 +183,25 @@ if (${valueAlias} !== undefined && ${valueAlias} !== null) { ${codeGen(schema.sc
|
|
|
196
183
|
return `
|
|
197
184
|
const ${valueAlias} = ${valuePath};
|
|
198
185
|
const ${registryAlias} = ctx.registry[${index}];
|
|
199
|
-
if (${valueAlias} === null || ${valueAlias} === undefined) { throw new
|
|
200
|
-
if (typeof ${valueAlias} === 'object' && !(${valueAlias} instanceof ${registryAlias})) { throw new
|
|
201
|
-
if (typeof ${valueAlias} !== 'object' && ${valueAlias}?.constructor !== ${registryAlias}) { throw new
|
|
186
|
+
if (${valueAlias} === null || ${valueAlias} === undefined) { throw new TypeError(\`Invalid value \${${valueAlias}} for path "${path}", expected non-nullable.\`); }
|
|
187
|
+
if (typeof ${valueAlias} === 'object' && !(${valueAlias} instanceof ${registryAlias})) { throw new TypeError(\`Invalid instance of \${${valueAlias}?.constructor?.name} for path "${path}", expected an instance of ${schema?.name}\`); }
|
|
188
|
+
if (typeof ${valueAlias} !== 'object' && ${valueAlias}?.constructor !== ${registryAlias}) { throw new TypeError(\`Invalid type \${${valueAlias}?.constructor?.name} for path "${path}", expected type ${schema?.name}\`); }
|
|
202
189
|
`;
|
|
203
190
|
} else if (Array.isArray(schema)) {
|
|
204
191
|
const valueAlias = context.unique('v');
|
|
205
192
|
const code = [
|
|
206
193
|
`const ${valueAlias} = ${valuePath};`,
|
|
207
|
-
`if (
|
|
194
|
+
`if (${valueAlias} === null || ${valueAlias} === undefined) { throw new TypeError(\`Invalid value \${${valueAlias}} for path "${path}", expected non-nullable.\`); }`,
|
|
195
|
+
`if (typeof ${valueAlias} !== 'object') { throw new TypeError(\`Invalid type \${typeof ${valueAlias}} for path "${path}", expected an instance of Array.\`); }`,
|
|
196
|
+
`if (!Array.isArray(${valueAlias})) { throw new TypeError(\`Invalid instance of \${${valueAlias}.constructor?.name} for path "${path}", expected an instance of Array.\`); }`
|
|
208
197
|
];
|
|
209
198
|
if (schema.length > 0) {
|
|
210
199
|
const value = context.unique('val');
|
|
211
200
|
const key = context.unique('key');
|
|
212
201
|
const errorsAlias = context.unique('err');
|
|
213
202
|
code.push(`const ${errorsAlias} = [];`);
|
|
214
|
-
code.push(...schema.map((s)=>`${valueAlias}.forEach((${value},${key}) => { try { ${codeGen(s, context, value, `${path}[\${${key}}]`)} } catch(e){ ${errorsAlias}.push(e); } });`));
|
|
215
|
-
code.push(`if (${errorsAlias}.length !== 0) { throw new
|
|
203
|
+
code.push(...schema.map((s)=>`${valueAlias}.forEach((${value},${key}) => { try { ${codeGen(s, context, value, `${path}[\${${key}}]`)} } catch(e){ ${errorsAlias}.push(e.message); } });`));
|
|
204
|
+
code.push(`if (${errorsAlias}.length !== 0) { throw new TypeError(${errorsAlias}.join('\\n')); }`);
|
|
216
205
|
}
|
|
217
206
|
return code.join('\n');
|
|
218
207
|
} else if (typeof schema === 'object' && schema !== null) {
|
|
@@ -220,38 +209,46 @@ if (typeof ${valueAlias} !== 'object' && ${valueAlias}?.constructor !== ${regist
|
|
|
220
209
|
const valueAlias = context.unique('v');
|
|
221
210
|
return `
|
|
222
211
|
const ${valueAlias} = ${valuePath};
|
|
223
|
-
if (!${schema.toString()}.test('' + ${valueAlias})) { throw new
|
|
212
|
+
if (!${schema.toString()}.test('' + ${valueAlias})) { throw new TypeError(\`Invalid value \${${valueAlias}} for path "${path}", expected to match ${schema.toString()}\`); }
|
|
224
213
|
`;
|
|
225
214
|
} else {
|
|
226
215
|
const valueAlias = context.unique('v');
|
|
227
216
|
const code = [
|
|
228
217
|
`const ${valueAlias} = ${valuePath};`,
|
|
229
|
-
`if (${valueAlias} === null || ${valueAlias} === undefined) { throw new
|
|
230
|
-
`if (typeof ${valueAlias} !== 'object') { throw new
|
|
218
|
+
`if (${valueAlias} === null || ${valueAlias} === undefined) { throw new TypeError(\`Invalid value \${${valueAlias}} for path "${path}", expected non-nullable.\`); }`,
|
|
219
|
+
`if (typeof ${valueAlias} !== 'object') { throw new TypeError(\`Invalid type \${typeof ${valueAlias}} for path "${path}", expected type is object.\`); }`
|
|
231
220
|
];
|
|
232
221
|
if ($keys in schema) {
|
|
233
|
-
const keysAlias = context.unique('
|
|
222
|
+
const keysAlias = context.unique('k');
|
|
234
223
|
const errorsAlias = context.unique('err');
|
|
235
|
-
const
|
|
224
|
+
const kAlias = context.unique('k');
|
|
236
225
|
code.push(`
|
|
237
226
|
const ${keysAlias} = Object.keys(${valueAlias});
|
|
238
|
-
const ${errorsAlias} = ${keysAlias}.
|
|
239
|
-
if (${errorsAlias}.length !== 0) { throw new
|
|
227
|
+
const ${errorsAlias} = ${keysAlias}.map(${kAlias} => { try { ${codeGen(schema[$keys], context, kAlias, `${path}[\${${kAlias}}]`)} } catch (e) { return e.message; } }).filter(Boolean);
|
|
228
|
+
if (${errorsAlias}.length !== 0) { throw new TypeError(${errorsAlias}.join('\\n')); }
|
|
240
229
|
`);
|
|
241
230
|
}
|
|
242
231
|
if ($values in schema) {
|
|
243
232
|
const vAlias = context.unique('val');
|
|
244
|
-
const
|
|
233
|
+
const kAlias = context.unique('k');
|
|
234
|
+
const entriesAlias = context.unique('en');
|
|
245
235
|
const errorsAlias = context.unique('err');
|
|
246
|
-
code.push(`
|
|
247
|
-
const ${
|
|
248
|
-
const ${errorsAlias} = ${
|
|
249
|
-
if (${errorsAlias}.length !== 0) { throw new
|
|
250
|
-
|
|
236
|
+
code.push(`
|
|
237
|
+
const ${entriesAlias} = Object.entries(${valueAlias});
|
|
238
|
+
const ${errorsAlias} = ${entriesAlias}.map(([${kAlias},${vAlias}]) => { try { ${codeGen(schema[$values], context, vAlias, `${path}[\${${kAlias}}]`)} } catch (e) { return e.message; } }).filter(Boolean);
|
|
239
|
+
if (${errorsAlias}.length !== 0) { throw new TypeError(${errorsAlias}.join('\\n')); }
|
|
240
|
+
`);
|
|
241
|
+
}
|
|
242
|
+
if ($strict in schema && schema[$strict]) {
|
|
243
|
+
const keysAlias = context.unique('k');
|
|
244
|
+
const kAlias = context.unique('k');
|
|
245
|
+
const extraAlias = context.unique('ex');
|
|
246
|
+
code.push(`const ${keysAlias} = new Set(${JSON.stringify(Object.keys(schema))});`);
|
|
247
|
+
code.push(`const ${extraAlias} = Object.keys(${valueAlias}).filter(${kAlias} => !${keysAlias}.has(${kAlias}));`);
|
|
248
|
+
code.push(`if (${extraAlias}.length !== 0) { throw new TypeError(\`Extra properties: \${${extraAlias}}, are not allowed for path "${path}"\`); }`);
|
|
251
249
|
}
|
|
252
|
-
|
|
253
|
-
code.
|
|
254
|
-
return `{${code.join('\n')}}`;
|
|
250
|
+
code.push(...Object.entries(schema).map(([key, s])=>codeGen(s, context, `${valueAlias}['${key}']`, `${path}.${key}`)));
|
|
251
|
+
return `${code.join('\n')}`;
|
|
255
252
|
}
|
|
256
253
|
} else if (typeof schema === 'symbol') {
|
|
257
254
|
const index = context.register(schema);
|
|
@@ -260,136 +257,34 @@ if (${errorsAlias}.length !== 0) { throw new AggregateError(${errorsAlias}, 'Inv
|
|
|
260
257
|
return `
|
|
261
258
|
const ${valueAlias} = ${valuePath};
|
|
262
259
|
const ${registryAlias} = ctx.registry[${index}];
|
|
263
|
-
if (typeof ${valueAlias} !== 'symbol') { throw new
|
|
264
|
-
if (${valueAlias} !== ${registryAlias}) { throw new
|
|
260
|
+
if (typeof ${valueAlias} !== 'symbol') { throw new TypeError(\`Invalid type \${typeof ${valueAlias}} for "path", expected symbol\`); }
|
|
261
|
+
if (${valueAlias} !== ${registryAlias}) { throw new TypeError(\`Invalid value \${${valueAlias}.toString()} for path "${path}", expected ${schema.toString()}\`); }
|
|
265
262
|
`;
|
|
266
263
|
} else if (schema === null || schema === undefined) {
|
|
267
264
|
const valueAlias = context.unique('v');
|
|
268
265
|
return `
|
|
269
266
|
const ${valueAlias} = ${valuePath};
|
|
270
|
-
if (${valueAlias} !== null && ${valueAlias} !== undefined ) { throw new
|
|
267
|
+
if (${valueAlias} !== null && ${valueAlias} !== undefined ) { throw new TypeError(\`Invalid value ${valueAlias} for path "${path}", expected nullable\`); }
|
|
271
268
|
`;
|
|
272
269
|
} else {
|
|
273
270
|
const valueAlias = context.unique('v');
|
|
274
|
-
const typeAlias = context.unique('t');
|
|
275
271
|
const value = context.unique('val');
|
|
276
272
|
return `
|
|
277
273
|
const ${valueAlias} = ${valuePath};
|
|
278
|
-
const ${typeAlias} = '${typeof schema}';
|
|
279
274
|
const ${value} = ${JSON.stringify(schema)};
|
|
280
|
-
if (typeof ${valueAlias} !== ${
|
|
281
|
-
if (${valueAlias} !== ${value}) { throw new
|
|
275
|
+
if (typeof ${valueAlias} !== '${typeof schema}') { throw new TypeError(\`Invalid type \${typeof ${valueAlias}} for path "${path}", expected ${typeof schema}\`); }
|
|
276
|
+
if (${valueAlias} !== ${value}) { throw new TypeError(\`Invalid value ${JSON.stringify(valueAlias)} for path "${path}", expected ${JSON.stringify(schema)}\`); }
|
|
282
277
|
`;
|
|
283
278
|
}
|
|
284
279
|
};
|
|
285
|
-
const
|
|
286
|
-
|
|
287
|
-
};
|
|
288
|
-
const compile = (schema, rootName, options = {})=>{
|
|
289
|
-
const context = new Context(options);
|
|
280
|
+
const compile = (schema, rootName)=>{
|
|
281
|
+
const context = new Context();
|
|
290
282
|
const code = codeGen(schema, context, 'data', rootName);
|
|
291
283
|
const validator = new Function('ctx', 'data', code);
|
|
292
|
-
return (data)=>
|
|
293
|
-
try {
|
|
294
|
-
validator(context, data);
|
|
295
|
-
} catch (e) {
|
|
296
|
-
const errors = e instanceof AggregateError ? flatAggregateError(e) : [
|
|
297
|
-
e
|
|
298
|
-
];
|
|
299
|
-
throw new AggregateError(errors, 'Validation failure');
|
|
300
|
-
}
|
|
301
|
-
};
|
|
302
|
-
};
|
|
303
|
-
const assert = (target, schema, path)=>{
|
|
304
|
-
if (schema instanceof And) {
|
|
305
|
-
return schema.schemas.flatMap((schema)=>assert(target, schema, path)).filter((error)=>!!error);
|
|
306
|
-
} else if (schema instanceof Or) {
|
|
307
|
-
const errors = schema.schemas.flatMap((schema)=>assert(target, schema, path));
|
|
308
|
-
const filteredErrors = errors.filter((error)=>!!error);
|
|
309
|
-
if (filteredErrors.length === schema.schemas.length) {
|
|
310
|
-
return filteredErrors;
|
|
311
|
-
}
|
|
312
|
-
} else if (schema instanceof Optional) {
|
|
313
|
-
if (target !== undefined && target !== null) {
|
|
314
|
-
return assert(target, schema.schemas[0], path);
|
|
315
|
-
}
|
|
316
|
-
} else if (schema instanceof Tuple) {
|
|
317
|
-
if (!Array.isArray(target)) {
|
|
318
|
-
return [
|
|
319
|
-
new AssertError(target, 'array', path)
|
|
320
|
-
];
|
|
321
|
-
}
|
|
322
|
-
return schema.schemas.flatMap((s, idx)=>assert(target[idx], s, `${path}[${idx}]`)).filter((error)=>!!error);
|
|
323
|
-
} else if (typeof schema === 'function') {
|
|
324
|
-
if (target === null || target === undefined) {
|
|
325
|
-
return [
|
|
326
|
-
new AssertError(target, 'a non-nullable', path)
|
|
327
|
-
];
|
|
328
|
-
}
|
|
329
|
-
if (typeof target === 'object' && !(target instanceof schema)) {
|
|
330
|
-
return [
|
|
331
|
-
new AssertError(target?.constructor?.name, `instance of ${schema.name}`, path, 'instance of')
|
|
332
|
-
];
|
|
333
|
-
}
|
|
334
|
-
if (typeof target !== 'object' && target?.constructor !== schema) {
|
|
335
|
-
return [
|
|
336
|
-
new AssertError(target?.constructor?.name, schema.name, path, 'type')
|
|
337
|
-
];
|
|
338
|
-
}
|
|
339
|
-
} else if (Array.isArray(schema)) {
|
|
340
|
-
if (!Array.isArray(target)) {
|
|
341
|
-
return [
|
|
342
|
-
new AssertError(target, 'array', path)
|
|
343
|
-
];
|
|
344
|
-
}
|
|
345
|
-
return schema.flatMap((s)=>target.flatMap((value, idx)=>assert(value, s, `${path}[${idx}]`))).filter((error)=>!!error);
|
|
346
|
-
} else if (typeof schema === 'object' && schema !== null) {
|
|
347
|
-
if (schema instanceof RegExp) {
|
|
348
|
-
if (!schema.test('' + target)) {
|
|
349
|
-
return [
|
|
350
|
-
new AssertError(target, `matching ${schema.toString()}`, path)
|
|
351
|
-
];
|
|
352
|
-
}
|
|
353
|
-
return [];
|
|
354
|
-
} else {
|
|
355
|
-
if (target === null || target === undefined) {
|
|
356
|
-
return [
|
|
357
|
-
new AssertError(target, 'object', path)
|
|
358
|
-
];
|
|
359
|
-
}
|
|
360
|
-
if (typeof target !== 'object') {
|
|
361
|
-
return [
|
|
362
|
-
new AssertError(target, schema.constructor.name, path)
|
|
363
|
-
];
|
|
364
|
-
}
|
|
365
|
-
if ($keys in schema) {
|
|
366
|
-
const targetKeys = Object.keys(target);
|
|
367
|
-
return targetKeys.flatMap((key)=>assert(key, schema[$keys], path)).filter((error)=>!!error);
|
|
368
|
-
}
|
|
369
|
-
if ($values in schema) {
|
|
370
|
-
const targetKeys = Object.keys(target);
|
|
371
|
-
return targetKeys.flatMap((key)=>assert(target[key], schema[$values], path)).filter((error)=>!!error);
|
|
372
|
-
}
|
|
373
|
-
return Object.keys(schema).flatMap((key)=>assert(target[key], schema[key], path)).filter((error)=>!!error);
|
|
374
|
-
}
|
|
375
|
-
} else if (schema === null || schema === undefined) {
|
|
376
|
-
if (target !== null && target !== undefined) {
|
|
377
|
-
return [
|
|
378
|
-
new AssertError(target, 'nullable', path)
|
|
379
|
-
];
|
|
380
|
-
}
|
|
381
|
-
} else if (target !== schema) {
|
|
382
|
-
return [
|
|
383
|
-
new AssertError(target, schema, path)
|
|
384
|
-
];
|
|
385
|
-
}
|
|
386
|
-
return [];
|
|
284
|
+
return (data)=>validator(context, data);
|
|
387
285
|
};
|
|
388
286
|
const ascertain = (schema, data, rootName = '[root]')=>{
|
|
389
|
-
|
|
390
|
-
if (result.length > 0) {
|
|
391
|
-
throw new AggregateError(result, 'Validation failure');
|
|
392
|
-
}
|
|
287
|
+
compile(schema, rootName)(data);
|
|
393
288
|
};
|
|
394
289
|
|
|
395
290
|
//# sourceMappingURL=index.cjs.map
|
package/build/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export class AssertError extends TypeError {\n constructor(\n public readonly value: unknown,\n public readonly expected: unknown,\n public readonly path: string,\n public readonly subject = 'value',\n ) {\n super(`Invalid ${subject} ${JSON.stringify(value)} for path ${path}, expected ${expected}.`);\n }\n}\n\ntype Keys = string | number | symbol;\ntype DataValue = any;\nexport type Data = Record<Keys, unknown>;\ntype DataArray = DataValue[];\n\nexport type Schema<T> = T extends Data ? { [S in keyof T]?: Schema<T[S]> } : T extends DataArray ? [Schema<T[number]>] : DataValue;\n\nexport type SchemaData<S> =\n S extends Record<Keys, unknown>\n ? { [K in keyof S as Exclude<K, typeof $keys | typeof $values>]?: SchemaData<S[K]> } & Record<string, unknown>\n : S extends unknown[]\n ? unknown[]\n : unknown;\n\nabstract class Operation<T extends Data = DataValue> {\n constructor(public readonly schemas: Schema<T>[]) {\n if (schemas.length === 0) {\n throw new TypeError(`Operation schema ${this.constructor.name} must have at least one element`);\n }\n }\n}\n\nclass Or<T extends Data = DataValue> extends Operation<T> {}\nexport const or = <T extends Data = DataValue>(...schemas: Schema<T>[]) => new Or(schemas);\n\nclass And<T extends Data = DataValue> extends Operation<T> {}\nexport const and = <T extends Data = DataValue>(...schemas: Schema<T>[]) => new And(schemas);\n\nclass Optional<T extends Data = DataValue> extends Operation<T> {\n constructor(schema: Schema<T>) {\n super([schema]);\n }\n}\nexport const optional = <T extends Data = DataValue>(schema: Schema<T>) => new Optional(schema);\n\nclass Tuple<T extends Data = DataValue> extends Operation<T> {}\nexport const tuple = <T extends Data = DataValue>(...schemas: Schema<T>[]) => new Tuple(schemas);\n\nexport const $keys = Symbol.for('@@keys');\nexport const $values = Symbol.for('@@values');\nexport const fromBase64 = typeof Buffer === 'undefined' ? (value: string) => atob(value) : (value: string) => Buffer.from(value, 'base64').toString('utf-8');\n\nconst MULTIPLIERS = {\n ms: 1,\n s: 1000,\n m: 60000,\n h: 3600000,\n d: 86400000,\n w: 604800000,\n};\n\nexport const as = {\n string: (value: string | undefined) => {\n return typeof value === 'string' ? value : undefined;\n },\n number: (value: string | undefined) => {\n const result = parseFloat(value as string);\n return Number.isFinite(result) ? result : undefined;\n },\n date: (value: string | undefined) => {\n const result = Date.parse(value as string);\n return Number.isFinite(result) ? new Date(result) : undefined;\n },\n time: (value: string | undefined) => {\n const matches = value?.match(/^(\\d+)(ms|s|m|h|d|w)?$/);\n if (matches) {\n const [, amount, unit = 'ms'] = matches;\n return parseInt(amount, 10) * MULTIPLIERS[unit as keyof typeof MULTIPLIERS];\n }\n return undefined;\n },\n boolean: (value: string | undefined) =>\n /^(0|1|true|false|enabled|disabled)$/i.test(value as string) ? /^(1|true|enabled)$/i.test(value as string) : undefined,\n array: (value: string | undefined, delimiter: string) => value?.split?.(delimiter) ?? undefined,\n json: (value: string | undefined) => {\n try {\n return JSON.parse(value as string);\n } catch (e) {\n return undefined;\n }\n },\n base64: (value: string | undefined) => {\n try {\n return fromBase64(value as string);\n } catch (e) {\n return undefined;\n }\n },\n};\n\nclass Context {\n public readonly Error: typeof AssertError;\n public readonly registry: unknown[] = [];\n private varIndex = 0;\n\n constructor(public readonly options: CompilerOptions = {}) {\n this.Error = options.error ?? AssertError;\n }\n\n register(value: unknown): number {\n if (!this.registry.includes(value)) {\n this.registry.push(value);\n }\n return this.registry.indexOf(value);\n }\n\n unique(prefix: string) {\n return `${prefix}$$${this.varIndex++}`;\n }\n}\n\nexport interface CompilerOptions {\n error?: typeof AssertError;\n}\n\nconst codeGen = <T extends Data = DataValue>(schema: Schema<T>, context: Context, valuePath: string, path: string): string => {\n if (schema instanceof And) {\n const valueAlias = context.unique('v');\n const errorsAlias = context.unique('err');\n const code = schema.schemas.map((s) => `try { ${codeGen(s, context, valueAlias, path)} } catch (e) { ${errorsAlias}.push(e); }`).join('\\n');\n return `// And\n const ${errorsAlias} = [];\n const ${valueAlias} = ${valuePath};\n ${code}\n if (${errorsAlias}.length !== 0) { throw new AggregateError(${errorsAlias}, 'Invalid value for path \"${path}\"'); }\n`;\n } else if (schema instanceof Or) {\n const valueAlias = context.unique('v');\n const errorsAlias = context.unique('err');\n const code = schema.schemas\n .map((s) => codeGen(s, context, valueAlias, path))\n .reduceRight(\n (result, code) => `try {${code}} catch (e) {${errorsAlias}.push(e);${result}}`,\n `throw new AggregateError(${errorsAlias}, 'Invalid value for path \"${path}\"');`,\n );\n return `// Or\nconst ${errorsAlias} = [];\nconst ${valueAlias} = ${valuePath};\n${code}\n `;\n } else if (schema instanceof Optional) {\n const valueAlias = context.unique('v');\n return `// Optional\nconst ${valueAlias} = ${valuePath};\nif (${valueAlias} !== undefined && ${valueAlias} !== null) { ${codeGen(schema.schemas[0], context, valueAlias, path)} }\n`;\n } else if (schema instanceof Tuple) {\n const valueAlias = context.unique('v');\n const errorsAlias = context.unique('err');\n const code: string[] = [\n '// Tuple',\n `const ${valueAlias} = ${valuePath};`,\n `const ${errorsAlias} = [];`,\n `if (!Array.isArray(${valueAlias})) { throw new ctx.Error(${valueAlias}, 'array', \\`${path}\\`); }`,\n ...schema.schemas.map((s, idx) => `try { ${codeGen(s, context, `${valueAlias}[${idx}]`, `${path}[${idx}]`)} } catch (e) { ${errorsAlias}.push(e); }`),\n `if (${errorsAlias}.length !== 0) { throw new AggregateError(${errorsAlias}, 'Invalid value for path \"${path}\"'); }`,\n ];\n\n return code.join('\\n');\n } else if (typeof schema === 'function') {\n const index = context.register(schema);\n const valueAlias = context.unique('v');\n const registryAlias = context.unique('r');\n return `\nconst ${valueAlias} = ${valuePath};\nconst ${registryAlias} = ctx.registry[${index}];\nif (${valueAlias} === null || ${valueAlias} === undefined) { throw new ctx.Error(${valueAlias}, 'a non-nullable', \\`${path}\\`); }\nif (typeof ${valueAlias} === 'object' && !(${valueAlias} instanceof ${registryAlias})) { throw new ctx.Error(${valueAlias}?.constructor?.name, \\`instance of \\${${registryAlias}.name}\\`, \\`${path}\\`, 'instance of'); }\nif (typeof ${valueAlias} !== 'object' && ${valueAlias}?.constructor !== ${registryAlias}) { throw new ctx.Error(${valueAlias}?.constructor?.name, ${registryAlias}.name, \\`${path}\\`, 'type'); }\n`;\n } else if (Array.isArray(schema)) {\n const valueAlias = context.unique('v');\n const code: string[] = [\n `const ${valueAlias} = ${valuePath};`,\n `if (!Array.isArray(${valueAlias})) { throw new ctx.Error(${valueAlias}, 'array', \\`${path}\\`); }`,\n ];\n if (schema.length > 0) {\n const value = context.unique('val');\n const key = context.unique('key');\n const errorsAlias = context.unique('err');\n code.push(`const ${errorsAlias} = [];`);\n code.push(\n ...schema.map(\n (s) =>\n `${valueAlias}.forEach((${value},${key}) => { try { ${codeGen(s, context, value, `${path}[\\${${key}}]`)} } catch(e){ ${errorsAlias}.push(e); } });`,\n ),\n );\n\n code.push(`if (${errorsAlias}.length !== 0) { throw new AggregateError(${errorsAlias}, 'Invalid value for path \"${path}\"'); }`);\n }\n return code.join('\\n');\n } else if (typeof schema === 'object' && schema !== null) {\n if (schema instanceof RegExp) {\n const valueAlias = context.unique('v');\n return `\nconst ${valueAlias} = ${valuePath};\nif (!${schema.toString()}.test('' + ${valueAlias})) { throw new ctx.Error(${valueAlias}, 'matching ${schema.toString()}', \\`${path}\\`); }\n`;\n } else {\n const valueAlias = context.unique('v');\n const code: string[] = [\n `const ${valueAlias} = ${valuePath};`,\n `if (${valueAlias} === null || ${valueAlias} === undefined) { throw new ctx.Error(${valueAlias}, 'object', \\`${path}\\`); }`,\n `if (typeof ${valueAlias} !== 'object') { throw new ctx.Error(${valueAlias}, '${schema.constructor.name}', \\`${path}\\`); }`,\n ];\n if ($keys in schema) {\n const keysAlias = context.unique('key');\n const errorsAlias = context.unique('err');\n const value = context.unique('v');\n code.push(`\nconst ${keysAlias} = Object.keys(${valueAlias});\nconst ${errorsAlias} = ${keysAlias}.flatMap((${value}) => { ${codeGen(schema[$keys], context, value, path)} }).filter(Boolean);\nif (${errorsAlias}.length !== 0) { throw new AggregateError(${errorsAlias}, 'Invalid value for path \"${path}\"'); }\n`);\n }\n if ($values in schema) {\n const vAlias = context.unique('val');\n const valuesAlias = context.unique('vals');\n const errorsAlias = context.unique('err');\n code.push(`{\nconst ${valuesAlias} = Object.values(${valuePath});\nconst ${errorsAlias} = ${valuesAlias}.flatMap((${vAlias}) => { ${codeGen(schema[$values], context, vAlias, path)} }).filter(Boolean);\nif (${errorsAlias}.length !== 0) { throw new AggregateError(${errorsAlias}, 'Invalid value for path \"${path}\"'); }\n}`);\n }\n const keys = Object.keys(schema);\n code.push(...keys.map((key) => codeGen(schema[key], context, `${valueAlias}['${key}']`, `${path}.${key}`)));\n return `{${code.join('\\n')}}`;\n }\n } else if (typeof schema === 'symbol') {\n const index = context.register(schema);\n const valueAlias = context.unique('v');\n const registryAlias = context.unique('r');\n\n return `\nconst ${valueAlias} = ${valuePath};\nconst ${registryAlias} = ctx.registry[${index}];\nif (typeof ${valueAlias} !== 'symbol') { throw new ctx.Error(typeof ${valueAlias}, 'symbol', '${path}', 'type of'); }\nif (${valueAlias} !== ${registryAlias}) { throw new ctx.Error(${valueAlias}.toString(), ${registryAlias}.toString(), '${path}', 'symbol'); }\n `;\n } else if (schema === null || schema === undefined) {\n const valueAlias = context.unique('v');\n return `\nconst ${valueAlias} = ${valuePath};\nif (${valueAlias} !== null && ${valueAlias} !== undefined ) { throw new ctx.Error(${valueAlias}, 'nullable', '${path}'); }\n `;\n } else {\n const valueAlias = context.unique('v');\n const typeAlias = context.unique('t');\n const value = context.unique('val');\n return `\nconst ${valueAlias} = ${valuePath};\nconst ${typeAlias} = '${typeof schema}';\nconst ${value} = ${JSON.stringify(schema)};\nif (typeof ${valueAlias} !== ${typeAlias}) { throw new ctx.Error(typeof ${valueAlias}, ${typeAlias}, '${path}', 'type of'); }\nif (${valueAlias} !== ${value}) { throw new ctx.Error(${valueAlias}, ${value}, '${path}'); }\n`;\n }\n};\n\nconst flatAggregateError = (error: AggregateError): AssertError[] => {\n return error.errors.flatMap((e) => (e instanceof AggregateError ? flatAggregateError(e) : e));\n};\n\nexport const compile = <S>(schema: S, rootName: string, options: CompilerOptions = {}) => {\n const context = new Context(options);\n const code = codeGen(schema, context, 'data', rootName);\n const validator = new Function('ctx', 'data', code);\n return (data: SchemaData<S>) => {\n try {\n validator(context, data);\n } catch (e) {\n const errors = e instanceof AggregateError ? flatAggregateError(e) : [e];\n throw new AggregateError(errors, 'Validation failure');\n }\n };\n};\n\nconst assert = (target: unknown, schema: unknown, path: string): AssertError[] => {\n if (schema instanceof And) {\n return schema.schemas.flatMap((schema) => assert(target, schema, path)).filter((error) => !!error);\n } else if (schema instanceof Or) {\n const errors = schema.schemas.flatMap((schema) => assert(target, schema, path));\n const filteredErrors = errors.filter((error) => !!error);\n if (filteredErrors.length === schema.schemas.length) {\n return filteredErrors;\n }\n } else if (schema instanceof Optional) {\n if (target !== undefined && target !== null) {\n return assert(target, schema.schemas[0], path);\n }\n } else if (schema instanceof Tuple) {\n if (!Array.isArray(target)) {\n return [new AssertError(target, 'array', path)];\n }\n return schema.schemas.flatMap((s, idx) => assert(target[idx], s, `${path}[${idx}]`)).filter((error) => !!error);\n } else if (typeof schema === 'function') {\n if (target === null || target === undefined) {\n return [new AssertError(target, 'a non-nullable', path)];\n }\n if (typeof target === 'object' && !(target instanceof schema)) {\n return [new AssertError(target?.constructor?.name, `instance of ${schema.name}`, path, 'instance of')];\n }\n if (typeof target !== 'object' && target?.constructor !== schema) {\n return [new AssertError(target?.constructor?.name, schema.name, path, 'type')];\n }\n } else if (Array.isArray(schema)) {\n if (!Array.isArray(target)) {\n return [new AssertError(target, 'array', path)];\n }\n return schema.flatMap((s) => target.flatMap((value, idx) => assert(value, s, `${path}[${idx}]`))).filter((error) => !!error);\n } else if (typeof schema === 'object' && schema !== null) {\n if (schema instanceof RegExp) {\n if (!schema.test('' + target)) {\n return [new AssertError(target, `matching ${schema.toString()}`, path)];\n }\n return [];\n } else {\n if (target === null || target === undefined) {\n return [new AssertError(target, 'object', path)];\n }\n if (typeof target !== 'object') {\n return [new AssertError(target, schema.constructor.name, path)];\n }\n if ($keys in schema) {\n const targetKeys = Object.keys(target);\n return targetKeys.flatMap((key) => assert(key, schema[$keys], path)).filter((error) => !!error);\n }\n if ($values in schema) {\n const targetKeys = Object.keys(target);\n return targetKeys.flatMap((key) => assert(target[key as keyof typeof target], schema[$values], path)).filter((error) => !!error);\n }\n return Object.keys(schema)\n .flatMap((key) => assert(target[key as keyof typeof target], schema[key as keyof typeof target], path))\n .filter((error) => !!error);\n }\n } else if (schema === null || schema === undefined) {\n if (target !== null && target !== undefined) {\n return [new AssertError(target, 'nullable', path)];\n }\n } else if (target !== schema) {\n return [new AssertError(target, schema, path)];\n }\n return [];\n};\n\nexport const ascertain = <T extends Data = DataValue>(schema: Schema<T>, data: T, rootName = '[root]') => {\n const result = assert(data, schema, rootName).filter((error) => !!error);\n if (result.length > 0) {\n throw new AggregateError(result, 'Validation failure');\n }\n};\n"],"names":["$keys","$values","AssertError","and","as","ascertain","compile","fromBase64","optional","or","tuple","TypeError","constructor","value","expected","path","subject","JSON","stringify","Operation","schemas","length","name","Or","And","Optional","schema","Tuple","Symbol","for","Buffer","atob","from","toString","MULTIPLIERS","ms","s","m","h","d","w","string","undefined","number","result","parseFloat","Number","isFinite","date","Date","parse","time","matches","match","amount","unit","parseInt","boolean","test","array","delimiter","split","json","e","base64","Context","Error","registry","varIndex","options","error","register","includes","push","indexOf","unique","prefix","codeGen","context","valuePath","valueAlias","errorsAlias","code","map","join","reduceRight","idx","index","registryAlias","Array","isArray","key","RegExp","keysAlias","vAlias","valuesAlias","keys","Object","typeAlias","flatAggregateError","errors","flatMap","AggregateError","rootName","validator","Function","data","assert","target","filter","filteredErrors","targetKeys"],"mappings":";;;;;;;;;;;IAiDaA,KAAK;eAALA;;IACAC,OAAO;eAAPA;;IAlDAC,WAAW;eAAXA;;IAqCAC,GAAG;eAAHA;;IAyBAC,EAAE;eAAFA;;IAuSAC,SAAS;eAATA;;IAlFAC,OAAO;eAAPA;;IAhOAC,UAAU;eAAVA;;IAPAC,QAAQ;eAARA;;IAVAC,EAAE;eAAFA;;IAaAC,KAAK;eAALA;;;AA/CN,MAAMR,oBAAoBS;;;;;IAC/BC,YACE,AAAgBC,KAAc,EAC9B,AAAgBC,QAAiB,EACjC,AAAgBC,IAAY,EAC5B,AAAgBC,UAAU,OAAO,CACjC;QACA,KAAK,CAAC,CAAC,QAAQ,EAAEA,QAAQ,CAAC,EAAEC,KAAKC,SAAS,CAACL,OAAO,UAAU,EAAEE,KAAK,WAAW,EAAED,SAAS,CAAC,CAAC;aAL3ED,QAAAA;aACAC,WAAAA;aACAC,OAAAA;aACAC,UAAAA;IAGlB;AACF;AAgBA,MAAeG;;IACbP,YAAY,AAAgBQ,OAAoB,CAAE;aAAtBA,UAAAA;QAC1B,IAAIA,QAAQC,MAAM,KAAK,GAAG;YACxB,MAAM,IAAIV,UAAU,CAAC,iBAAiB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EAAE5E,OAChG8F,MAAM,CAAC,CAACvC,QAAU,CAAC,CAACA;QACzB;IACF,OAAO,IAAI5C,WAAW,QAAQA,WAAWgB,WAAW;QAClD,IAAIkE,WAAW,QAAQA,WAAWlE,WAAW;YAC3C,OAAO;gBAAC,IAAIxC,YAAY0G,QAAQ,YAAY7F;aAAM;QACpD;IACF,OAAO,IAAI6F,WAAWlF,QAAQ;QAC5B,OAAO;YAAC,IAAIxB,YAAY0G,QAAQlF,QAAQX;SAAM;IAChD;IACA,OAAO,EAAE;AACX;AAEO,MAAMV,YAAY,CAA6BqB,QAAmBgF,MAASH,WAAW,QAAQ;IACnG,MAAM3D,SAAS+D,OAAOD,MAAMhF,QAAQ6E,UAAUM,MAAM,CAAC,CAACvC,QAAU,CAAC,CAACA;IAClE,IAAI1B,OAAOvB,MAAM,GAAG,GAAG;QACrB,MAAM,IAAIiF,eAAe1D,QAAQ;IACnC;AACF"}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["abstract class Operator<T> {\n constructor(public readonly schemas: Schema<T>[]) {\n if (schemas.length === 0) {\n throw new TypeError(`Operation schema ${this.constructor.name} must have at least one element`);\n }\n }\n}\n\nexport const $keys = Symbol.for('@@keys');\nexport const $values = Symbol.for('@@values');\nexport const $strict = Symbol.for('@@strict');\n\nexport type Schema<T> =\n T extends Record<string | number | symbol, unknown>\n ? { [K in keyof T]?: Schema<T[K]> | unknown } & { [$keys]?: Schema<keyof T> } & { [$values]?: Schema<T[keyof T]> } & { [$strict]?: boolean }\n : T extends Array<infer A>\n ? Schema<A>[] | unknown\n : unknown;\n\nclass Or<T> extends Operator<T> {}\nexport const or = <T>(...schemas: Schema<T>[]) => new Or(schemas);\n\nclass And<T> extends Operator<T> {}\nexport const and = <T>(...schemas: Schema<T>[]) => new And(schemas);\n\nclass Optional<T> extends Operator<T> {\n constructor(schema: Schema<T>) {\n super([schema]);\n }\n}\nexport const optional = <T>(schema: Schema<T>) => new Optional(schema);\n\nclass Tuple<T> extends Operator<T> {}\nexport const tuple = <T>(...schemas: Schema<T>[]) => new Tuple(schemas);\n\nexport const fromBase64 = typeof Buffer === 'undefined' ? (value: string) => atob(value) : (value: string) => Buffer.from(value, 'base64').toString('utf-8');\n\nconst MULTIPLIERS = {\n ms: 1,\n s: 1000,\n m: 60000,\n h: 3600000,\n d: 86400000,\n w: 604800000,\n};\n\nexport const as = {\n string: (value: string | undefined) => {\n return typeof value === 'string' ? value : undefined;\n },\n number: (value: string | undefined) => {\n const result = parseFloat(value as string);\n return Number.isFinite(result) ? result : undefined;\n },\n date: (value: string | undefined) => {\n const result = Date.parse(value as string);\n return Number.isFinite(result) ? new Date(result) : undefined;\n },\n time: (value: string | undefined) => {\n const matches = value?.match(/^(\\d+)(ms|s|m|h|d|w)?$/);\n if (matches) {\n const [, amount, unit = 'ms'] = matches;\n return parseInt(amount, 10) * MULTIPLIERS[unit as keyof typeof MULTIPLIERS];\n }\n return undefined;\n },\n boolean: (value: string | undefined) =>\n /^(0|1|true|false|enabled|disabled)$/i.test(value as string) ? /^(1|true|enabled)$/i.test(value as string) : undefined,\n array: (value: string | undefined, delimiter: string) => value?.split?.(delimiter) ?? undefined,\n json: (value: string | undefined) => {\n try {\n return JSON.parse(value as string);\n } catch (e) {\n return undefined;\n }\n },\n base64: (value: string | undefined) => {\n try {\n return fromBase64(value as string);\n } catch (e) {\n return undefined;\n }\n },\n};\n\nexport interface ErrorFormatter {\n (value: unknown, expected: unknown, path: string, subject: string): string;\n}\n\nexport const formatError: ErrorFormatter = (value, expected, path, subject) =>\n `Invalid ${subject} ${JSON.stringify(value)} for path ${path}, expected ${expected}.`;\n\nclass Context {\n public readonly registry: unknown[] = [];\n private varIndex = 0;\n\n register(value: unknown): number {\n if (!this.registry.includes(value)) {\n this.registry.push(value);\n }\n return this.registry.indexOf(value);\n }\n\n unique(prefix: string) {\n return `${prefix}$$${this.varIndex++}`;\n }\n}\n\nconst codeGen = <T>(schema: Schema<T>, context: Context, valuePath: string, path: string): string => {\n if (schema instanceof And) {\n const valueAlias = context.unique('v');\n const errorsAlias = context.unique('err');\n const code = schema.schemas.map((s) => `try { ${codeGen(s, context, valueAlias, path)} } catch (e) { ${errorsAlias}.push(e.message); }`).join('\\n');\n return `// And\n const ${errorsAlias} = [];\n const ${valueAlias} = ${valuePath};\n ${code}\n if (${errorsAlias}.length !== 0) { throw new TypeError(${errorsAlias}.join('\\\\n')); }\n`;\n } else if (schema instanceof Or) {\n const valueAlias = context.unique('v');\n const errorsAlias = context.unique('err');\n const code = schema.schemas\n .map((s) => codeGen(s, context, valueAlias, path))\n .reduceRight((result, code) => `try {${code}} catch (e) {${errorsAlias}.push(e.message);${result}}`, `throw new TypeError(${errorsAlias}.join('\\\\n'));`);\n return `// Or\nconst ${errorsAlias} = [];\nconst ${valueAlias} = ${valuePath};\n${code}\n `;\n } else if (schema instanceof Optional) {\n const valueAlias = context.unique('v');\n return `// Optional\nconst ${valueAlias} = ${valuePath};\nif (${valueAlias} !== undefined && ${valueAlias} !== null) { ${codeGen(schema.schemas[0], context, valueAlias, path)} }\n`;\n } else if (schema instanceof Tuple) {\n const valueAlias = context.unique('v');\n const errorsAlias = context.unique('err');\n const code: string[] = [\n '// Tuple',\n `const ${valueAlias} = ${valuePath};`,\n `const ${errorsAlias} = [];`,\n `if (${valueAlias} === null || ${valueAlias} === undefined) { throw new TypeError(\\`Invalid value \\${${valueAlias}} for path \"${path}\", expected non-nullable.\\`); }`,\n `if (typeof ${valueAlias} !== 'object') { throw new TypeError(\\`Invalid type \\${typeof ${valueAlias}} for path \"${path}\", expected an instance of Array\\`); }`,\n `if (!Array.isArray(${valueAlias})) { throw new TypeError(\\`Invalid instance of \\${${valueAlias}.constructor?.name} for path \"${path}\", expected an instance of Array.\\`); }`,\n `if (${valueAlias}.length > ${schema.schemas.length}) { throw new TypeError(\\`Invalid tuple length \\${${valueAlias}.length} for path \"${path}\", expected ${schema.schemas.length}.\\`); }`,\n ...schema.schemas.map(\n (s, idx) => `try { ${codeGen(s, context, `${valueAlias}[${idx}]`, `${path}[${idx}]`)} } catch (e) { ${errorsAlias}.push(e.message); }`,\n ),\n `if (${errorsAlias}.length !== 0) { throw new TypeError(${errorsAlias}.join('\\\\n')); }`,\n ];\n return code.join('\\n');\n } else if (typeof schema === 'function') {\n const index = context.register(schema);\n const valueAlias = context.unique('v');\n const registryAlias = context.unique('r');\n return `\nconst ${valueAlias} = ${valuePath};\nconst ${registryAlias} = ctx.registry[${index}];\nif (${valueAlias} === null || ${valueAlias} === undefined) { throw new TypeError(\\`Invalid value \\${${valueAlias}} for path \"${path}\", expected non-nullable.\\`); }\nif (typeof ${valueAlias} === 'object' && !(${valueAlias} instanceof ${registryAlias})) { throw new TypeError(\\`Invalid instance of \\${${valueAlias}?.constructor?.name} for path \"${path}\", expected an instance of ${schema?.name}\\`); }\nif (typeof ${valueAlias} !== 'object' && ${valueAlias}?.constructor !== ${registryAlias}) { throw new TypeError(\\`Invalid type \\${${valueAlias}?.constructor?.name} for path \"${path}\", expected type ${schema?.name}\\`); }\n`;\n } else if (Array.isArray(schema)) {\n const valueAlias = context.unique('v');\n const code: string[] = [\n `const ${valueAlias} = ${valuePath};`,\n `if (${valueAlias} === null || ${valueAlias} === undefined) { throw new TypeError(\\`Invalid value \\${${valueAlias}} for path \"${path}\", expected non-nullable.\\`); }`,\n `if (typeof ${valueAlias} !== 'object') { throw new TypeError(\\`Invalid type \\${typeof ${valueAlias}} for path \"${path}\", expected an instance of Array.\\`); }`,\n `if (!Array.isArray(${valueAlias})) { throw new TypeError(\\`Invalid instance of \\${${valueAlias}.constructor?.name} for path \"${path}\", expected an instance of Array.\\`); }`,\n ];\n if (schema.length > 0) {\n const value = context.unique('val');\n const key = context.unique('key');\n const errorsAlias = context.unique('err');\n code.push(`const ${errorsAlias} = [];`);\n code.push(\n ...schema.map(\n (s) =>\n `${valueAlias}.forEach((${value},${key}) => { try { ${codeGen(s, context, value, `${path}[\\${${key}}]`)} } catch(e){ ${errorsAlias}.push(e.message); } });`,\n ),\n );\n\n code.push(`if (${errorsAlias}.length !== 0) { throw new TypeError(${errorsAlias}.join('\\\\n')); }`);\n }\n return code.join('\\n');\n } else if (typeof schema === 'object' && schema !== null) {\n if (schema instanceof RegExp) {\n const valueAlias = context.unique('v');\n return `\nconst ${valueAlias} = ${valuePath};\nif (!${schema.toString()}.test('' + ${valueAlias})) { throw new TypeError(\\`Invalid value \\${${valueAlias}} for path \"${path}\", expected to match ${schema.toString()}\\`); }\n`;\n } else {\n const valueAlias = context.unique('v');\n const code: string[] = [\n `const ${valueAlias} = ${valuePath};`,\n `if (${valueAlias} === null || ${valueAlias} === undefined) { throw new TypeError(\\`Invalid value \\${${valueAlias}} for path \"${path}\", expected non-nullable.\\`); }`,\n `if (typeof ${valueAlias} !== 'object') { throw new TypeError(\\`Invalid type \\${typeof ${valueAlias}} for path \"${path}\", expected type is object.\\`); }`,\n ];\n if ($keys in schema) {\n const keysAlias = context.unique('k');\n const errorsAlias = context.unique('err');\n const kAlias = context.unique('k');\n code.push(`\nconst ${keysAlias} = Object.keys(${valueAlias});\nconst ${errorsAlias} = ${keysAlias}.map(${kAlias} => { try { ${codeGen(schema[$keys], context, kAlias, `${path}[\\${${kAlias}}]`)} } catch (e) { return e.message; } }).filter(Boolean);\nif (${errorsAlias}.length !== 0) { throw new TypeError(${errorsAlias}.join('\\\\n')); }\n`);\n }\n if ($values in schema) {\n const vAlias = context.unique('val');\n const kAlias = context.unique('k');\n const entriesAlias = context.unique('en');\n const errorsAlias = context.unique('err');\n code.push(`\nconst ${entriesAlias} = Object.entries(${valueAlias});\nconst ${errorsAlias} = ${entriesAlias}.map(([${kAlias},${vAlias}]) => { try { ${codeGen(schema[$values], context, vAlias, `${path}[\\${${kAlias}}]`)} } catch (e) { return e.message; } }).filter(Boolean);\nif (${errorsAlias}.length !== 0) { throw new TypeError(${errorsAlias}.join('\\\\n')); }\n`);\n }\n if ($strict in schema && schema[$strict]) {\n const keysAlias = context.unique('k');\n const kAlias = context.unique('k');\n const extraAlias = context.unique('ex');\n code.push(`const ${keysAlias} = new Set(${JSON.stringify(Object.keys(schema))});`);\n code.push(`const ${extraAlias} = Object.keys(${valueAlias}).filter(${kAlias} => !${keysAlias}.has(${kAlias}));`);\n code.push(`if (${extraAlias}.length !== 0) { throw new TypeError(\\`Extra properties: \\${${extraAlias}}, are not allowed for path \"${path}\"\\`); }`);\n }\n code.push(...Object.entries(schema).map(([key, s]) => codeGen(s, context, `${valueAlias}['${key}']`, `${path}.${key}`)));\n return `${code.join('\\n')}`;\n }\n } else if (typeof schema === 'symbol') {\n const index = context.register(schema);\n const valueAlias = context.unique('v');\n const registryAlias = context.unique('r');\n\n return `\nconst ${valueAlias} = ${valuePath};\nconst ${registryAlias} = ctx.registry[${index}];\nif (typeof ${valueAlias} !== 'symbol') { throw new TypeError(\\`Invalid type \\${typeof ${valueAlias}} for \"path\", expected symbol\\`); }\nif (${valueAlias} !== ${registryAlias}) { throw new TypeError(\\`Invalid value \\${${valueAlias}.toString()} for path \"${path}\", expected ${schema.toString()}\\`); }\n `;\n } else if (schema === null || schema === undefined) {\n const valueAlias = context.unique('v');\n return `\nconst ${valueAlias} = ${valuePath};\nif (${valueAlias} !== null && ${valueAlias} !== undefined ) { throw new TypeError(\\`Invalid value ${valueAlias} for path \"${path}\", expected nullable\\`); }\n `;\n } else {\n const valueAlias = context.unique('v');\n const value = context.unique('val');\n return `\nconst ${valueAlias} = ${valuePath};\nconst ${value} = ${JSON.stringify(schema)};\nif (typeof ${valueAlias} !== '${typeof schema}') { throw new TypeError(\\`Invalid type \\${typeof ${valueAlias}} for path \"${path}\", expected ${typeof schema}\\`); }\nif (${valueAlias} !== ${value}) { throw new TypeError(\\`Invalid value ${JSON.stringify(valueAlias)} for path \"${path}\", expected ${JSON.stringify(schema)}\\`); }\n`;\n }\n};\n\nexport const compile = <T>(schema: Schema<T>, rootName: string) => {\n const context = new Context();\n const code = codeGen(schema, context, 'data', rootName);\n const validator = new Function('ctx', 'data', code);\n return (data: T) => validator(context, data);\n};\n\nexport const ascertain = <T>(schema: Schema<T>, data: T, rootName = '[root]') => {\n compile(schema, rootName)(data);\n};\n"],"names":["$keys","$strict","$values","and","as","ascertain","compile","formatError","fromBase64","optional","or","tuple","Operator","constructor","schemas","length","TypeError","name","Symbol","for","Or","And","Optional","schema","Tuple","Buffer","value","atob","from","toString","MULTIPLIERS","ms","s","m","h","d","w","string","undefined","number","result","parseFloat","Number","isFinite","date","Date","parse","time","matches","match","amount","unit","parseInt","boolean","test","array","delimiter","split","json","JSON","e","base64","expected","path","subject","stringify","Context","registry","varIndex","register","includes","push","indexOf","unique","prefix","codeGen","context","valuePath","valueAlias","errorsAlias","code","map","join","reduceRight","idx","index","registryAlias","Array","isArray","key","RegExp","keysAlias","kAlias","vAlias","entriesAlias","extraAlias","Object","keys","entries","rootName","validator","Function","data"],"mappings":";;;;;;;;;;;IAQaA,KAAK;eAALA;;IAEAC,OAAO;eAAPA;;IADAC,OAAO;eAAPA;;IAcAC,GAAG;eAAHA;;IAuBAC,EAAE;eAAFA;;IA+NAC,SAAS;eAATA;;IAPAC,OAAO;eAAPA;;IA7KAC,WAAW;eAAXA;;IAtDAC,UAAU;eAAVA;;IALAC,QAAQ;eAARA;;IAVAC,EAAE;eAAFA;;IAaAC,KAAK;eAALA;;;AAjCb,MAAeC;;IACbC,YAAY,AAAgBC,OAAoB,CAAE;aAAtBA,UAAAA;QAC1B,IAAIA,QAAQC,MAAM,KAAK,GAAG;YACxB,MAAM,IAAIC,UAAU,CAAC,iBAAiB,EAAE,IAAI,CAACH,WAAW,CAACI,IAAI,CAAC,+BAA+B,CAAC;QAChG;IACF;AACF;AAEO,MAAMjB,QAAQkB,OAAOC,GAAG,CAAC;AACzB,MAAMjB,UAAUgB,OAAOC,GAAG,CAAC;AAC3B,MAAMlB,UAAUiB,OAAOC,GAAG,CAAC;AASlC,MAAMC,WAAcR;AAAa;AAC1B,MAAMF,KAAK,CAAI,GAAGI,UAAyB,IAAIM,GA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AAO,QAAQrB;IAC9C,OAAO,CAACsB,OAAYF,UAAUxB,SAAS0B;AACzC;AAEO,MAAMjG,YAAY,CAAIkB,QAAmB+E,MAASH,WAAW,QAAQ;IAC1E7F,QAAQiB,QAAQ4E,UAAUG;AAC5B"}
|
package/build/index.d.ts
CHANGED
|
@@ -1,39 +1,32 @@
|
|
|
1
|
-
|
|
2
|
-
readonly value: unknown;
|
|
3
|
-
readonly expected: unknown;
|
|
4
|
-
readonly path: string;
|
|
5
|
-
readonly subject: string;
|
|
6
|
-
constructor(value: unknown, expected: unknown, path: string, subject?: string);
|
|
7
|
-
}
|
|
8
|
-
type Keys = string | number | symbol;
|
|
9
|
-
type DataValue = any;
|
|
10
|
-
export type Data = Record<Keys, unknown>;
|
|
11
|
-
type DataArray = DataValue[];
|
|
12
|
-
export type Schema<T> = T extends Data ? {
|
|
13
|
-
[S in keyof T]?: Schema<T[S]>;
|
|
14
|
-
} : T extends DataArray ? [Schema<T[number]>] : DataValue;
|
|
15
|
-
export type SchemaData<S> = S extends Record<Keys, unknown> ? {
|
|
16
|
-
[K in keyof S as Exclude<K, typeof $keys | typeof $values>]?: SchemaData<S[K]>;
|
|
17
|
-
} & Record<string, unknown> : S extends unknown[] ? unknown[] : unknown;
|
|
18
|
-
declare abstract class Operation<T extends Data = DataValue> {
|
|
1
|
+
declare abstract class Operator<T> {
|
|
19
2
|
readonly schemas: Schema<T>[];
|
|
20
3
|
constructor(schemas: Schema<T>[]);
|
|
21
4
|
}
|
|
22
|
-
declare
|
|
5
|
+
export declare const $keys: unique symbol;
|
|
6
|
+
export declare const $values: unique symbol;
|
|
7
|
+
export declare const $strict: unique symbol;
|
|
8
|
+
export type Schema<T> = T extends Record<string | number | symbol, unknown> ? {
|
|
9
|
+
[K in keyof T]?: Schema<T[K]> | unknown;
|
|
10
|
+
} & {
|
|
11
|
+
[$keys]?: Schema<keyof T>;
|
|
12
|
+
} & {
|
|
13
|
+
[$values]?: Schema<T[keyof T]>;
|
|
14
|
+
} & {
|
|
15
|
+
[$strict]?: boolean;
|
|
16
|
+
} : T extends Array<infer A> ? Schema<A>[] | unknown : unknown;
|
|
17
|
+
declare class Or<T> extends Operator<T> {
|
|
23
18
|
}
|
|
24
|
-
export declare const or: <T
|
|
25
|
-
declare class And<T
|
|
19
|
+
export declare const or: <T>(...schemas: Schema<T>[]) => Or<T>;
|
|
20
|
+
declare class And<T> extends Operator<T> {
|
|
26
21
|
}
|
|
27
|
-
export declare const and: <T
|
|
28
|
-
declare class Optional<T
|
|
22
|
+
export declare const and: <T>(...schemas: Schema<T>[]) => And<T>;
|
|
23
|
+
declare class Optional<T> extends Operator<T> {
|
|
29
24
|
constructor(schema: Schema<T>);
|
|
30
25
|
}
|
|
31
|
-
export declare const optional: <T
|
|
32
|
-
declare class Tuple<T
|
|
26
|
+
export declare const optional: <T>(schema: Schema<T>) => Optional<T>;
|
|
27
|
+
declare class Tuple<T> extends Operator<T> {
|
|
33
28
|
}
|
|
34
|
-
export declare const tuple: <T
|
|
35
|
-
export declare const $keys: unique symbol;
|
|
36
|
-
export declare const $values: unique symbol;
|
|
29
|
+
export declare const tuple: <T>(...schemas: Schema<T>[]) => Tuple<T>;
|
|
37
30
|
export declare const fromBase64: (value: string) => string;
|
|
38
31
|
export declare const as: {
|
|
39
32
|
string: (value: string | undefined) => string | undefined;
|
|
@@ -45,9 +38,10 @@ export declare const as: {
|
|
|
45
38
|
json: (value: string | undefined) => any;
|
|
46
39
|
base64: (value: string | undefined) => string | undefined;
|
|
47
40
|
};
|
|
48
|
-
export interface
|
|
49
|
-
|
|
41
|
+
export interface ErrorFormatter {
|
|
42
|
+
(value: unknown, expected: unknown, path: string, subject: string): string;
|
|
50
43
|
}
|
|
51
|
-
export declare const
|
|
52
|
-
export declare const
|
|
44
|
+
export declare const formatError: ErrorFormatter;
|
|
45
|
+
export declare const compile: <T>(schema: Schema<T>, rootName: string) => (data: T) => any;
|
|
46
|
+
export declare const ascertain: <T>(schema: Schema<T>, data: T, rootName?: string) => void;
|
|
53
47
|
export {};
|
package/build/index.js
CHANGED
|
@@ -1,17 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
value;
|
|
3
|
-
expected;
|
|
4
|
-
path;
|
|
5
|
-
subject;
|
|
6
|
-
constructor(value, expected, path, subject = 'value'){
|
|
7
|
-
super(`Invalid ${subject} ${JSON.stringify(value)} for path ${path}, expected ${expected}.`);
|
|
8
|
-
this.value = value;
|
|
9
|
-
this.expected = expected;
|
|
10
|
-
this.path = path;
|
|
11
|
-
this.subject = subject;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
class Operation {
|
|
1
|
+
class Operator {
|
|
15
2
|
schemas;
|
|
16
3
|
constructor(schemas){
|
|
17
4
|
this.schemas = schemas;
|
|
@@ -20,13 +7,16 @@ class Operation {
|
|
|
20
7
|
}
|
|
21
8
|
}
|
|
22
9
|
}
|
|
23
|
-
|
|
10
|
+
export const $keys = Symbol.for('@@keys');
|
|
11
|
+
export const $values = Symbol.for('@@values');
|
|
12
|
+
export const $strict = Symbol.for('@@strict');
|
|
13
|
+
class Or extends Operator {
|
|
24
14
|
}
|
|
25
15
|
export const or = (...schemas)=>new Or(schemas);
|
|
26
|
-
class And extends
|
|
16
|
+
class And extends Operator {
|
|
27
17
|
}
|
|
28
18
|
export const and = (...schemas)=>new And(schemas);
|
|
29
|
-
class Optional extends
|
|
19
|
+
class Optional extends Operator {
|
|
30
20
|
constructor(schema){
|
|
31
21
|
super([
|
|
32
22
|
schema
|
|
@@ -34,11 +24,9 @@ class Optional extends Operation {
|
|
|
34
24
|
}
|
|
35
25
|
}
|
|
36
26
|
export const optional = (schema)=>new Optional(schema);
|
|
37
|
-
class Tuple extends
|
|
27
|
+
class Tuple extends Operator {
|
|
38
28
|
}
|
|
39
29
|
export const tuple = (...schemas)=>new Tuple(schemas);
|
|
40
|
-
export const $keys = Symbol.for('@@keys');
|
|
41
|
-
export const $values = Symbol.for('@@values');
|
|
42
30
|
export const fromBase64 = typeof Buffer === 'undefined' ? (value)=>atob(value) : (value)=>Buffer.from(value, 'base64').toString('utf-8');
|
|
43
31
|
const MULTIPLIERS = {
|
|
44
32
|
ms: 1,
|
|
@@ -85,17 +73,10 @@ export const as = {
|
|
|
85
73
|
}
|
|
86
74
|
}
|
|
87
75
|
};
|
|
76
|
+
export const formatError = (value, expected, path, subject)=>`Invalid ${subject} ${JSON.stringify(value)} for path ${path}, expected ${expected}.`;
|
|
88
77
|
class Context {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
registry;
|
|
92
|
-
varIndex;
|
|
93
|
-
constructor(options = {}){
|
|
94
|
-
this.options = options;
|
|
95
|
-
this.registry = [];
|
|
96
|
-
this.varIndex = 0;
|
|
97
|
-
this.Error = options.error ?? AssertError;
|
|
98
|
-
}
|
|
78
|
+
registry = [];
|
|
79
|
+
varIndex = 0;
|
|
99
80
|
register(value) {
|
|
100
81
|
if (!this.registry.includes(value)) {
|
|
101
82
|
this.registry.push(value);
|
|
@@ -110,17 +91,17 @@ const codeGen = (schema, context, valuePath, path)=>{
|
|
|
110
91
|
if (schema instanceof And) {
|
|
111
92
|
const valueAlias = context.unique('v');
|
|
112
93
|
const errorsAlias = context.unique('err');
|
|
113
|
-
const code = schema.schemas.map((s)=>`try { ${codeGen(s, context, valueAlias, path)} } catch (e) { ${errorsAlias}.push(e); }`).join('\n');
|
|
94
|
+
const code = schema.schemas.map((s)=>`try { ${codeGen(s, context, valueAlias, path)} } catch (e) { ${errorsAlias}.push(e.message); }`).join('\n');
|
|
114
95
|
return `// And
|
|
115
96
|
const ${errorsAlias} = [];
|
|
116
97
|
const ${valueAlias} = ${valuePath};
|
|
117
98
|
${code}
|
|
118
|
-
if (${errorsAlias}.length !== 0) { throw new
|
|
99
|
+
if (${errorsAlias}.length !== 0) { throw new TypeError(${errorsAlias}.join('\\n')); }
|
|
119
100
|
`;
|
|
120
101
|
} else if (schema instanceof Or) {
|
|
121
102
|
const valueAlias = context.unique('v');
|
|
122
103
|
const errorsAlias = context.unique('err');
|
|
123
|
-
const code = schema.schemas.map((s)=>codeGen(s, context, valueAlias, path)).reduceRight((result, code)=>`try {${code}} catch (e) {${errorsAlias}.push(e);${result}}`, `throw new
|
|
104
|
+
const code = schema.schemas.map((s)=>codeGen(s, context, valueAlias, path)).reduceRight((result, code)=>`try {${code}} catch (e) {${errorsAlias}.push(e.message);${result}}`, `throw new TypeError(${errorsAlias}.join('\\n'));`);
|
|
124
105
|
return `// Or
|
|
125
106
|
const ${errorsAlias} = [];
|
|
126
107
|
const ${valueAlias} = ${valuePath};
|
|
@@ -139,9 +120,12 @@ if (${valueAlias} !== undefined && ${valueAlias} !== null) { ${codeGen(schema.sc
|
|
|
139
120
|
'// Tuple',
|
|
140
121
|
`const ${valueAlias} = ${valuePath};`,
|
|
141
122
|
`const ${errorsAlias} = [];`,
|
|
142
|
-
`if (
|
|
143
|
-
|
|
144
|
-
`if (${
|
|
123
|
+
`if (${valueAlias} === null || ${valueAlias} === undefined) { throw new TypeError(\`Invalid value \${${valueAlias}} for path "${path}", expected non-nullable.\`); }`,
|
|
124
|
+
`if (typeof ${valueAlias} !== 'object') { throw new TypeError(\`Invalid type \${typeof ${valueAlias}} for path "${path}", expected an instance of Array\`); }`,
|
|
125
|
+
`if (!Array.isArray(${valueAlias})) { throw new TypeError(\`Invalid instance of \${${valueAlias}.constructor?.name} for path "${path}", expected an instance of Array.\`); }`,
|
|
126
|
+
`if (${valueAlias}.length > ${schema.schemas.length}) { throw new TypeError(\`Invalid tuple length \${${valueAlias}.length} for path "${path}", expected ${schema.schemas.length}.\`); }`,
|
|
127
|
+
...schema.schemas.map((s, idx)=>`try { ${codeGen(s, context, `${valueAlias}[${idx}]`, `${path}[${idx}]`)} } catch (e) { ${errorsAlias}.push(e.message); }`),
|
|
128
|
+
`if (${errorsAlias}.length !== 0) { throw new TypeError(${errorsAlias}.join('\\n')); }`
|
|
145
129
|
];
|
|
146
130
|
return code.join('\n');
|
|
147
131
|
} else if (typeof schema === 'function') {
|
|
@@ -151,23 +135,25 @@ if (${valueAlias} !== undefined && ${valueAlias} !== null) { ${codeGen(schema.sc
|
|
|
151
135
|
return `
|
|
152
136
|
const ${valueAlias} = ${valuePath};
|
|
153
137
|
const ${registryAlias} = ctx.registry[${index}];
|
|
154
|
-
if (${valueAlias} === null || ${valueAlias} === undefined) { throw new
|
|
155
|
-
if (typeof ${valueAlias} === 'object' && !(${valueAlias} instanceof ${registryAlias})) { throw new
|
|
156
|
-
if (typeof ${valueAlias} !== 'object' && ${valueAlias}?.constructor !== ${registryAlias}) { throw new
|
|
138
|
+
if (${valueAlias} === null || ${valueAlias} === undefined) { throw new TypeError(\`Invalid value \${${valueAlias}} for path "${path}", expected non-nullable.\`); }
|
|
139
|
+
if (typeof ${valueAlias} === 'object' && !(${valueAlias} instanceof ${registryAlias})) { throw new TypeError(\`Invalid instance of \${${valueAlias}?.constructor?.name} for path "${path}", expected an instance of ${schema?.name}\`); }
|
|
140
|
+
if (typeof ${valueAlias} !== 'object' && ${valueAlias}?.constructor !== ${registryAlias}) { throw new TypeError(\`Invalid type \${${valueAlias}?.constructor?.name} for path "${path}", expected type ${schema?.name}\`); }
|
|
157
141
|
`;
|
|
158
142
|
} else if (Array.isArray(schema)) {
|
|
159
143
|
const valueAlias = context.unique('v');
|
|
160
144
|
const code = [
|
|
161
145
|
`const ${valueAlias} = ${valuePath};`,
|
|
162
|
-
`if (
|
|
146
|
+
`if (${valueAlias} === null || ${valueAlias} === undefined) { throw new TypeError(\`Invalid value \${${valueAlias}} for path "${path}", expected non-nullable.\`); }`,
|
|
147
|
+
`if (typeof ${valueAlias} !== 'object') { throw new TypeError(\`Invalid type \${typeof ${valueAlias}} for path "${path}", expected an instance of Array.\`); }`,
|
|
148
|
+
`if (!Array.isArray(${valueAlias})) { throw new TypeError(\`Invalid instance of \${${valueAlias}.constructor?.name} for path "${path}", expected an instance of Array.\`); }`
|
|
163
149
|
];
|
|
164
150
|
if (schema.length > 0) {
|
|
165
151
|
const value = context.unique('val');
|
|
166
152
|
const key = context.unique('key');
|
|
167
153
|
const errorsAlias = context.unique('err');
|
|
168
154
|
code.push(`const ${errorsAlias} = [];`);
|
|
169
|
-
code.push(...schema.map((s)=>`${valueAlias}.forEach((${value},${key}) => { try { ${codeGen(s, context, value, `${path}[\${${key}}]`)} } catch(e){ ${errorsAlias}.push(e); } });`));
|
|
170
|
-
code.push(`if (${errorsAlias}.length !== 0) { throw new
|
|
155
|
+
code.push(...schema.map((s)=>`${valueAlias}.forEach((${value},${key}) => { try { ${codeGen(s, context, value, `${path}[\${${key}}]`)} } catch(e){ ${errorsAlias}.push(e.message); } });`));
|
|
156
|
+
code.push(`if (${errorsAlias}.length !== 0) { throw new TypeError(${errorsAlias}.join('\\n')); }`);
|
|
171
157
|
}
|
|
172
158
|
return code.join('\n');
|
|
173
159
|
} else if (typeof schema === 'object' && schema !== null) {
|
|
@@ -175,38 +161,46 @@ if (typeof ${valueAlias} !== 'object' && ${valueAlias}?.constructor !== ${regist
|
|
|
175
161
|
const valueAlias = context.unique('v');
|
|
176
162
|
return `
|
|
177
163
|
const ${valueAlias} = ${valuePath};
|
|
178
|
-
if (!${schema.toString()}.test('' + ${valueAlias})) { throw new
|
|
164
|
+
if (!${schema.toString()}.test('' + ${valueAlias})) { throw new TypeError(\`Invalid value \${${valueAlias}} for path "${path}", expected to match ${schema.toString()}\`); }
|
|
179
165
|
`;
|
|
180
166
|
} else {
|
|
181
167
|
const valueAlias = context.unique('v');
|
|
182
168
|
const code = [
|
|
183
169
|
`const ${valueAlias} = ${valuePath};`,
|
|
184
|
-
`if (${valueAlias} === null || ${valueAlias} === undefined) { throw new
|
|
185
|
-
`if (typeof ${valueAlias} !== 'object') { throw new
|
|
170
|
+
`if (${valueAlias} === null || ${valueAlias} === undefined) { throw new TypeError(\`Invalid value \${${valueAlias}} for path "${path}", expected non-nullable.\`); }`,
|
|
171
|
+
`if (typeof ${valueAlias} !== 'object') { throw new TypeError(\`Invalid type \${typeof ${valueAlias}} for path "${path}", expected type is object.\`); }`
|
|
186
172
|
];
|
|
187
173
|
if ($keys in schema) {
|
|
188
|
-
const keysAlias = context.unique('
|
|
174
|
+
const keysAlias = context.unique('k');
|
|
189
175
|
const errorsAlias = context.unique('err');
|
|
190
|
-
const
|
|
176
|
+
const kAlias = context.unique('k');
|
|
191
177
|
code.push(`
|
|
192
178
|
const ${keysAlias} = Object.keys(${valueAlias});
|
|
193
|
-
const ${errorsAlias} = ${keysAlias}.
|
|
194
|
-
if (${errorsAlias}.length !== 0) { throw new
|
|
179
|
+
const ${errorsAlias} = ${keysAlias}.map(${kAlias} => { try { ${codeGen(schema[$keys], context, kAlias, `${path}[\${${kAlias}}]`)} } catch (e) { return e.message; } }).filter(Boolean);
|
|
180
|
+
if (${errorsAlias}.length !== 0) { throw new TypeError(${errorsAlias}.join('\\n')); }
|
|
195
181
|
`);
|
|
196
182
|
}
|
|
197
183
|
if ($values in schema) {
|
|
198
184
|
const vAlias = context.unique('val');
|
|
199
|
-
const
|
|
185
|
+
const kAlias = context.unique('k');
|
|
186
|
+
const entriesAlias = context.unique('en');
|
|
200
187
|
const errorsAlias = context.unique('err');
|
|
201
|
-
code.push(`
|
|
202
|
-
const ${
|
|
203
|
-
const ${errorsAlias} = ${
|
|
204
|
-
if (${errorsAlias}.length !== 0) { throw new
|
|
205
|
-
|
|
188
|
+
code.push(`
|
|
189
|
+
const ${entriesAlias} = Object.entries(${valueAlias});
|
|
190
|
+
const ${errorsAlias} = ${entriesAlias}.map(([${kAlias},${vAlias}]) => { try { ${codeGen(schema[$values], context, vAlias, `${path}[\${${kAlias}}]`)} } catch (e) { return e.message; } }).filter(Boolean);
|
|
191
|
+
if (${errorsAlias}.length !== 0) { throw new TypeError(${errorsAlias}.join('\\n')); }
|
|
192
|
+
`);
|
|
193
|
+
}
|
|
194
|
+
if ($strict in schema && schema[$strict]) {
|
|
195
|
+
const keysAlias = context.unique('k');
|
|
196
|
+
const kAlias = context.unique('k');
|
|
197
|
+
const extraAlias = context.unique('ex');
|
|
198
|
+
code.push(`const ${keysAlias} = new Set(${JSON.stringify(Object.keys(schema))});`);
|
|
199
|
+
code.push(`const ${extraAlias} = Object.keys(${valueAlias}).filter(${kAlias} => !${keysAlias}.has(${kAlias}));`);
|
|
200
|
+
code.push(`if (${extraAlias}.length !== 0) { throw new TypeError(\`Extra properties: \${${extraAlias}}, are not allowed for path "${path}"\`); }`);
|
|
206
201
|
}
|
|
207
|
-
|
|
208
|
-
code.
|
|
209
|
-
return `{${code.join('\n')}}`;
|
|
202
|
+
code.push(...Object.entries(schema).map(([key, s])=>codeGen(s, context, `${valueAlias}['${key}']`, `${path}.${key}`)));
|
|
203
|
+
return `${code.join('\n')}`;
|
|
210
204
|
}
|
|
211
205
|
} else if (typeof schema === 'symbol') {
|
|
212
206
|
const index = context.register(schema);
|
|
@@ -215,136 +209,34 @@ if (${errorsAlias}.length !== 0) { throw new AggregateError(${errorsAlias}, 'Inv
|
|
|
215
209
|
return `
|
|
216
210
|
const ${valueAlias} = ${valuePath};
|
|
217
211
|
const ${registryAlias} = ctx.registry[${index}];
|
|
218
|
-
if (typeof ${valueAlias} !== 'symbol') { throw new
|
|
219
|
-
if (${valueAlias} !== ${registryAlias}) { throw new
|
|
212
|
+
if (typeof ${valueAlias} !== 'symbol') { throw new TypeError(\`Invalid type \${typeof ${valueAlias}} for "path", expected symbol\`); }
|
|
213
|
+
if (${valueAlias} !== ${registryAlias}) { throw new TypeError(\`Invalid value \${${valueAlias}.toString()} for path "${path}", expected ${schema.toString()}\`); }
|
|
220
214
|
`;
|
|
221
215
|
} else if (schema === null || schema === undefined) {
|
|
222
216
|
const valueAlias = context.unique('v');
|
|
223
217
|
return `
|
|
224
218
|
const ${valueAlias} = ${valuePath};
|
|
225
|
-
if (${valueAlias} !== null && ${valueAlias} !== undefined ) { throw new
|
|
219
|
+
if (${valueAlias} !== null && ${valueAlias} !== undefined ) { throw new TypeError(\`Invalid value ${valueAlias} for path "${path}", expected nullable\`); }
|
|
226
220
|
`;
|
|
227
221
|
} else {
|
|
228
222
|
const valueAlias = context.unique('v');
|
|
229
|
-
const typeAlias = context.unique('t');
|
|
230
223
|
const value = context.unique('val');
|
|
231
224
|
return `
|
|
232
225
|
const ${valueAlias} = ${valuePath};
|
|
233
|
-
const ${typeAlias} = '${typeof schema}';
|
|
234
226
|
const ${value} = ${JSON.stringify(schema)};
|
|
235
|
-
if (typeof ${valueAlias} !== ${
|
|
236
|
-
if (${valueAlias} !== ${value}) { throw new
|
|
227
|
+
if (typeof ${valueAlias} !== '${typeof schema}') { throw new TypeError(\`Invalid type \${typeof ${valueAlias}} for path "${path}", expected ${typeof schema}\`); }
|
|
228
|
+
if (${valueAlias} !== ${value}) { throw new TypeError(\`Invalid value ${JSON.stringify(valueAlias)} for path "${path}", expected ${JSON.stringify(schema)}\`); }
|
|
237
229
|
`;
|
|
238
230
|
}
|
|
239
231
|
};
|
|
240
|
-
const
|
|
241
|
-
|
|
242
|
-
};
|
|
243
|
-
export const compile = (schema, rootName, options = {})=>{
|
|
244
|
-
const context = new Context(options);
|
|
232
|
+
export const compile = (schema, rootName)=>{
|
|
233
|
+
const context = new Context();
|
|
245
234
|
const code = codeGen(schema, context, 'data', rootName);
|
|
246
235
|
const validator = new Function('ctx', 'data', code);
|
|
247
|
-
return (data)=>
|
|
248
|
-
try {
|
|
249
|
-
validator(context, data);
|
|
250
|
-
} catch (e) {
|
|
251
|
-
const errors = e instanceof AggregateError ? flatAggregateError(e) : [
|
|
252
|
-
e
|
|
253
|
-
];
|
|
254
|
-
throw new AggregateError(errors, 'Validation failure');
|
|
255
|
-
}
|
|
256
|
-
};
|
|
257
|
-
};
|
|
258
|
-
const assert = (target, schema, path)=>{
|
|
259
|
-
if (schema instanceof And) {
|
|
260
|
-
return schema.schemas.flatMap((schema)=>assert(target, schema, path)).filter((error)=>!!error);
|
|
261
|
-
} else if (schema instanceof Or) {
|
|
262
|
-
const errors = schema.schemas.flatMap((schema)=>assert(target, schema, path));
|
|
263
|
-
const filteredErrors = errors.filter((error)=>!!error);
|
|
264
|
-
if (filteredErrors.length === schema.schemas.length) {
|
|
265
|
-
return filteredErrors;
|
|
266
|
-
}
|
|
267
|
-
} else if (schema instanceof Optional) {
|
|
268
|
-
if (target !== undefined && target !== null) {
|
|
269
|
-
return assert(target, schema.schemas[0], path);
|
|
270
|
-
}
|
|
271
|
-
} else if (schema instanceof Tuple) {
|
|
272
|
-
if (!Array.isArray(target)) {
|
|
273
|
-
return [
|
|
274
|
-
new AssertError(target, 'array', path)
|
|
275
|
-
];
|
|
276
|
-
}
|
|
277
|
-
return schema.schemas.flatMap((s, idx)=>assert(target[idx], s, `${path}[${idx}]`)).filter((error)=>!!error);
|
|
278
|
-
} else if (typeof schema === 'function') {
|
|
279
|
-
if (target === null || target === undefined) {
|
|
280
|
-
return [
|
|
281
|
-
new AssertError(target, 'a non-nullable', path)
|
|
282
|
-
];
|
|
283
|
-
}
|
|
284
|
-
if (typeof target === 'object' && !(target instanceof schema)) {
|
|
285
|
-
return [
|
|
286
|
-
new AssertError(target?.constructor?.name, `instance of ${schema.name}`, path, 'instance of')
|
|
287
|
-
];
|
|
288
|
-
}
|
|
289
|
-
if (typeof target !== 'object' && target?.constructor !== schema) {
|
|
290
|
-
return [
|
|
291
|
-
new AssertError(target?.constructor?.name, schema.name, path, 'type')
|
|
292
|
-
];
|
|
293
|
-
}
|
|
294
|
-
} else if (Array.isArray(schema)) {
|
|
295
|
-
if (!Array.isArray(target)) {
|
|
296
|
-
return [
|
|
297
|
-
new AssertError(target, 'array', path)
|
|
298
|
-
];
|
|
299
|
-
}
|
|
300
|
-
return schema.flatMap((s)=>target.flatMap((value, idx)=>assert(value, s, `${path}[${idx}]`))).filter((error)=>!!error);
|
|
301
|
-
} else if (typeof schema === 'object' && schema !== null) {
|
|
302
|
-
if (schema instanceof RegExp) {
|
|
303
|
-
if (!schema.test('' + target)) {
|
|
304
|
-
return [
|
|
305
|
-
new AssertError(target, `matching ${schema.toString()}`, path)
|
|
306
|
-
];
|
|
307
|
-
}
|
|
308
|
-
return [];
|
|
309
|
-
} else {
|
|
310
|
-
if (target === null || target === undefined) {
|
|
311
|
-
return [
|
|
312
|
-
new AssertError(target, 'object', path)
|
|
313
|
-
];
|
|
314
|
-
}
|
|
315
|
-
if (typeof target !== 'object') {
|
|
316
|
-
return [
|
|
317
|
-
new AssertError(target, schema.constructor.name, path)
|
|
318
|
-
];
|
|
319
|
-
}
|
|
320
|
-
if ($keys in schema) {
|
|
321
|
-
const targetKeys = Object.keys(target);
|
|
322
|
-
return targetKeys.flatMap((key)=>assert(key, schema[$keys], path)).filter((error)=>!!error);
|
|
323
|
-
}
|
|
324
|
-
if ($values in schema) {
|
|
325
|
-
const targetKeys = Object.keys(target);
|
|
326
|
-
return targetKeys.flatMap((key)=>assert(target[key], schema[$values], path)).filter((error)=>!!error);
|
|
327
|
-
}
|
|
328
|
-
return Object.keys(schema).flatMap((key)=>assert(target[key], schema[key], path)).filter((error)=>!!error);
|
|
329
|
-
}
|
|
330
|
-
} else if (schema === null || schema === undefined) {
|
|
331
|
-
if (target !== null && target !== undefined) {
|
|
332
|
-
return [
|
|
333
|
-
new AssertError(target, 'nullable', path)
|
|
334
|
-
];
|
|
335
|
-
}
|
|
336
|
-
} else if (target !== schema) {
|
|
337
|
-
return [
|
|
338
|
-
new AssertError(target, schema, path)
|
|
339
|
-
];
|
|
340
|
-
}
|
|
341
|
-
return [];
|
|
236
|
+
return (data)=>validator(context, data);
|
|
342
237
|
};
|
|
343
238
|
export const ascertain = (schema, data, rootName = '[root]')=>{
|
|
344
|
-
|
|
345
|
-
if (result.length > 0) {
|
|
346
|
-
throw new AggregateError(result, 'Validation failure');
|
|
347
|
-
}
|
|
239
|
+
compile(schema, rootName)(data);
|
|
348
240
|
};
|
|
349
241
|
|
|
350
242
|
//# sourceMappingURL=index.js.map
|
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export class AssertError extends TypeError {\n constructor(\n public readonly value: unknown,\n public readonly expected: unknown,\n public readonly path: string,\n public readonly subject = 'value',\n ) {\n super(`Invalid ${subject} ${JSON.stringify(value)} for path ${path}, expected ${expected}.`);\n }\n}\n\ntype Keys = string | number | symbol;\ntype DataValue = any;\nexport type Data = Record<Keys, unknown>;\ntype DataArray = DataValue[];\n\nexport type Schema<T> = T extends Data ? { [S in keyof T]?: Schema<T[S]> } : T extends DataArray ? [Schema<T[number]>] : DataValue;\n\nexport type SchemaData<S> =\n S extends Record<Keys, unknown>\n ? { [K in keyof S as Exclude<K, typeof $keys | typeof $values>]?: SchemaData<S[K]> } & Record<string, unknown>\n : S extends unknown[]\n ? unknown[]\n : unknown;\n\nabstract class Operation<T extends Data = DataValue> {\n constructor(public readonly schemas: Schema<T>[]) {\n if (schemas.length === 0) {\n throw new TypeError(`Operation schema ${this.constructor.name} must have at least one element`);\n }\n }\n}\n\nclass Or<T extends Data = DataValue> extends Operation<T> {}\nexport const or = <T extends Data = DataValue>(...schemas: Schema<T>[]) => new Or(schemas);\n\nclass And<T extends Data = DataValue> extends Operation<T> {}\nexport const and = <T extends Data = DataValue>(...schemas: Schema<T>[]) => new And(schemas);\n\nclass Optional<T extends Data = DataValue> extends Operation<T> {\n constructor(schema: Schema<T>) {\n super([schema]);\n }\n}\nexport const optional = <T extends Data = DataValue>(schema: Schema<T>) => new Optional(schema);\n\nclass Tuple<T extends Data = DataValue> extends Operation<T> {}\nexport const tuple = <T extends Data = DataValue>(...schemas: Schema<T>[]) => new Tuple(schemas);\n\nexport const $keys = Symbol.for('@@keys');\nexport const $values = Symbol.for('@@values');\nexport const fromBase64 = typeof Buffer === 'undefined' ? (value: string) => atob(value) : (value: string) => Buffer.from(value, 'base64').toString('utf-8');\n\nconst MULTIPLIERS = {\n ms: 1,\n s: 1000,\n m: 60000,\n h: 3600000,\n d: 86400000,\n w: 604800000,\n};\n\nexport const as = {\n string: (value: string | undefined) => {\n return typeof value === 'string' ? value : undefined;\n },\n number: (value: string | undefined) => {\n const result = parseFloat(value as string);\n return Number.isFinite(result) ? result : undefined;\n },\n date: (value: string | undefined) => {\n const result = Date.parse(value as string);\n return Number.isFinite(result) ? new Date(result) : undefined;\n },\n time: (value: string | undefined) => {\n const matches = value?.match(/^(\\d+)(ms|s|m|h|d|w)?$/);\n if (matches) {\n const [, amount, unit = 'ms'] = matches;\n return parseInt(amount, 10) * MULTIPLIERS[unit as keyof typeof MULTIPLIERS];\n }\n return undefined;\n },\n boolean: (value: string | undefined) =>\n /^(0|1|true|false|enabled|disabled)$/i.test(value as string) ? /^(1|true|enabled)$/i.test(value as string) : undefined,\n array: (value: string | undefined, delimiter: string) => value?.split?.(delimiter) ?? undefined,\n json: (value: string | undefined) => {\n try {\n return JSON.parse(value as string);\n } catch (e) {\n return undefined;\n }\n },\n base64: (value: string | undefined) => {\n try {\n return fromBase64(value as string);\n } catch (e) {\n return undefined;\n }\n },\n};\n\nclass Context {\n public readonly Error: typeof AssertError;\n public readonly registry: unknown[] = [];\n private varIndex = 0;\n\n constructor(public readonly options: CompilerOptions = {}) {\n this.Error = options.error ?? AssertError;\n }\n\n register(value: unknown): number {\n if (!this.registry.includes(value)) {\n this.registry.push(value);\n }\n return this.registry.indexOf(value);\n }\n\n unique(prefix: string) {\n return `${prefix}$$${this.varIndex++}`;\n }\n}\n\nexport interface CompilerOptions {\n error?: typeof AssertError;\n}\n\nconst codeGen = <T extends Data = DataValue>(schema: Schema<T>, context: Context, valuePath: string, path: string): string => {\n if (schema instanceof And) {\n const valueAlias = context.unique('v');\n const errorsAlias = context.unique('err');\n const code = schema.schemas.map((s) => `try { ${codeGen(s, context, valueAlias, path)} } catch (e) { ${errorsAlias}.push(e); }`).join('\\n');\n return `// And\n const ${errorsAlias} = [];\n const ${valueAlias} = ${valuePath};\n ${code}\n if (${errorsAlias}.length !== 0) { throw new AggregateError(${errorsAlias}, 'Invalid value for path \"${path}\"'); }\n`;\n } else if (schema instanceof Or) {\n const valueAlias = context.unique('v');\n const errorsAlias = context.unique('err');\n const code = schema.schemas\n .map((s) => codeGen(s, context, valueAlias, path))\n .reduceRight(\n (result, code) => `try {${code}} catch (e) {${errorsAlias}.push(e);${result}}`,\n `throw new AggregateError(${errorsAlias}, 'Invalid value for path \"${path}\"');`,\n );\n return `// Or\nconst ${errorsAlias} = [];\nconst ${valueAlias} = ${valuePath};\n${code}\n `;\n } else if (schema instanceof Optional) {\n const valueAlias = context.unique('v');\n return `// Optional\nconst ${valueAlias} = ${valuePath};\nif (${valueAlias} !== undefined && ${valueAlias} !== null) { ${codeGen(schema.schemas[0], context, valueAlias, path)} }\n`;\n } else if (schema instanceof Tuple) {\n const valueAlias = context.unique('v');\n const errorsAlias = context.unique('err');\n const code: string[] = [\n '// Tuple',\n `const ${valueAlias} = ${valuePath};`,\n `const ${errorsAlias} = [];`,\n `if (!Array.isArray(${valueAlias})) { throw new ctx.Error(${valueAlias}, 'array', \\`${path}\\`); }`,\n ...schema.schemas.map((s, idx) => `try { ${codeGen(s, context, `${valueAlias}[${idx}]`, `${path}[${idx}]`)} } catch (e) { ${errorsAlias}.push(e); }`),\n `if (${errorsAlias}.length !== 0) { throw new AggregateError(${errorsAlias}, 'Invalid value for path \"${path}\"'); }`,\n ];\n\n return code.join('\\n');\n } else if (typeof schema === 'function') {\n const index = context.register(schema);\n const valueAlias = context.unique('v');\n const registryAlias = context.unique('r');\n return `\nconst ${valueAlias} = ${valuePath};\nconst ${registryAlias} = ctx.registry[${index}];\nif (${valueAlias} === null || ${valueAlias} === undefined) { throw new ctx.Error(${valueAlias}, 'a non-nullable', \\`${path}\\`); }\nif (typeof ${valueAlias} === 'object' && !(${valueAlias} instanceof ${registryAlias})) { throw new ctx.Error(${valueAlias}?.constructor?.name, \\`instance of \\${${registryAlias}.name}\\`, \\`${path}\\`, 'instance of'); }\nif (typeof ${valueAlias} !== 'object' && ${valueAlias}?.constructor !== ${registryAlias}) { throw new ctx.Error(${valueAlias}?.constructor?.name, ${registryAlias}.name, \\`${path}\\`, 'type'); }\n`;\n } else if (Array.isArray(schema)) {\n const valueAlias = context.unique('v');\n const code: string[] = [\n `const ${valueAlias} = ${valuePath};`,\n `if (!Array.isArray(${valueAlias})) { throw new ctx.Error(${valueAlias}, 'array', \\`${path}\\`); }`,\n ];\n if (schema.length > 0) {\n const value = context.unique('val');\n const key = context.unique('key');\n const errorsAlias = context.unique('err');\n code.push(`const ${errorsAlias} = [];`);\n code.push(\n ...schema.map(\n (s) =>\n `${valueAlias}.forEach((${value},${key}) => { try { ${codeGen(s, context, value, `${path}[\\${${key}}]`)} } catch(e){ ${errorsAlias}.push(e); } });`,\n ),\n );\n\n code.push(`if (${errorsAlias}.length !== 0) { throw new AggregateError(${errorsAlias}, 'Invalid value for path \"${path}\"'); }`);\n }\n return code.join('\\n');\n } else if (typeof schema === 'object' && schema !== null) {\n if (schema instanceof RegExp) {\n const valueAlias = context.unique('v');\n return `\nconst ${valueAlias} = ${valuePath};\nif (!${schema.toString()}.test('' + ${valueAlias})) { throw new ctx.Error(${valueAlias}, 'matching ${schema.toString()}', \\`${path}\\`); }\n`;\n } else {\n const valueAlias = context.unique('v');\n const code: string[] = [\n `const ${valueAlias} = ${valuePath};`,\n `if (${valueAlias} === null || ${valueAlias} === undefined) { throw new ctx.Error(${valueAlias}, 'object', \\`${path}\\`); }`,\n `if (typeof ${valueAlias} !== 'object') { throw new ctx.Error(${valueAlias}, '${schema.constructor.name}', \\`${path}\\`); }`,\n ];\n if ($keys in schema) {\n const keysAlias = context.unique('key');\n const errorsAlias = context.unique('err');\n const value = context.unique('v');\n code.push(`\nconst ${keysAlias} = Object.keys(${valueAlias});\nconst ${errorsAlias} = ${keysAlias}.flatMap((${value}) => { ${codeGen(schema[$keys], context, value, path)} }).filter(Boolean);\nif (${errorsAlias}.length !== 0) { throw new AggregateError(${errorsAlias}, 'Invalid value for path \"${path}\"'); }\n`);\n }\n if ($values in schema) {\n const vAlias = context.unique('val');\n const valuesAlias = context.unique('vals');\n const errorsAlias = context.unique('err');\n code.push(`{\nconst ${valuesAlias} = Object.values(${valuePath});\nconst ${errorsAlias} = ${valuesAlias}.flatMap((${vAlias}) => { ${codeGen(schema[$values], context, vAlias, path)} }).filter(Boolean);\nif (${errorsAlias}.length !== 0) { throw new AggregateError(${errorsAlias}, 'Invalid value for path \"${path}\"'); }\n}`);\n }\n const keys = Object.keys(schema);\n code.push(...keys.map((key) => codeGen(schema[key], context, `${valueAlias}['${key}']`, `${path}.${key}`)));\n return `{${code.join('\\n')}}`;\n }\n } else if (typeof schema === 'symbol') {\n const index = context.register(schema);\n const valueAlias = context.unique('v');\n const registryAlias = context.unique('r');\n\n return `\nconst ${valueAlias} = ${valuePath};\nconst ${registryAlias} = ctx.registry[${index}];\nif (typeof ${valueAlias} !== 'symbol') { throw new ctx.Error(typeof ${valueAlias}, 'symbol', '${path}', 'type of'); }\nif (${valueAlias} !== ${registryAlias}) { throw new ctx.Error(${valueAlias}.toString(), ${registryAlias}.toString(), '${path}', 'symbol'); }\n `;\n } else if (schema === null || schema === undefined) {\n const valueAlias = context.unique('v');\n return `\nconst ${valueAlias} = ${valuePath};\nif (${valueAlias} !== null && ${valueAlias} !== undefined ) { throw new ctx.Error(${valueAlias}, 'nullable', '${path}'); }\n `;\n } else {\n const valueAlias = context.unique('v');\n const typeAlias = context.unique('t');\n const value = context.unique('val');\n return `\nconst ${valueAlias} = ${valuePath};\nconst ${typeAlias} = '${typeof schema}';\nconst ${value} = ${JSON.stringify(schema)};\nif (typeof ${valueAlias} !== ${typeAlias}) { throw new ctx.Error(typeof ${valueAlias}, ${typeAlias}, '${path}', 'type of'); }\nif (${valueAlias} !== ${value}) { throw new ctx.Error(${valueAlias}, ${value}, '${path}'); }\n`;\n }\n};\n\nconst flatAggregateError = (error: AggregateError): AssertError[] => {\n return error.errors.flatMap((e) => (e instanceof AggregateError ? flatAggregateError(e) : e));\n};\n\nexport const compile = <S>(schema: S, rootName: string, options: CompilerOptions = {}) => {\n const context = new Context(options);\n const code = codeGen(schema, context, 'data', rootName);\n const validator = new Function('ctx', 'data', code);\n return (data: SchemaData<S>) => {\n try {\n validator(context, data);\n } catch (e) {\n const errors = e instanceof AggregateError ? flatAggregateError(e) : [e];\n throw new AggregateError(errors, 'Validation failure');\n }\n };\n};\n\nconst assert = (target: unknown, schema: unknown, path: string): AssertError[] => {\n if (schema instanceof And) {\n return schema.schemas.flatMap((schema) => assert(target, schema, path)).filter((error) => !!error);\n } else if (schema instanceof Or) {\n const errors = schema.schemas.flatMap((schema) => assert(target, schema, path));\n const filteredErrors = errors.filter((error) => !!error);\n if (filteredErrors.length === schema.schemas.length) {\n return filteredErrors;\n }\n } else if (schema instanceof Optional) {\n if (target !== undefined && target !== null) {\n return assert(target, schema.schemas[0], path);\n }\n } else if (schema instanceof Tuple) {\n if (!Array.isArray(target)) {\n return [new AssertError(target, 'array', path)];\n }\n return schema.schemas.flatMap((s, idx) => assert(target[idx], s, `${path}[${idx}]`)).filter((error) => !!error);\n } else if (typeof schema === 'function') {\n if (target === null || target === undefined) {\n return [new AssertError(target, 'a non-nullable', path)];\n }\n if (typeof target === 'object' && !(target instanceof schema)) {\n return [new AssertError(target?.constructor?.name, `instance of ${schema.name}`, path, 'instance of')];\n }\n if (typeof target !== 'object' && target?.constructor !== schema) {\n return [new AssertError(target?.constructor?.name, schema.name, path, 'type')];\n }\n } else if (Array.isArray(schema)) {\n if (!Array.isArray(target)) {\n return [new AssertError(target, 'array', path)];\n }\n return schema.flatMap((s) => target.flatMap((value, idx) => assert(value, s, `${path}[${idx}]`))).filter((error) => !!error);\n } else if (typeof schema === 'object' && schema !== null) {\n if (schema instanceof RegExp) {\n if (!schema.test('' + target)) {\n return [new AssertError(target, `matching ${schema.toString()}`, path)];\n }\n return [];\n } else {\n if (target === null || target === undefined) {\n return [new AssertError(target, 'object', path)];\n }\n if (typeof target !== 'object') {\n return [new AssertError(target, schema.constructor.name, path)];\n }\n if ($keys in schema) {\n const targetKeys = Object.keys(target);\n return targetKeys.flatMap((key) => assert(key, schema[$keys], path)).filter((error) => !!error);\n }\n if ($values in schema) {\n const targetKeys = Object.keys(target);\n return targetKeys.flatMap((key) => assert(target[key as keyof typeof target], schema[$values], path)).filter((error) => !!error);\n }\n return Object.keys(schema)\n .flatMap((key) => assert(target[key as keyof typeof target], schema[key as keyof typeof target], path))\n .filter((error) => !!error);\n }\n } else if (schema === null || schema === undefined) {\n if (target !== null && target !== undefined) {\n return [new AssertError(target, 'nullable', path)];\n }\n } else if (target !== schema) {\n return [new AssertError(target, schema, path)];\n }\n return [];\n};\n\nexport const ascertain = <T extends Data = DataValue>(schema: Schema<T>, data: T, rootName = '[root]') => {\n const result = assert(data, schema, rootName).filter((error) => !!error);\n if (result.length > 0) {\n throw new AggregateError(result, 'Validation failure');\n }\n};\n"],"names":["AssertError","TypeError","constructor","value","expected","path","subject","JSON","stringify","Operation","schemas","length","name","Or","or","And","and","Optional","schema","optional","Tuple","tuple","$keys","Symbol","for","$values","fromBase64","Buffer","atob","from","toString","MULTIPLIERS","ms","s","m","h","d","w","as","string","undefined","number","result","parseFloat","Number","isFinite","date","Date","parse","time","matches","match","amount","unit","parseInt","boolean","test","array","delimiter","split","json","e","base64","Context","Error","registry","varIndex","options","error","register","includes","push","indexOf","unique","prefix","codeGen","context","valuePath","valueAlias","errorsAlias","code","map","join","reduceRight","idx","index","registryAlias","Array","isArray","key","RegExp","keysAlias","vAlias","valuesAlias","keys","Object","typeAlias","flatAggregateError","errors","flatMap","AggregateError","compile","rootName","validator","Function","data","assert","target","filter","filteredErrors","targetKeys","ascertain"],"mappings":"AAAA,OAAO,MAAMA,oBAAoBC;;;;;IAC/BC,YACE,AAAgBC,KAAc,EAC9B,AAAgBC,QAAiB,EACjC,AAAgBC,IAAY,EAC5B,AAAgBC,UAAU,OAAO,CACjC;QACA,KAAK,CAAC,CAAC,QAAQ,EAAEA,QAAQ,CAAC,EAAEC,KAAKC,SAAS,CAACL,OAAO,UAAU,EAAEE,KAAK,WAAW,EAAED,S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QAAQ;gBACrB,MAAM4F,aAAaf,OAAOD,IAAI,CAACa;gBAC/B,OAAOG,WAAWX,OAAO,CAAC,CAACV,MAAQiB,OAAOC,MAAM,CAAClB,IAA2B,EAAEvE,MAAM,CAACO,QAAQ,EAAEpB,OAAOuG,MAAM,CAAC,CAACxC,QAAU,CAAC,CAACA;YAC5H;YACA,OAAO2B,OAAOD,IAAI,CAAC5E,QAChBiF,OAAO,CAAC,CAACV,MAAQiB,OAAOC,MAAM,CAAClB,IAA2B,EAAEvE,MAAM,CAACuE,IAA2B,EAAEpF,OAChGuG,MAAM,CAAC,CAACxC,QAAU,CAAC,CAACA;QACzB;IACF,OAAO,IAAIlD,WAAW,QAAQA,WAAWsB,WAAW;QAClD,IAAImE,WAAW,QAAQA,WAAWnE,WAAW;YAC3C,OAAO;gBAAC,IAAIxC,YAAY2G,QAAQ,YAAYtG;aAAM;QACpD;IACF,OAAO,IAAIsG,WAAWzF,QAAQ;QAC5B,OAAO;YAAC,IAAIlB,YAAY2G,QAAQzF,QAAQb;SAAM;IAChD;IACA,OAAO,EAAE;AACX;AAEA,OAAO,MAAM0G,YAAY,CAA6B7F,QAAmBuF,MAASH,WAAW,QAAQ;IACnG,MAAM5D,SAASgE,OAAOD,MAAMvF,QAAQoF,UAAUM,MAAM,CAAC,CAACxC,QAAU,CAAC,CAACA;IAClE,IAAI1B,OAAO/B,MAAM,GAAG,GAAG;QACrB,MAAM,IAAIyF,eAAe1D,QAAQ;IACnC;AACF,EAAE"}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["abstract class Operator<T> {\n constructor(public readonly schemas: Schema<T>[]) {\n if (schemas.length === 0) {\n throw new TypeError(`Operation schema ${this.constructor.name} must have at least one element`);\n }\n }\n}\n\nexport const $keys = Symbol.for('@@keys');\nexport const $values = Symbol.for('@@values');\nexport const $strict = Symbol.for('@@strict');\n\nexport type Schema<T> =\n T extends Record<string | number | symbol, unknown>\n ? { [K in keyof T]?: Schema<T[K]> | unknown } & { [$keys]?: Schema<keyof T> } & { [$values]?: Schema<T[keyof T]> } & { [$strict]?: boolean }\n : T extends Array<infer A>\n ? Schema<A>[] | unknown\n : unknown;\n\nclass Or<T> extends Operator<T> {}\nexport const or = <T>(...schemas: Schema<T>[]) => new Or(schemas);\n\nclass And<T> extends Operator<T> {}\nexport const and = <T>(...schemas: Schema<T>[]) => new And(schemas);\n\nclass Optional<T> extends Operator<T> {\n constructor(schema: Schema<T>) {\n super([schema]);\n }\n}\nexport const optional = <T>(schema: Schema<T>) => new Optional(schema);\n\nclass Tuple<T> extends Operator<T> {}\nexport const tuple = <T>(...schemas: Schema<T>[]) => new Tuple(schemas);\n\nexport const fromBase64 = typeof Buffer === 'undefined' ? (value: string) => atob(value) : (value: string) => Buffer.from(value, 'base64').toString('utf-8');\n\nconst MULTIPLIERS = {\n ms: 1,\n s: 1000,\n m: 60000,\n h: 3600000,\n d: 86400000,\n w: 604800000,\n};\n\nexport const as = {\n string: (value: string | undefined) => {\n return typeof value === 'string' ? value : undefined;\n },\n number: (value: string | undefined) => {\n const result = parseFloat(value as string);\n return Number.isFinite(result) ? result : undefined;\n },\n date: (value: string | undefined) => {\n const result = Date.parse(value as string);\n return Number.isFinite(result) ? new Date(result) : undefined;\n },\n time: (value: string | undefined) => {\n const matches = value?.match(/^(\\d+)(ms|s|m|h|d|w)?$/);\n if (matches) {\n const [, amount, unit = 'ms'] = matches;\n return parseInt(amount, 10) * MULTIPLIERS[unit as keyof typeof MULTIPLIERS];\n }\n return undefined;\n },\n boolean: (value: string | undefined) =>\n /^(0|1|true|false|enabled|disabled)$/i.test(value as string) ? /^(1|true|enabled)$/i.test(value as string) : undefined,\n array: (value: string | undefined, delimiter: string) => value?.split?.(delimiter) ?? undefined,\n json: (value: string | undefined) => {\n try {\n return JSON.parse(value as string);\n } catch (e) {\n return undefined;\n }\n },\n base64: (value: string | undefined) => {\n try {\n return fromBase64(value as string);\n } catch (e) {\n return undefined;\n }\n },\n};\n\nexport interface ErrorFormatter {\n (value: unknown, expected: unknown, path: string, subject: string): string;\n}\n\nexport const formatError: ErrorFormatter = (value, expected, path, subject) =>\n `Invalid ${subject} ${JSON.stringify(value)} for path ${path}, expected ${expected}.`;\n\nclass Context {\n public readonly registry: unknown[] = [];\n private varIndex = 0;\n\n register(value: unknown): number {\n if (!this.registry.includes(value)) {\n this.registry.push(value);\n }\n return this.registry.indexOf(value);\n }\n\n unique(prefix: string) {\n return `${prefix}$$${this.varIndex++}`;\n }\n}\n\nconst codeGen = <T>(schema: Schema<T>, context: Context, valuePath: string, path: string): string => {\n if (schema instanceof And) {\n const valueAlias = context.unique('v');\n const errorsAlias = context.unique('err');\n const code = schema.schemas.map((s) => `try { ${codeGen(s, context, valueAlias, path)} } catch (e) { ${errorsAlias}.push(e.message); }`).join('\\n');\n return `// And\n const ${errorsAlias} = [];\n const ${valueAlias} = ${valuePath};\n ${code}\n if (${errorsAlias}.length !== 0) { throw new TypeError(${errorsAlias}.join('\\\\n')); }\n`;\n } else if (schema instanceof Or) {\n const valueAlias = context.unique('v');\n const errorsAlias = context.unique('err');\n const code = schema.schemas\n .map((s) => codeGen(s, context, valueAlias, path))\n .reduceRight((result, code) => `try {${code}} catch (e) {${errorsAlias}.push(e.message);${result}}`, `throw new TypeError(${errorsAlias}.join('\\\\n'));`);\n return `// Or\nconst ${errorsAlias} = [];\nconst ${valueAlias} = ${valuePath};\n${code}\n `;\n } else if (schema instanceof Optional) {\n const valueAlias = context.unique('v');\n return `// Optional\nconst ${valueAlias} = ${valuePath};\nif (${valueAlias} !== undefined && ${valueAlias} !== null) { ${codeGen(schema.schemas[0], context, valueAlias, path)} }\n`;\n } else if (schema instanceof Tuple) {\n const valueAlias = context.unique('v');\n const errorsAlias = context.unique('err');\n const code: string[] = [\n '// Tuple',\n `const ${valueAlias} = ${valuePath};`,\n `const ${errorsAlias} = [];`,\n `if (${valueAlias} === null || ${valueAlias} === undefined) { throw new TypeError(\\`Invalid value \\${${valueAlias}} for path \"${path}\", expected non-nullable.\\`); }`,\n `if (typeof ${valueAlias} !== 'object') { throw new TypeError(\\`Invalid type \\${typeof ${valueAlias}} for path \"${path}\", expected an instance of Array\\`); }`,\n `if (!Array.isArray(${valueAlias})) { throw new TypeError(\\`Invalid instance of \\${${valueAlias}.constructor?.name} for path \"${path}\", expected an instance of Array.\\`); }`,\n `if (${valueAlias}.length > ${schema.schemas.length}) { throw new TypeError(\\`Invalid tuple length \\${${valueAlias}.length} for path \"${path}\", expected ${schema.schemas.length}.\\`); }`,\n ...schema.schemas.map(\n (s, idx) => `try { ${codeGen(s, context, `${valueAlias}[${idx}]`, `${path}[${idx}]`)} } catch (e) { ${errorsAlias}.push(e.message); }`,\n ),\n `if (${errorsAlias}.length !== 0) { throw new TypeError(${errorsAlias}.join('\\\\n')); }`,\n ];\n return code.join('\\n');\n } else if (typeof schema === 'function') {\n const index = context.register(schema);\n const valueAlias = context.unique('v');\n const registryAlias = context.unique('r');\n return `\nconst ${valueAlias} = ${valuePath};\nconst ${registryAlias} = ctx.registry[${index}];\nif (${valueAlias} === null || ${valueAlias} === undefined) { throw new TypeError(\\`Invalid value \\${${valueAlias}} for path \"${path}\", expected non-nullable.\\`); }\nif (typeof ${valueAlias} === 'object' && !(${valueAlias} instanceof ${registryAlias})) { throw new TypeError(\\`Invalid instance of \\${${valueAlias}?.constructor?.name} for path \"${path}\", expected an instance of ${schema?.name}\\`); }\nif (typeof ${valueAlias} !== 'object' && ${valueAlias}?.constructor !== ${registryAlias}) { throw new TypeError(\\`Invalid type \\${${valueAlias}?.constructor?.name} for path \"${path}\", expected type ${schema?.name}\\`); }\n`;\n } else if (Array.isArray(schema)) {\n const valueAlias = context.unique('v');\n const code: string[] = [\n `const ${valueAlias} = ${valuePath};`,\n `if (${valueAlias} === null || ${valueAlias} === undefined) { throw new TypeError(\\`Invalid value \\${${valueAlias}} for path \"${path}\", expected non-nullable.\\`); }`,\n `if (typeof ${valueAlias} !== 'object') { throw new TypeError(\\`Invalid type \\${typeof ${valueAlias}} for path \"${path}\", expected an instance of Array.\\`); }`,\n `if (!Array.isArray(${valueAlias})) { throw new TypeError(\\`Invalid instance of \\${${valueAlias}.constructor?.name} for path \"${path}\", expected an instance of Array.\\`); }`,\n ];\n if (schema.length > 0) {\n const value = context.unique('val');\n const key = context.unique('key');\n const errorsAlias = context.unique('err');\n code.push(`const ${errorsAlias} = [];`);\n code.push(\n ...schema.map(\n (s) =>\n `${valueAlias}.forEach((${value},${key}) => { try { ${codeGen(s, context, value, `${path}[\\${${key}}]`)} } catch(e){ ${errorsAlias}.push(e.message); } });`,\n ),\n );\n\n code.push(`if (${errorsAlias}.length !== 0) { throw new TypeError(${errorsAlias}.join('\\\\n')); }`);\n }\n return code.join('\\n');\n } else if (typeof schema === 'object' && schema !== null) {\n if (schema instanceof RegExp) {\n const valueAlias = context.unique('v');\n return `\nconst ${valueAlias} = ${valuePath};\nif (!${schema.toString()}.test('' + ${valueAlias})) { throw new TypeError(\\`Invalid value \\${${valueAlias}} for path \"${path}\", expected to match ${schema.toString()}\\`); }\n`;\n } else {\n const valueAlias = context.unique('v');\n const code: string[] = [\n `const ${valueAlias} = ${valuePath};`,\n `if (${valueAlias} === null || ${valueAlias} === undefined) { throw new TypeError(\\`Invalid value \\${${valueAlias}} for path \"${path}\", expected non-nullable.\\`); }`,\n `if (typeof ${valueAlias} !== 'object') { throw new TypeError(\\`Invalid type \\${typeof ${valueAlias}} for path \"${path}\", expected type is object.\\`); }`,\n ];\n if ($keys in schema) {\n const keysAlias = context.unique('k');\n const errorsAlias = context.unique('err');\n const kAlias = context.unique('k');\n code.push(`\nconst ${keysAlias} = Object.keys(${valueAlias});\nconst ${errorsAlias} = ${keysAlias}.map(${kAlias} => { try { ${codeGen(schema[$keys], context, kAlias, `${path}[\\${${kAlias}}]`)} } catch (e) { return e.message; } }).filter(Boolean);\nif (${errorsAlias}.length !== 0) { throw new TypeError(${errorsAlias}.join('\\\\n')); }\n`);\n }\n if ($values in schema) {\n const vAlias = context.unique('val');\n const kAlias = context.unique('k');\n const entriesAlias = context.unique('en');\n const errorsAlias = context.unique('err');\n code.push(`\nconst ${entriesAlias} = Object.entries(${valueAlias});\nconst ${errorsAlias} = ${entriesAlias}.map(([${kAlias},${vAlias}]) => { try { ${codeGen(schema[$values], context, vAlias, `${path}[\\${${kAlias}}]`)} } catch (e) { return e.message; } }).filter(Boolean);\nif (${errorsAlias}.length !== 0) { throw new TypeError(${errorsAlias}.join('\\\\n')); }\n`);\n }\n if ($strict in schema && schema[$strict]) {\n const keysAlias = context.unique('k');\n const kAlias = context.unique('k');\n const extraAlias = context.unique('ex');\n code.push(`const ${keysAlias} = new Set(${JSON.stringify(Object.keys(schema))});`);\n code.push(`const ${extraAlias} = Object.keys(${valueAlias}).filter(${kAlias} => !${keysAlias}.has(${kAlias}));`);\n code.push(`if (${extraAlias}.length !== 0) { throw new TypeError(\\`Extra properties: \\${${extraAlias}}, are not allowed for path \"${path}\"\\`); }`);\n }\n code.push(...Object.entries(schema).map(([key, s]) => codeGen(s, context, `${valueAlias}['${key}']`, `${path}.${key}`)));\n return `${code.join('\\n')}`;\n }\n } else if (typeof schema === 'symbol') {\n const index = context.register(schema);\n const valueAlias = context.unique('v');\n const registryAlias = context.unique('r');\n\n return `\nconst ${valueAlias} = ${valuePath};\nconst ${registryAlias} = ctx.registry[${index}];\nif (typeof ${valueAlias} !== 'symbol') { throw new TypeError(\\`Invalid type \\${typeof ${valueAlias}} for \"path\", expected symbol\\`); }\nif (${valueAlias} !== ${registryAlias}) { throw new TypeError(\\`Invalid value \\${${valueAlias}.toString()} for path \"${path}\", expected ${schema.toString()}\\`); }\n `;\n } else if (schema === null || schema === undefined) {\n const valueAlias = context.unique('v');\n return `\nconst ${valueAlias} = ${valuePath};\nif (${valueAlias} !== null && ${valueAlias} !== undefined ) { throw new TypeError(\\`Invalid value ${valueAlias} for path \"${path}\", expected nullable\\`); }\n `;\n } else {\n const valueAlias = context.unique('v');\n const value = context.unique('val');\n return `\nconst ${valueAlias} = ${valuePath};\nconst ${value} = ${JSON.stringify(schema)};\nif (typeof ${valueAlias} !== '${typeof schema}') { throw new TypeError(\\`Invalid type \\${typeof ${valueAlias}} for path \"${path}\", expected ${typeof schema}\\`); }\nif (${valueAlias} !== ${value}) { throw new TypeError(\\`Invalid value ${JSON.stringify(valueAlias)} for path \"${path}\", expected ${JSON.stringify(schema)}\\`); }\n`;\n }\n};\n\nexport const compile = <T>(schema: Schema<T>, rootName: string) => {\n const context = new Context();\n const code = codeGen(schema, context, 'data', rootName);\n const validator = new Function('ctx', 'data', code);\n return (data: T) => validator(context, data);\n};\n\nexport const ascertain = <T>(schema: Schema<T>, data: T, rootName = '[root]') => {\n compile(schema, rootName)(data);\n};\n"],"names":["Operator","constructor","schemas","length","TypeError","name","$keys","Symbol","for","$values","$strict","Or","or","And","and","Optional","schema","optional","Tuple","tuple","fromBase64","Buffer","value","atob","from","toString","MULTIPLIERS","ms","s","m","h","d","w","as","string","undefined","number","result","parseFloat","Number","isFinite","date","Date","parse","time","matches","match","amount","unit","parseInt","boolean","test","array","delimiter","split","json","JSON","e","base64","formatError","expected","path","subject","stringify","Context","registry","varIndex","register","includes","push","indexOf","unique","prefix","codeGen","context","valuePath","valueAlias","errorsAlias","code","map","join","reduceRight","idx","index","registryAlias","Array","isArray","key","RegExp","keysAlias","kAlias","vAlias","entriesAlias","extraAlias","Object","keys","entries","compile","rootName","validator","Function","data","ascertain"],"mappings":"AAAA,MAAeA;;IACbC,YAAY,AAAgBC,OAAoB,CAAE;aAAtBA,UAAAA;QAC1B,IAAIA,QAAQC,MAAM,KAAK,GAAG;YACxB,MAAM,IAAIC,UAAU,CAAC,iBAAiB,EAAE,IAAI,CAACH,WAAW,CAACI,IAAI,CAAC,+BAA+B,CAAC;QAChG;IACF;AACF;AAEA,OAAO,MAAMC,QAAQC,OAAOC,GAAG,CAAC,UAAU;AAC1C,OAAO,MAAMC,UAAUF,OAAOC,GAAG,CAAC,YAAY;AAC9C,OAAO,MAAME,UA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AAQ;AAC1J,CAAC;IACC;AACF;AAEA,OAAO,MAAMiF,UAAU,CAAIjF,QAAmBkF;IAC5C,MAAMxB,UAAU,IAAIV;IACpB,MAAMc,OAAOL,QAAQzD,QAAQ0D,SAAS,QAAQwB;IAC9C,MAAMC,YAAY,IAAIC,SAAS,OAAO,QAAQtB;IAC9C,OAAO,CAACuB,OAAYF,UAAUzB,SAAS2B;AACzC,EAAE;AAEF,OAAO,MAAMC,YAAY,CAAItF,QAAmBqF,MAASH,WAAW,QAAQ;IAC1ED,QAAQjF,QAAQkF,UAAUG;AAC5B,EAAE"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ascertain",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "0-Deps, simple, fast, for browser and node js object schema validator",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "build/index.d.ts",
|
|
@@ -14,13 +14,6 @@
|
|
|
14
14
|
"build",
|
|
15
15
|
"src/index.js"
|
|
16
16
|
],
|
|
17
|
-
"scripts": {
|
|
18
|
-
"build": "rm -rf build && NODE_ENV=production inop src/ build -i __tests__ && tsc --declaration --emitDeclarationOnly",
|
|
19
|
-
"lint": "eslint src",
|
|
20
|
-
"test": "NODE_ENV=test jest --bail",
|
|
21
|
-
"test:cov": "CI=1 NODE_ENV=test jest",
|
|
22
|
-
"prepare": "husky"
|
|
23
|
-
},
|
|
24
17
|
"repository": {
|
|
25
18
|
"type": "git",
|
|
26
19
|
"url": "git+https://github.com/3axap4eHko/ascertain.git"
|
|
@@ -65,5 +58,10 @@
|
|
|
65
58
|
"ts-node": "^10.9.2",
|
|
66
59
|
"typescript": "^5.5.3"
|
|
67
60
|
},
|
|
68
|
-
"
|
|
69
|
-
|
|
61
|
+
"scripts": {
|
|
62
|
+
"build": "rm -rf build && NODE_ENV=production inop src/ build -i __tests__ && tsc --declaration --emitDeclarationOnly",
|
|
63
|
+
"lint": "eslint src",
|
|
64
|
+
"test": "NODE_ENV=test jest --bail",
|
|
65
|
+
"test:cov": "CI=1 NODE_ENV=test jest"
|
|
66
|
+
}
|
|
67
|
+
}
|