@sinclair/typebox 0.25.24 → 0.26.0-dev

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.
Files changed (46) hide show
  1. package/compiler/compiler.d.ts +9 -4
  2. package/compiler/compiler.js +160 -122
  3. package/errors/errors.d.ts +56 -46
  4. package/errors/errors.js +234 -153
  5. package/package.json +1 -6
  6. package/readme.md +294 -207
  7. package/system/system.d.ts +9 -6
  8. package/system/system.js +17 -17
  9. package/typebox.d.ts +386 -162
  10. package/typebox.js +1710 -229
  11. package/value/cast.d.ts +2 -2
  12. package/value/cast.js +121 -188
  13. package/value/check.d.ts +1 -1
  14. package/value/check.js +156 -111
  15. package/value/convert.d.ts +13 -0
  16. package/value/convert.js +345 -0
  17. package/value/create.d.ts +6 -2
  18. package/value/create.js +149 -97
  19. package/{hash → value}/hash.js +39 -14
  20. package/value/index.d.ts +1 -0
  21. package/value/index.js +3 -1
  22. package/value/value.d.ts +2 -8
  23. package/value/value.js +20 -14
  24. package/conditional/conditional.d.ts +0 -17
  25. package/conditional/conditional.js +0 -91
  26. package/conditional/index.d.ts +0 -2
  27. package/conditional/index.js +0 -45
  28. package/conditional/structural.d.ts +0 -11
  29. package/conditional/structural.js +0 -685
  30. package/custom/custom.d.ts +0 -12
  31. package/custom/custom.js +0 -55
  32. package/custom/index.d.ts +0 -1
  33. package/custom/index.js +0 -44
  34. package/format/format.d.ts +0 -12
  35. package/format/format.js +0 -55
  36. package/format/index.d.ts +0 -1
  37. package/format/index.js +0 -44
  38. package/guard/extends.d.ts +0 -10
  39. package/guard/extends.js +0 -50
  40. package/guard/guard.d.ts +0 -60
  41. package/guard/guard.js +0 -440
  42. package/guard/index.d.ts +0 -2
  43. package/guard/index.js +0 -45
  44. package/hash/index.d.ts +0 -1
  45. package/hash/index.js +0 -44
  46. /package/{hash → value}/hash.d.ts +0 -0
@@ -1,12 +1,11 @@
1
- import { ValueError } from '../errors/index';
2
1
  import * as Types from '../typebox';
2
+ import { ValueError } from '../errors/index';
3
3
  export type CheckFunction = (value: unknown) => boolean;
4
4
  export declare class TypeCheck<T extends Types.TSchema> {
5
5
  private readonly schema;
6
- private readonly references;
7
6
  private readonly checkFunc;
8
7
  private readonly code;
9
- constructor(schema: T, references: Types.TSchema[], checkFunc: CheckFunction, code: string);
8
+ constructor(schema: T, checkFunc: CheckFunction, code: string);
10
9
  /** Returns the generated validation code used to validate this type. */
11
10
  Code(): string;
12
11
  /** Returns an iterator for each error in this value. */
@@ -21,8 +20,14 @@ export declare class TypeCompilerUnknownTypeError extends Error {
21
20
  readonly schema: Types.TSchema;
22
21
  constructor(schema: Types.TSchema);
23
22
  }
23
+ export declare class TypeCompilerPreflightCheckError extends Error {
24
+ readonly schema: Types.TSchema;
25
+ constructor(schema: Types.TSchema);
26
+ }
24
27
  /** Compiles Types for Runtime Type Checking */
25
28
  export declare namespace TypeCompiler {
29
+ /** Returns the generated validation code used to validate this type. */
30
+ function Code<T extends Types.TSchema>(schema: T): string;
26
31
  /** Compiles the given type for runtime type checking. This compiler only accepts known TypeBox types non-inclusive of unsafe types. */
27
- function Compile<T extends Types.TSchema>(schema: T, references?: Types.TSchema[]): TypeCheck<T>;
32
+ function Compile<T extends Types.TSchema>(schema: T): TypeCheck<T>;
28
33
  }
@@ -27,21 +27,17 @@ THE SOFTWARE.
27
27
 
28
28
  ---------------------------------------------------------------------------*/
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.TypeCompiler = exports.TypeCompilerUnknownTypeError = exports.MemberExpression = exports.TypeCheck = void 0;
30
+ exports.TypeCompiler = exports.TypeCompilerPreflightCheckError = exports.TypeCompilerUnknownTypeError = exports.MemberExpression = exports.TypeCheck = void 0;
31
+ const Types = require("../typebox");
31
32
  const index_1 = require("../errors/index");
32
33
  const index_2 = require("../system/index");
33
- const index_3 = require("../guard/index");
34
- const index_4 = require("../format/index");
35
- const index_5 = require("../custom/index");
36
- const index_6 = require("../hash/index");
37
- const Types = require("../typebox");
34
+ const hash_1 = require("../value/hash");
38
35
  // -------------------------------------------------------------------
39
36
  // TypeCheck
40
37
  // -------------------------------------------------------------------
41
38
  class TypeCheck {
42
- constructor(schema, references, checkFunc, code) {
39
+ constructor(schema, checkFunc, code) {
43
40
  this.schema = schema;
44
- this.references = references;
45
41
  this.checkFunc = checkFunc;
46
42
  this.code = code;
47
43
  }
@@ -51,7 +47,7 @@ class TypeCheck {
51
47
  }
52
48
  /** Returns an iterator for each error in this value. */
53
49
  Errors(value) {
54
- return index_1.ValueErrors.Errors(this.schema, this.references, value);
50
+ return index_1.ValueErrors.Errors(this.schema, value);
55
51
  }
56
52
  /** Returns true if the value matches the compiled type. */
57
53
  Check(value) {
@@ -138,220 +134,260 @@ class TypeCompilerUnknownTypeError extends Error {
138
134
  }
139
135
  }
140
136
  exports.TypeCompilerUnknownTypeError = TypeCompilerUnknownTypeError;
137
+ class TypeCompilerPreflightCheckError extends Error {
138
+ constructor(schema) {
139
+ super('TypeCompiler: Preflight validation check failed for given schema');
140
+ this.schema = schema;
141
+ }
142
+ }
143
+ exports.TypeCompilerPreflightCheckError = TypeCompilerPreflightCheckError;
141
144
  /** Compiles Types for Runtime Type Checking */
142
145
  var TypeCompiler;
143
146
  (function (TypeCompiler) {
144
147
  // -------------------------------------------------------------------
145
148
  // Guards
146
149
  // -------------------------------------------------------------------
150
+ function IsBigInt(value) {
151
+ return typeof value === 'bigint';
152
+ }
147
153
  function IsNumber(value) {
148
- return typeof value === 'number' && !globalThis.isNaN(value);
154
+ return typeof value === 'number' && globalThis.Number.isFinite(value);
149
155
  }
150
156
  // -------------------------------------------------------------------
151
157
  // Types
152
158
  // -------------------------------------------------------------------
153
159
  function* Any(schema, value) {
154
- yield '(true)';
160
+ yield 'true';
155
161
  }
156
162
  function* Array(schema, value) {
157
163
  const expression = CreateExpression(schema.items, 'value');
158
- yield `(Array.isArray(${value}) && ${value}.every(value => ${expression}))`;
164
+ yield `Array.isArray(${value}) && ${value}.every(value => ${expression})`;
159
165
  if (IsNumber(schema.minItems))
160
- yield `(${value}.length >= ${schema.minItems})`;
166
+ yield `${value}.length >= ${schema.minItems}`;
161
167
  if (IsNumber(schema.maxItems))
162
- yield `(${value}.length <= ${schema.maxItems})`;
168
+ yield `${value}.length <= ${schema.maxItems}`;
163
169
  if (schema.uniqueItems === true)
164
170
  yield `((function() { const set = new Set(); for(const element of ${value}) { const hashed = hash(element); if(set.has(hashed)) { return false } else { set.add(hashed) } } return true })())`;
165
171
  }
172
+ function* BigInt(schema, value) {
173
+ yield `(typeof ${value} === 'bigint')`;
174
+ if (IsBigInt(schema.multipleOf))
175
+ yield `(${value} % BigInt(${schema.multipleOf})) === 0`;
176
+ if (IsBigInt(schema.exclusiveMinimum))
177
+ yield `${value} > BigInt(${schema.exclusiveMinimum})`;
178
+ if (IsBigInt(schema.exclusiveMaximum))
179
+ yield `${value} < BigInt(${schema.exclusiveMaximum})`;
180
+ if (IsBigInt(schema.minimum))
181
+ yield `${value} >= BigInt(${schema.minimum})`;
182
+ if (IsBigInt(schema.maximum))
183
+ yield `${value} <= BigInt(${schema.maximum})`;
184
+ }
166
185
  function* Boolean(schema, value) {
167
- yield `(typeof ${value} === 'boolean')`;
186
+ yield `typeof ${value} === 'boolean'`;
168
187
  }
169
188
  function* Constructor(schema, value) {
170
189
  yield* Visit(schema.returns, `${value}.prototype`);
171
190
  }
172
191
  function* Date(schema, value) {
173
- yield `(${value} instanceof Date) && !isNaN(${value}.getTime())`;
192
+ yield `(${value} instanceof Date) && Number.isFinite(${value}.getTime())`;
174
193
  if (IsNumber(schema.exclusiveMinimumTimestamp))
175
- yield `(${value}.getTime() > ${schema.exclusiveMinimumTimestamp})`;
194
+ yield `${value}.getTime() > ${schema.exclusiveMinimumTimestamp}`;
176
195
  if (IsNumber(schema.exclusiveMaximumTimestamp))
177
- yield `(${value}.getTime() < ${schema.exclusiveMaximumTimestamp})`;
196
+ yield `${value}.getTime() < ${schema.exclusiveMaximumTimestamp}`;
178
197
  if (IsNumber(schema.minimumTimestamp))
179
- yield `(${value}.getTime() >= ${schema.minimumTimestamp})`;
198
+ yield `${value}.getTime() >= ${schema.minimumTimestamp}`;
180
199
  if (IsNumber(schema.maximumTimestamp))
181
- yield `(${value}.getTime() <= ${schema.maximumTimestamp})`;
200
+ yield `${value}.getTime() <= ${schema.maximumTimestamp}`;
182
201
  }
183
202
  function* Function(schema, value) {
184
- yield `(typeof ${value} === 'function')`;
203
+ yield `typeof ${value} === 'function'`;
185
204
  }
186
205
  function* Integer(schema, value) {
187
206
  yield `(typeof ${value} === 'number' && Number.isInteger(${value}))`;
188
207
  if (IsNumber(schema.multipleOf))
189
- yield `(${value} % ${schema.multipleOf} === 0)`;
208
+ yield `(${value} % ${schema.multipleOf}) === 0`;
190
209
  if (IsNumber(schema.exclusiveMinimum))
191
- yield `(${value} > ${schema.exclusiveMinimum})`;
210
+ yield `${value} > ${schema.exclusiveMinimum}`;
192
211
  if (IsNumber(schema.exclusiveMaximum))
193
- yield `(${value} < ${schema.exclusiveMaximum})`;
212
+ yield `${value} < ${schema.exclusiveMaximum}`;
194
213
  if (IsNumber(schema.minimum))
195
- yield `(${value} >= ${schema.minimum})`;
214
+ yield `${value} >= ${schema.minimum}`;
196
215
  if (IsNumber(schema.maximum))
197
- yield `(${value} <= ${schema.maximum})`;
216
+ yield `${value} <= ${schema.maximum}`;
217
+ }
218
+ function* Intersect(schema, value) {
219
+ if (schema.unevaluatedProperties === undefined) {
220
+ const expressions = schema.allOf.map((schema) => CreateExpression(schema, value));
221
+ yield `${expressions.join(' && ')}`;
222
+ }
223
+ else if (schema.unevaluatedProperties === false) {
224
+ // prettier-ignore
225
+ const schemaKeys = Types.KeyResolver.Resolve(schema).map((key) => `'${key}'`).join(', ');
226
+ const expressions = schema.allOf.map((schema) => CreateExpression(schema, value));
227
+ const expression1 = `Object.getOwnPropertyNames(${value}).every(key => [${schemaKeys}].includes(key))`;
228
+ yield `${expressions.join(' && ')} && ${expression1}`;
229
+ }
230
+ else if (typeof schema.unevaluatedProperties === 'object') {
231
+ // prettier-ignore
232
+ const schemaKeys = Types.KeyResolver.Resolve(schema).map((key) => `'${key}'`).join(', ');
233
+ const expressions = schema.allOf.map((schema) => CreateExpression(schema, value));
234
+ const expression1 = CreateExpression(schema.unevaluatedProperties, 'value[key]');
235
+ const expression2 = `Object.getOwnPropertyNames(${value}).every(key => [${schemaKeys}].includes(key) || ${expression1})`;
236
+ yield `${expressions.join(' && ')} && ${expression2}`;
237
+ }
198
238
  }
199
239
  function* Literal(schema, value) {
200
240
  if (typeof schema.const === 'number' || typeof schema.const === 'boolean') {
201
- yield `(${value} === ${schema.const})`;
241
+ yield `${value} === ${schema.const}`;
202
242
  }
203
243
  else {
204
- yield `(${value} === '${schema.const}')`;
244
+ yield `${value} === '${schema.const}'`;
205
245
  }
206
246
  }
207
247
  function* Never(schema, value) {
208
- yield `(false)`;
248
+ yield `false`;
249
+ }
250
+ function* Not(schema, value) {
251
+ const left = CreateExpression(schema.allOf[0].not, value);
252
+ const right = CreateExpression(schema.allOf[1], value);
253
+ yield `!${left} && ${right}`;
209
254
  }
210
255
  function* Null(schema, value) {
211
- yield `(${value} === null)`;
256
+ yield `${value} === null`;
212
257
  }
213
258
  function* Number(schema, value) {
214
- if (index_2.TypeSystem.AllowNaN) {
215
- yield `(typeof ${value} === 'number')`;
216
- }
217
- else {
218
- yield `(typeof ${value} === 'number' && !isNaN(${value}))`;
219
- }
259
+ yield `typeof ${value} === 'number'`;
260
+ if (!index_2.TypeSystem.AllowNaN)
261
+ yield `Number.isFinite(${value})`;
220
262
  if (IsNumber(schema.multipleOf))
221
- yield `(${value} % ${schema.multipleOf} === 0)`;
263
+ yield `(${value} % ${schema.multipleOf}) === 0`;
222
264
  if (IsNumber(schema.exclusiveMinimum))
223
- yield `(${value} > ${schema.exclusiveMinimum})`;
265
+ yield `${value} > ${schema.exclusiveMinimum}`;
224
266
  if (IsNumber(schema.exclusiveMaximum))
225
- yield `(${value} < ${schema.exclusiveMaximum})`;
267
+ yield `${value} < ${schema.exclusiveMaximum}`;
226
268
  if (IsNumber(schema.minimum))
227
- yield `(${value} >= ${schema.minimum})`;
269
+ yield `${value} >= ${schema.minimum}`;
228
270
  if (IsNumber(schema.maximum))
229
- yield `(${value} <= ${schema.maximum})`;
271
+ yield `${value} <= ${schema.maximum}`;
230
272
  }
231
273
  function* Object(schema, value) {
232
- if (index_2.TypeSystem.AllowArrayObjects) {
233
- yield `(typeof ${value} === 'object' && ${value} !== null)`;
234
- }
235
- else {
236
- yield `(typeof ${value} === 'object' && ${value} !== null && !Array.isArray(${value}))`;
237
- }
274
+ yield `(typeof ${value} === 'object' && ${value} !== null)`;
275
+ if (!index_2.TypeSystem.AllowArrayObjects)
276
+ yield `!Array.isArray(${value})`;
238
277
  if (IsNumber(schema.minProperties))
239
- yield `(Object.getOwnPropertyNames(${value}).length >= ${schema.minProperties})`;
278
+ yield `Object.getOwnPropertyNames(${value}).length >= ${schema.minProperties}`;
240
279
  if (IsNumber(schema.maxProperties))
241
- yield `(Object.getOwnPropertyNames(${value}).length <= ${schema.maxProperties})`;
242
- const propertyKeys = globalThis.Object.getOwnPropertyNames(schema.properties);
243
- if (schema.additionalProperties === false) {
244
- // Optimization: If the property key length matches the required keys length
245
- // then we only need check that the values property key length matches that
246
- // of the property key length. This is because exhaustive testing for values
247
- // will occur in subsequent property tests.
248
- if (schema.required && schema.required.length === propertyKeys.length) {
249
- yield `(Object.getOwnPropertyNames(${value}).length === ${propertyKeys.length})`;
280
+ yield `Object.getOwnPropertyNames(${value}).length <= ${schema.maxProperties}`;
281
+ const schemaKeys = globalThis.Object.getOwnPropertyNames(schema.properties);
282
+ for (const schemaKey of schemaKeys) {
283
+ const memberExpression = MemberExpression.Encode(value, schemaKey);
284
+ const property = schema.properties[schemaKey];
285
+ if (schema.required && schema.required.includes(schemaKey)) {
286
+ yield* Visit(property, memberExpression);
287
+ if (Types.ExtendsUndefined.Check(property))
288
+ yield `('${schemaKey}' in ${value})`;
250
289
  }
251
290
  else {
252
- const keys = `[${propertyKeys.map((key) => `'${key}'`).join(', ')}]`;
253
- yield `(Object.getOwnPropertyNames(${value}).every(key => ${keys}.includes(key)))`;
291
+ const expression = CreateExpression(property, memberExpression);
292
+ yield `('${schemaKey}' in ${value} ? ${expression} : true)`;
254
293
  }
255
294
  }
256
- if (index_3.TypeGuard.TSchema(schema.additionalProperties)) {
257
- const expression = CreateExpression(schema.additionalProperties, 'value[key]');
258
- const keys = `[${propertyKeys.map((key) => `'${key}'`).join(', ')}]`;
259
- yield `(Object.getOwnPropertyNames(${value}).every(key => ${keys}.includes(key) || ${expression}))`;
260
- }
261
- for (const propertyKey of propertyKeys) {
262
- const memberExpression = MemberExpression.Encode(value, propertyKey);
263
- const propertySchema = schema.properties[propertyKey];
264
- if (schema.required && schema.required.includes(propertyKey)) {
265
- yield* Visit(propertySchema, memberExpression);
266
- if (index_3.TypeExtends.Undefined(propertySchema)) {
267
- yield `('${propertyKey}' in ${value})`;
268
- }
295
+ if (schema.additionalProperties === false) {
296
+ if (schema.required && schema.required.length === schemaKeys.length) {
297
+ yield `Object.getOwnPropertyNames(${value}).length === ${schemaKeys.length}`;
269
298
  }
270
299
  else {
271
- const expression = CreateExpression(propertySchema, memberExpression);
272
- yield `(${memberExpression} === undefined ? true : (${expression}))`;
300
+ const keys = `[${schemaKeys.map((key) => `'${key}'`).join(', ')}]`;
301
+ yield `Object.getOwnPropertyNames(${value}).every(key => ${keys}.includes(key))`;
273
302
  }
274
303
  }
304
+ if (typeof schema.additionalProperties === 'object') {
305
+ const expression = CreateExpression(schema.additionalProperties, 'value[key]');
306
+ const keys = `[${schemaKeys.map((key) => `'${key}'`).join(', ')}]`;
307
+ yield `(Object.getOwnPropertyNames(${value}).every(key => ${keys}.includes(key) || ${expression}))`;
308
+ }
275
309
  }
276
310
  function* Promise(schema, value) {
277
311
  yield `(typeof value === 'object' && typeof ${value}.then === 'function')`;
278
312
  }
279
313
  function* Record(schema, value) {
280
- if (index_2.TypeSystem.AllowArrayObjects) {
281
- yield `(typeof ${value} === 'object' && ${value} !== null && !(${value} instanceof Date))`;
282
- }
283
- else {
284
- yield `(typeof ${value} === 'object' && ${value} !== null && !(${value} instanceof Date) && !Array.isArray(${value}))`;
285
- }
314
+ yield `(typeof ${value} === 'object' && ${value} !== null && !(${value} instanceof Date))`;
315
+ if (!index_2.TypeSystem.AllowArrayObjects)
316
+ yield `!Array.isArray(${value})`;
286
317
  const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0];
287
318
  const local = PushLocal(`new RegExp(/${keyPattern}/)`);
288
319
  yield `(Object.getOwnPropertyNames(${value}).every(key => ${local}.test(key)))`;
289
320
  const expression = CreateExpression(valueSchema, 'value');
290
- yield `(Object.values(${value}).every(value => ${expression}))`;
321
+ yield `Object.values(${value}).every(value => ${expression})`;
291
322
  }
292
323
  function* Ref(schema, value) {
293
324
  // Reference: If we have seen this reference before we can just yield and return
294
325
  // the function call. If this isn't the case we defer to visit to generate and
295
326
  // set the function for subsequent passes. Consider for refactor.
296
327
  if (state_local_function_names.has(schema.$ref))
297
- return yield `(${CreateFunctionName(schema.$ref)}(${value}))`;
298
- if (!state_reference_map.has(schema.$ref))
299
- throw Error(`TypeCompiler.Ref: Cannot de-reference schema with $id '${schema.$ref}'`);
300
- const reference = state_reference_map.get(schema.$ref);
301
- yield* Visit(reference, value);
328
+ return yield `${CreateFunctionName(schema.$ref)}(${value})`;
329
+ yield* Visit(Types.ReferenceRegistry.DerefOne(schema), value);
302
330
  }
303
331
  function* Self(schema, value) {
304
332
  const func = CreateFunctionName(schema.$ref);
305
- yield `(${func}(${value}))`;
333
+ yield `${func}(${value})`;
306
334
  }
307
335
  function* String(schema, value) {
308
336
  yield `(typeof ${value} === 'string')`;
309
337
  if (IsNumber(schema.minLength))
310
- yield `(${value}.length >= ${schema.minLength})`;
338
+ yield `${value}.length >= ${schema.minLength}`;
311
339
  if (IsNumber(schema.maxLength))
312
- yield `(${value}.length <= ${schema.maxLength})`;
340
+ yield `${value}.length <= ${schema.maxLength}`;
313
341
  if (schema.pattern !== undefined) {
314
342
  const local = PushLocal(`${new RegExp(schema.pattern)};`);
315
- yield `(${local}.test(${value}))`;
343
+ yield `${local}.test(${value})`;
316
344
  }
317
345
  if (schema.format !== undefined) {
318
- yield `(format('${schema.format}', ${value}))`;
346
+ yield `format('${schema.format}', ${value})`;
319
347
  }
320
348
  }
349
+ function* Symbol(schema, value) {
350
+ yield `(typeof ${value} === 'symbol')`;
351
+ }
321
352
  function* Tuple(schema, value) {
322
353
  yield `(Array.isArray(${value}))`;
323
354
  if (schema.items === undefined)
324
- return yield `(${value}.length === 0)`;
355
+ return yield `${value}.length === 0`;
325
356
  yield `(${value}.length === ${schema.maxItems})`;
326
357
  for (let i = 0; i < schema.items.length; i++) {
327
358
  const expression = CreateExpression(schema.items[i], `${value}[${i}]`);
328
- yield `(${expression})`;
359
+ yield `${expression}`;
329
360
  }
330
361
  }
331
362
  function* Undefined(schema, value) {
332
- yield `(${value} === undefined)`;
363
+ yield `${value} === undefined`;
333
364
  }
334
365
  function* Union(schema, value) {
335
366
  const expressions = schema.anyOf.map((schema) => CreateExpression(schema, value));
336
367
  yield `(${expressions.join(' || ')})`;
337
368
  }
338
369
  function* Uint8Array(schema, value) {
339
- yield `(${value} instanceof Uint8Array)`;
370
+ yield `${value} instanceof Uint8Array`;
340
371
  if (IsNumber(schema.maxByteLength))
341
372
  yield `(${value}.length <= ${schema.maxByteLength})`;
342
373
  if (IsNumber(schema.minByteLength))
343
374
  yield `(${value}.length >= ${schema.minByteLength})`;
344
375
  }
345
376
  function* Unknown(schema, value) {
346
- yield '(true)';
377
+ yield 'true';
347
378
  }
348
379
  function* Void(schema, value) {
349
- yield `(${value} === null)`;
380
+ if (index_2.TypeSystem.AllowVoidNull) {
381
+ yield `(${value} === undefined || ${value} === null)`;
382
+ }
383
+ else {
384
+ yield `${value} === undefined`;
385
+ }
350
386
  }
351
387
  function* UserDefined(schema, value) {
352
388
  const schema_key = `schema_key_${state_remote_custom_types.size}`;
353
389
  state_remote_custom_types.set(schema_key, schema);
354
- yield `(custom('${schema[Types.Kind]}', '${schema_key}', ${value}))`;
390
+ yield `custom('${schema[Types.Kind]}', '${schema_key}', ${value})`;
355
391
  }
356
392
  function* Visit(schema, value) {
357
393
  // Reference: Referenced schemas can originate from either additional schemas
@@ -362,7 +398,7 @@ var TypeCompiler;
362
398
  const name = CreateFunctionName(schema.$id);
363
399
  const body = CreateFunction(name, schema, 'value');
364
400
  PushFunction(body);
365
- yield `(${name}(${value}))`;
401
+ yield `${name}(${value})`;
366
402
  return;
367
403
  }
368
404
  const anySchema = schema;
@@ -371,6 +407,8 @@ var TypeCompiler;
371
407
  return yield* Any(anySchema, value);
372
408
  case 'Array':
373
409
  return yield* Array(anySchema, value);
410
+ case 'BigInt':
411
+ return yield* BigInt(anySchema, value);
374
412
  case 'Boolean':
375
413
  return yield* Boolean(anySchema, value);
376
414
  case 'Constructor':
@@ -381,10 +419,14 @@ var TypeCompiler;
381
419
  return yield* Function(anySchema, value);
382
420
  case 'Integer':
383
421
  return yield* Integer(anySchema, value);
422
+ case 'Intersect':
423
+ return yield* Intersect(anySchema, value);
384
424
  case 'Literal':
385
425
  return yield* Literal(anySchema, value);
386
426
  case 'Never':
387
427
  return yield* Never(anySchema, value);
428
+ case 'Not':
429
+ return yield* Not(anySchema, value);
388
430
  case 'Null':
389
431
  return yield* Null(anySchema, value);
390
432
  case 'Number':
@@ -401,6 +443,8 @@ var TypeCompiler;
401
443
  return yield* Self(anySchema, value);
402
444
  case 'String':
403
445
  return yield* String(anySchema, value);
446
+ case 'Symbol':
447
+ return yield* Symbol(anySchema, value);
404
448
  case 'Tuple':
405
449
  return yield* Tuple(anySchema, value);
406
450
  case 'Undefined':
@@ -414,7 +458,7 @@ var TypeCompiler;
414
458
  case 'Void':
415
459
  return yield* Void(anySchema, value);
416
460
  default:
417
- if (!index_5.Custom.Has(anySchema[Types.Kind]))
461
+ if (!Types.TypeRegistry.Has(anySchema[Types.Kind]))
418
462
  throw new TypeCompilerUnknownTypeError(schema);
419
463
  return yield* UserDefined(anySchema, value);
420
464
  }
@@ -422,25 +466,14 @@ var TypeCompiler;
422
466
  // -------------------------------------------------------------------
423
467
  // Compiler State
424
468
  // -------------------------------------------------------------------
425
- const state_reference_map = new Map(); // tracks schemas with identifiers
426
469
  const state_local_variables = new Set(); // local variables and functions
427
470
  const state_local_function_names = new Set(); // local function names used call ref validators
428
471
  const state_remote_custom_types = new Map(); // remote custom types used during compilation
429
472
  function ResetCompiler() {
430
- state_reference_map.clear();
431
473
  state_local_variables.clear();
432
474
  state_local_function_names.clear();
433
475
  state_remote_custom_types.clear();
434
476
  }
435
- function AddReferences(schemas = []) {
436
- for (const schema of schemas) {
437
- if (!schema.$id)
438
- throw new Error(`TypeCompiler: Referenced schemas must specify an $id.`);
439
- if (state_reference_map.has(schema.$id))
440
- throw new Error(`TypeCompiler: Duplicate schema $id found for '${schema.$id}'`);
441
- state_reference_map.set(schema.$id, schema);
442
- }
443
- }
444
477
  function CreateExpression(schema, value) {
445
478
  return `(${[...Visit(schema, value)].join(' && ')})`;
446
479
  }
@@ -465,34 +498,39 @@ var TypeCompiler;
465
498
  // -------------------------------------------------------------------
466
499
  // Compile
467
500
  // -------------------------------------------------------------------
468
- function Build(schema, references = []) {
501
+ function Build(schema) {
469
502
  ResetCompiler();
470
- AddReferences(references);
471
503
  const check = CreateFunction('check', schema, 'value');
472
504
  const locals = GetLocals();
473
505
  return `${locals.join('\n')}\nreturn ${check}`;
474
506
  }
507
+ /** Returns the generated validation code used to validate this type. */
508
+ function Code(schema) {
509
+ if (!Types.TypeGuard.TSchema(schema))
510
+ throw new TypeCompilerPreflightCheckError(schema);
511
+ return Build(schema);
512
+ }
513
+ TypeCompiler.Code = Code;
475
514
  /** Compiles the given type for runtime type checking. This compiler only accepts known TypeBox types non-inclusive of unsafe types. */
476
- function Compile(schema, references = []) {
477
- index_3.TypeGuard.Assert(schema, references);
478
- const code = Build(schema, references);
515
+ function Compile(schema) {
516
+ const code = Code(schema);
479
517
  const custom_schemas = new Map(state_remote_custom_types);
480
518
  const compiledFunction = globalThis.Function('custom', 'format', 'hash', code);
481
519
  const checkFunction = compiledFunction((kind, schema_key, value) => {
482
- if (!index_5.Custom.Has(kind) || !custom_schemas.has(schema_key))
520
+ if (!Types.TypeRegistry.Has(kind) || !custom_schemas.has(schema_key))
483
521
  return false;
484
522
  const schema = custom_schemas.get(schema_key);
485
- const func = index_5.Custom.Get(kind);
523
+ const func = Types.TypeRegistry.Get(kind);
486
524
  return func(schema, value);
487
525
  }, (format, value) => {
488
- if (!index_4.Format.Has(format))
526
+ if (!Types.FormatRegistry.Has(format))
489
527
  return false;
490
- const func = index_4.Format.Get(format);
528
+ const func = Types.FormatRegistry.Get(format);
491
529
  return func(value);
492
530
  }, (value) => {
493
- return index_6.ValueHash.Create(value);
531
+ return hash_1.ValueHash.Create(value);
494
532
  });
495
- return new TypeCheck(schema, references, checkFunction, code);
533
+ return new TypeCheck(schema, checkFunction, code);
496
534
  }
497
535
  TypeCompiler.Compile = Compile;
498
536
  })(TypeCompiler = exports.TypeCompiler || (exports.TypeCompiler = {}));
@@ -4,51 +4,61 @@ export declare enum ValueErrorType {
4
4
  ArrayMinItems = 1,
5
5
  ArrayMaxItems = 2,
6
6
  ArrayUniqueItems = 3,
7
- Boolean = 4,
8
- Date = 5,
9
- DateExclusiveMinimumTimestamp = 6,
10
- DateExclusiveMaximumTimestamp = 7,
11
- DateMinimumTimestamp = 8,
12
- DateMaximumTimestamp = 9,
13
- Function = 10,
14
- Integer = 11,
15
- IntegerMultipleOf = 12,
16
- IntegerExclusiveMinimum = 13,
17
- IntegerExclusiveMaximum = 14,
18
- IntegerMinimum = 15,
19
- IntegerMaximum = 16,
20
- Literal = 17,
21
- Never = 18,
22
- Null = 19,
23
- Number = 20,
24
- NumberMultipleOf = 21,
25
- NumberExclusiveMinimum = 22,
26
- NumberExclusiveMaximum = 23,
27
- NumberMinumum = 24,
28
- NumberMaximum = 25,
29
- Object = 26,
30
- ObjectMinProperties = 27,
31
- ObjectMaxProperties = 28,
32
- ObjectAdditionalProperties = 29,
33
- ObjectRequiredProperties = 30,
34
- Promise = 31,
35
- RecordKeyNumeric = 32,
36
- RecordKeyString = 33,
37
- String = 34,
38
- StringMinLength = 35,
39
- StringMaxLength = 36,
40
- StringPattern = 37,
41
- StringFormatUnknown = 38,
42
- StringFormat = 39,
43
- TupleZeroLength = 40,
44
- TupleLength = 41,
45
- Undefined = 42,
46
- Union = 43,
47
- Uint8Array = 44,
48
- Uint8ArrayMinByteLength = 45,
49
- Uint8ArrayMaxByteLength = 46,
50
- Void = 47,
51
- Custom = 48
7
+ BigInt = 4,
8
+ BigIntMultipleOf = 5,
9
+ BigIntExclusiveMinimum = 6,
10
+ BigIntExclusiveMaximum = 7,
11
+ BigIntMinimum = 8,
12
+ BigIntMaximum = 9,
13
+ Boolean = 10,
14
+ Date = 11,
15
+ DateExclusiveMinimumTimestamp = 12,
16
+ DateExclusiveMaximumTimestamp = 13,
17
+ DateMinimumTimestamp = 14,
18
+ DateMaximumTimestamp = 15,
19
+ Function = 16,
20
+ Integer = 17,
21
+ IntegerMultipleOf = 18,
22
+ IntegerExclusiveMinimum = 19,
23
+ IntegerExclusiveMaximum = 20,
24
+ IntegerMinimum = 21,
25
+ IntegerMaximum = 22,
26
+ Intersect = 23,
27
+ IntersectUnevaluatedProperties = 24,
28
+ Literal = 25,
29
+ Never = 26,
30
+ Not = 27,
31
+ Null = 28,
32
+ Number = 29,
33
+ NumberMultipleOf = 30,
34
+ NumberExclusiveMinimum = 31,
35
+ NumberExclusiveMaximum = 32,
36
+ NumberMinumum = 33,
37
+ NumberMaximum = 34,
38
+ Object = 35,
39
+ ObjectMinProperties = 36,
40
+ ObjectMaxProperties = 37,
41
+ ObjectAdditionalProperties = 38,
42
+ ObjectRequiredProperties = 39,
43
+ Promise = 40,
44
+ RecordKeyNumeric = 41,
45
+ RecordKeyString = 42,
46
+ String = 43,
47
+ StringMinLength = 44,
48
+ StringMaxLength = 45,
49
+ StringPattern = 46,
50
+ StringFormatUnknown = 47,
51
+ StringFormat = 48,
52
+ Symbol = 49,
53
+ TupleZeroLength = 50,
54
+ TupleLength = 51,
55
+ Undefined = 52,
56
+ Union = 53,
57
+ Uint8Array = 54,
58
+ Uint8ArrayMinByteLength = 55,
59
+ Uint8ArrayMaxByteLength = 56,
60
+ Void = 57,
61
+ Custom = 58
52
62
  }
53
63
  export interface ValueError {
54
64
  type: ValueErrorType;
@@ -63,5 +73,5 @@ export declare class ValueErrorsUnknownTypeError extends Error {
63
73
  }
64
74
  /** Provides functionality to generate a sequence of errors against a TypeBox type. */
65
75
  export declare namespace ValueErrors {
66
- function Errors<T extends Types.TSchema>(schema: T, references: Types.TSchema[], value: any): IterableIterator<ValueError>;
76
+ function Errors<T extends Types.TSchema>(schema: T, value: any): IterableIterator<ValueError>;
67
77
  }