@sinclair/typebox 0.24.25 → 0.24.28

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.
@@ -17,7 +17,7 @@ export declare class TypeCheck<T extends Types.TSchema> {
17
17
  export declare namespace Property {
18
18
  function Check(propertyName: string): boolean;
19
19
  }
20
- export declare class TypeCompilerInvalidTypeError extends Error {
20
+ export declare class TypeCompilerUnknownTypeError extends Error {
21
21
  readonly schema: Types.TSchema;
22
22
  constructor(schema: Types.TSchema);
23
23
  }
@@ -27,9 +27,10 @@ THE SOFTWARE.
27
27
 
28
28
  ---------------------------------------------------------------------------*/
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.TypeCompiler = exports.TypeCompilerInvalidTypeError = exports.Property = exports.TypeCheck = void 0;
30
+ exports.TypeCompiler = exports.TypeCompilerUnknownTypeError = exports.Property = exports.TypeCheck = void 0;
31
31
  const index_1 = require("../errors/index");
32
32
  const index_2 = require("../guard/index");
33
+ const Types = require("../typebox");
33
34
  // -------------------------------------------------------------------
34
35
  // TypeCheck
35
36
  // -------------------------------------------------------------------
@@ -71,16 +72,7 @@ var Property;
71
72
  function Alpha(code) {
72
73
  return (code >= 65 && code <= 90) || (code >= 97 && code <= 122);
73
74
  }
74
- function AssertEscapeCharacters(propertyName) {
75
- for (let i = 0; i < propertyName.length; i++) {
76
- const code = propertyName.charCodeAt(i);
77
- if ((code >= 7 && code <= 13) || code === 27 || code === 127) {
78
- throw Error('Property: Invalid escape character found in property key');
79
- }
80
- }
81
- }
82
75
  function Check(propertyName) {
83
- AssertEscapeCharacters(propertyName);
84
76
  if (propertyName.length === 0)
85
77
  return false;
86
78
  {
@@ -102,13 +94,13 @@ var Property;
102
94
  // -------------------------------------------------------------------
103
95
  // TypeCompiler
104
96
  // -------------------------------------------------------------------
105
- class TypeCompilerInvalidTypeError extends Error {
97
+ class TypeCompilerUnknownTypeError extends Error {
106
98
  constructor(schema) {
107
- super('TypeCompiler: Invalid type');
99
+ super('TypeCompiler: Unknown type');
108
100
  this.schema = schema;
109
101
  }
110
102
  }
111
- exports.TypeCompilerInvalidTypeError = TypeCompilerInvalidTypeError;
103
+ exports.TypeCompilerUnknownTypeError = TypeCompilerUnknownTypeError;
112
104
  /** Compiles Types for Runtime Type Checking */
113
105
  var TypeCompiler;
114
106
  (function (TypeCompiler) {
@@ -132,7 +124,7 @@ var TypeCompiler;
132
124
  yield `(typeof ${value} === 'boolean')`;
133
125
  }
134
126
  function* Constructor(schema, value) {
135
- yield* Visit(schema.returns, value);
127
+ yield* Visit(schema.returns, `${value}.prototype`);
136
128
  }
137
129
  function* Function(schema, value) {
138
130
  yield `(typeof ${value} === 'function')`;
@@ -154,11 +146,8 @@ var TypeCompiler;
154
146
  if (typeof schema.const === 'number' || typeof schema.const === 'boolean') {
155
147
  yield `(${value} === ${schema.const})`;
156
148
  }
157
- else if (typeof schema.const === 'string') {
158
- yield `(${value} === '${schema.const}')`;
159
- }
160
149
  else {
161
- throw Error('TypeCompiler: Literal value should be string, number or boolean');
150
+ yield `(${value} === '${schema.const}')`;
162
151
  }
163
152
  }
164
153
  function* Null(schema, value) {
@@ -185,7 +174,7 @@ var TypeCompiler;
185
174
  yield `(Object.keys(${value}).length <= ${schema.maxProperties})`;
186
175
  const propertyKeys = globalThis.Object.keys(schema.properties);
187
176
  if (schema.additionalProperties === false) {
188
- // optimization: If the property key length matches the required keys length
177
+ // Optimization: If the property key length matches the required keys length
189
178
  // then we only need check that the values property key length matches that
190
179
  // of the property key length. This is because exhaustive testing for values
191
180
  // will occur in subsequent property tests.
@@ -221,6 +210,11 @@ var TypeCompiler;
221
210
  yield `(Object.values(${value}).every(value => ${expression}))`;
222
211
  }
223
212
  function* Ref(schema, value) {
213
+ // Reference: If we have seen this reference before we can just yield and return
214
+ // the function call. If this isn't the case we defer to visit to generate and
215
+ // set the function for subsequent passes. Consider for refactor.
216
+ if (names.has(schema.$ref))
217
+ return yield `(${CreateFunctionName(schema.$ref)}(${value}))`;
224
218
  if (!referenceMap.has(schema.$ref))
225
219
  throw Error(`TypeCompiler.Ref: Cannot de-reference schema with $id '${schema.$ref}'`);
226
220
  const reference = referenceMap.get(schema.$ref);
@@ -274,9 +268,9 @@ var TypeCompiler;
274
268
  yield `(${value} === null)`;
275
269
  }
276
270
  function* Visit(schema, value) {
277
- // reference: referenced schemas can originate from either additional
278
- // schemas or inline in the schema itself. Ideally the recursive
279
- // path should align to reference path. Consider for review.
271
+ // Reference: Referenced schemas can originate from either additional schemas
272
+ // or inline in the schema itself. Ideally the recursive path should align to
273
+ // reference path. Consider for refactor.
280
274
  if (schema.$id && !names.has(schema.$id)) {
281
275
  names.add(schema.$id);
282
276
  const name = CreateFunctionName(schema.$id);
@@ -285,71 +279,52 @@ var TypeCompiler;
285
279
  yield `(${name}(${value}))`;
286
280
  return;
287
281
  }
288
- if (index_2.TypeGuard.TAny(schema)) {
289
- return yield* Any(schema, value);
290
- }
291
- else if (index_2.TypeGuard.TArray(schema)) {
292
- return yield* Array(schema, value);
293
- }
294
- else if (index_2.TypeGuard.TBoolean(schema)) {
295
- return yield* Boolean(schema, value);
296
- }
297
- else if (index_2.TypeGuard.TConstructor(schema)) {
298
- return yield* Constructor(schema, value);
299
- }
300
- else if (index_2.TypeGuard.TFunction(schema)) {
301
- return yield* Function(schema, value);
302
- }
303
- else if (index_2.TypeGuard.TInteger(schema)) {
304
- return yield* Integer(schema, value);
305
- }
306
- else if (index_2.TypeGuard.TLiteral(schema)) {
307
- return yield* Literal(schema, value);
308
- }
309
- else if (index_2.TypeGuard.TNull(schema)) {
310
- return yield* Null(schema, value);
311
- }
312
- else if (index_2.TypeGuard.TNumber(schema)) {
313
- return yield* Number(schema, value);
314
- }
315
- else if (index_2.TypeGuard.TObject(schema)) {
316
- return yield* Object(schema, value);
317
- }
318
- else if (index_2.TypeGuard.TPromise(schema)) {
319
- return yield* Promise(schema, value);
320
- }
321
- else if (index_2.TypeGuard.TRecord(schema)) {
322
- return yield* Record(schema, value);
323
- }
324
- else if (index_2.TypeGuard.TRef(schema)) {
325
- return yield* Ref(schema, value);
326
- }
327
- else if (index_2.TypeGuard.TSelf(schema)) {
328
- return yield* Self(schema, value);
329
- }
330
- else if (index_2.TypeGuard.TString(schema)) {
331
- return yield* String(schema, value);
332
- }
333
- else if (index_2.TypeGuard.TTuple(schema)) {
334
- return yield* Tuple(schema, value);
335
- }
336
- else if (index_2.TypeGuard.TUndefined(schema)) {
337
- return yield* Undefined(schema, value);
338
- }
339
- else if (index_2.TypeGuard.TUnion(schema)) {
340
- return yield* Union(schema, value);
341
- }
342
- else if (index_2.TypeGuard.TUint8Array(schema)) {
343
- return yield* Uint8Array(schema, value);
344
- }
345
- else if (index_2.TypeGuard.TUnknown(schema)) {
346
- return yield* Unknown(schema, value);
347
- }
348
- else if (index_2.TypeGuard.TVoid(schema)) {
349
- return yield* Void(schema, value);
350
- }
351
- else {
352
- throw new TypeCompilerInvalidTypeError(schema);
282
+ const anySchema = schema;
283
+ switch (anySchema[Types.Kind]) {
284
+ case 'Any':
285
+ return yield* Any(anySchema, value);
286
+ case 'Array':
287
+ return yield* Array(anySchema, value);
288
+ case 'Boolean':
289
+ return yield* Boolean(anySchema, value);
290
+ case 'Constructor':
291
+ return yield* Constructor(anySchema, value);
292
+ case 'Function':
293
+ return yield* Function(anySchema, value);
294
+ case 'Integer':
295
+ return yield* Integer(anySchema, value);
296
+ case 'Literal':
297
+ return yield* Literal(anySchema, value);
298
+ case 'Null':
299
+ return yield* Null(anySchema, value);
300
+ case 'Number':
301
+ return yield* Number(anySchema, value);
302
+ case 'Object':
303
+ return yield* Object(anySchema, value);
304
+ case 'Promise':
305
+ return yield* Promise(anySchema, value);
306
+ case 'Record':
307
+ return yield* Record(anySchema, value);
308
+ case 'Ref':
309
+ return yield* Ref(anySchema, value);
310
+ case 'Self':
311
+ return yield* Self(anySchema, value);
312
+ case 'String':
313
+ return yield* String(anySchema, value);
314
+ case 'Tuple':
315
+ return yield* Tuple(anySchema, value);
316
+ case 'Undefined':
317
+ return yield* Undefined(anySchema, value);
318
+ case 'Union':
319
+ return yield* Union(anySchema, value);
320
+ case 'Uint8Array':
321
+ return yield* Uint8Array(anySchema, value);
322
+ case 'Unknown':
323
+ return yield* Unknown(anySchema, value);
324
+ case 'Void':
325
+ return yield* Void(anySchema, value);
326
+ default:
327
+ throw new TypeCompilerUnknownTypeError(schema);
353
328
  }
354
329
  }
355
330
  // -------------------------------------------------------------------
@@ -405,6 +380,7 @@ var TypeCompiler;
405
380
  }
406
381
  /** Compiles the given type for runtime type checking. This compiler only accepts known TypeBox types non-inclusive of unsafe types. */
407
382
  function Compile(schema, references = []) {
383
+ index_2.TypeGuard.Assert(schema, references);
408
384
  const code = Build(schema, references);
409
385
  const func = globalThis.Function(code);
410
386
  return new TypeCheck(schema, references, func(), code);
@@ -30,7 +30,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
30
30
  exports.Conditional = void 0;
31
31
  const Types = require("../typebox");
32
32
  const structural_1 = require("./structural");
33
- const guard_1 = require("../guard");
33
+ const index_1 = require("../guard/index");
34
34
  /** Conditional Types */
35
35
  var Conditional;
36
36
  (function (Conditional) {
@@ -59,7 +59,7 @@ var Conditional;
59
59
  Conditional.Exclude = Exclude;
60
60
  /** (Experimental) Constructs a type by extracting from Type all union members that are assignable to Union. */
61
61
  function Extract(type, union, options = {}) {
62
- if (guard_1.TypeGuard.TUnion(type)) {
62
+ if (index_1.TypeGuard.TUnion(type)) {
63
63
  const anyOf = type.anyOf.filter((schema) => structural_1.Structural.Check(schema, union) === structural_1.StructuralResult.True).map((schema) => Clone(schema));
64
64
  return { ...options, [Types.Kind]: 'Union', anyOf };
65
65
  }
@@ -47,7 +47,7 @@ export interface ValueError {
47
47
  value: unknown;
48
48
  message: string;
49
49
  }
50
- export declare class ValueErrorsInvalidTypeError extends Error {
50
+ export declare class ValueErrorsUnknownTypeError extends Error {
51
51
  readonly schema: Types.TSchema;
52
52
  constructor(schema: Types.TSchema);
53
53
  }
package/errors/errors.js CHANGED
@@ -27,8 +27,8 @@ THE SOFTWARE.
27
27
 
28
28
  ---------------------------------------------------------------------------*/
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.ValueErrors = exports.ValueErrorsInvalidTypeError = exports.ValueErrorType = void 0;
31
- const index_1 = require("../guard/index");
30
+ exports.ValueErrors = exports.ValueErrorsUnknownTypeError = exports.ValueErrorType = void 0;
31
+ const Types = require("../typebox");
32
32
  // -------------------------------------------------------------------
33
33
  // ValueErrorType
34
34
  // -------------------------------------------------------------------
@@ -77,13 +77,13 @@ var ValueErrorType;
77
77
  // -------------------------------------------------------------------
78
78
  // ValueErrors
79
79
  // -------------------------------------------------------------------
80
- class ValueErrorsInvalidTypeError extends Error {
80
+ class ValueErrorsUnknownTypeError extends Error {
81
81
  constructor(schema) {
82
- super('ValueErrors: Invalid type');
82
+ super('ValueErrors: Unknown type');
83
83
  this.schema = schema;
84
84
  }
85
85
  }
86
- exports.ValueErrorsInvalidTypeError = ValueErrorsInvalidTypeError;
86
+ exports.ValueErrorsUnknownTypeError = ValueErrorsUnknownTypeError;
87
87
  var ValueErrors;
88
88
  (function (ValueErrors) {
89
89
  function* Any(schema, references, path, value) { }
@@ -110,7 +110,7 @@ var ValueErrors;
110
110
  }
111
111
  }
112
112
  function* Constructor(schema, references, path, value) {
113
- yield* Visit(schema.returns, references, path, value);
113
+ yield* Visit(schema.returns, references, path, value.prototype);
114
114
  }
115
115
  function* Function(schema, references, path, value) {
116
116
  if (!(typeof value === 'function')) {
@@ -306,72 +306,53 @@ var ValueErrors;
306
306
  }
307
307
  }
308
308
  function* Visit(schema, references, path, value) {
309
- const refs = schema.$id === undefined ? references : [schema, ...references];
310
- if (index_1.TypeGuard.TAny(schema)) {
311
- return yield* Any(schema, refs, path, value);
312
- }
313
- else if (index_1.TypeGuard.TArray(schema)) {
314
- return yield* Array(schema, refs, path, value);
315
- }
316
- else if (index_1.TypeGuard.TBoolean(schema)) {
317
- return yield* Boolean(schema, refs, path, value);
318
- }
319
- else if (index_1.TypeGuard.TConstructor(schema)) {
320
- return yield* Constructor(schema, refs, path, value);
321
- }
322
- else if (index_1.TypeGuard.TFunction(schema)) {
323
- return yield* Function(schema, refs, path, value);
324
- }
325
- else if (index_1.TypeGuard.TInteger(schema)) {
326
- return yield* Integer(schema, refs, path, value);
327
- }
328
- else if (index_1.TypeGuard.TLiteral(schema)) {
329
- return yield* Literal(schema, refs, path, value);
330
- }
331
- else if (index_1.TypeGuard.TNull(schema)) {
332
- return yield* Null(schema, refs, path, value);
333
- }
334
- else if (index_1.TypeGuard.TNumber(schema)) {
335
- return yield* Number(schema, refs, path, value);
336
- }
337
- else if (index_1.TypeGuard.TObject(schema)) {
338
- return yield* Object(schema, refs, path, value);
339
- }
340
- else if (index_1.TypeGuard.TPromise(schema)) {
341
- return yield* Promise(schema, refs, path, value);
342
- }
343
- else if (index_1.TypeGuard.TRecord(schema)) {
344
- return yield* Record(schema, refs, path, value);
345
- }
346
- else if (index_1.TypeGuard.TRef(schema)) {
347
- return yield* Ref(schema, refs, path, value);
348
- }
349
- else if (index_1.TypeGuard.TSelf(schema)) {
350
- return yield* Self(schema, refs, path, value);
351
- }
352
- else if (index_1.TypeGuard.TString(schema)) {
353
- return yield* String(schema, refs, path, value);
354
- }
355
- else if (index_1.TypeGuard.TTuple(schema)) {
356
- return yield* Tuple(schema, refs, path, value);
357
- }
358
- else if (index_1.TypeGuard.TUndefined(schema)) {
359
- return yield* Undefined(schema, refs, path, value);
360
- }
361
- else if (index_1.TypeGuard.TUnion(schema)) {
362
- return yield* Union(schema, refs, path, value);
363
- }
364
- else if (index_1.TypeGuard.TUint8Array(schema)) {
365
- return yield* Uint8Array(schema, refs, path, value);
366
- }
367
- else if (index_1.TypeGuard.TUnknown(schema)) {
368
- return yield* Unknown(schema, refs, path, value);
369
- }
370
- else if (index_1.TypeGuard.TVoid(schema)) {
371
- return yield* Void(schema, refs, path, value);
372
- }
373
- else {
374
- throw new ValueErrorsInvalidTypeError(schema);
309
+ const anyReferences = schema.$id === undefined ? references : [schema, ...references];
310
+ const anySchema = schema;
311
+ switch (anySchema[Types.Kind]) {
312
+ case 'Any':
313
+ return yield* Any(anySchema, anyReferences, path, value);
314
+ case 'Array':
315
+ return yield* Array(anySchema, anyReferences, path, value);
316
+ case 'Boolean':
317
+ return yield* Boolean(anySchema, anyReferences, path, value);
318
+ case 'Constructor':
319
+ return yield* Constructor(anySchema, anyReferences, path, value);
320
+ case 'Function':
321
+ return yield* Function(anySchema, anyReferences, path, value);
322
+ case 'Integer':
323
+ return yield* Integer(anySchema, anyReferences, path, value);
324
+ case 'Literal':
325
+ return yield* Literal(anySchema, anyReferences, path, value);
326
+ case 'Null':
327
+ return yield* Null(anySchema, anyReferences, path, value);
328
+ case 'Number':
329
+ return yield* Number(anySchema, anyReferences, path, value);
330
+ case 'Object':
331
+ return yield* Object(anySchema, anyReferences, path, value);
332
+ case 'Promise':
333
+ return yield* Promise(anySchema, anyReferences, path, value);
334
+ case 'Record':
335
+ return yield* Record(anySchema, anyReferences, path, value);
336
+ case 'Ref':
337
+ return yield* Ref(anySchema, anyReferences, path, value);
338
+ case 'Self':
339
+ return yield* Self(anySchema, anyReferences, path, value);
340
+ case 'String':
341
+ return yield* String(anySchema, anyReferences, path, value);
342
+ case 'Tuple':
343
+ return yield* Tuple(anySchema, anyReferences, path, value);
344
+ case 'Undefined':
345
+ return yield* Undefined(anySchema, anyReferences, path, value);
346
+ case 'Union':
347
+ return yield* Union(anySchema, anyReferences, path, value);
348
+ case 'Uint8Array':
349
+ return yield* Uint8Array(anySchema, anyReferences, path, value);
350
+ case 'Unknown':
351
+ return yield* Unknown(anySchema, anyReferences, path, value);
352
+ case 'Void':
353
+ return yield* Void(anySchema, anyReferences, path, value);
354
+ default:
355
+ throw new ValueErrorsUnknownTypeError(schema);
375
356
  }
376
357
  }
377
358
  function* Errors(schema, references, value) {
package/guard/guard.d.ts CHANGED
@@ -1,48 +1,54 @@
1
1
  import * as Types from '../typebox';
2
+ export declare class TypeGuardInvalidTypeError extends Error {
3
+ readonly schema: unknown;
4
+ constructor(schema: unknown);
5
+ }
2
6
  /** TypeGuard tests that values conform to a known TypeBox type specification */
3
7
  export declare namespace TypeGuard {
4
8
  /** Returns true if the given schema is TAny */
5
- function TAny(schema: any): schema is Types.TAny;
9
+ function TAny(schema: unknown): schema is Types.TAny;
6
10
  /** Returns true if the given schema is TArray */
7
- function TArray(schema: any): schema is Types.TArray;
11
+ function TArray(schema: unknown): schema is Types.TArray;
8
12
  /** Returns true if the given schema is TBoolean */
9
- function TBoolean(schema: any): schema is Types.TBoolean;
13
+ function TBoolean(schema: unknown): schema is Types.TBoolean;
10
14
  /** Returns true if the given schema is TConstructor */
11
- function TConstructor(schema: any): schema is Types.TConstructor;
15
+ function TConstructor(schema: unknown): schema is Types.TConstructor;
12
16
  /** Returns true if the given schema is TFunction */
13
- function TFunction(schema: any): schema is Types.TFunction;
17
+ function TFunction(schema: unknown): schema is Types.TFunction;
14
18
  /** Returns true if the given schema is TInteger */
15
- function TInteger(schema: any): schema is Types.TInteger;
19
+ function TInteger(schema: unknown): schema is Types.TInteger;
16
20
  /** Returns true if the given schema is TLiteral */
17
- function TLiteral(schema: any): schema is Types.TLiteral;
21
+ function TLiteral(schema: unknown): schema is Types.TLiteral;
18
22
  /** Returns true if the given schema is TNull */
19
- function TNull(schema: any): schema is Types.TNull;
23
+ function TNull(schema: unknown): schema is Types.TNull;
20
24
  /** Returns true if the given schema is TNumber */
21
- function TNumber(schema: any): schema is Types.TNumber;
25
+ function TNumber(schema: unknown): schema is Types.TNumber;
22
26
  /** Returns true if the given schema is TObject */
23
- function TObject(schema: any): schema is Types.TObject;
27
+ function TObject(schema: unknown): schema is Types.TObject;
24
28
  /** Returns true if the given schema is TPromise */
25
- function TPromise(schema: any): schema is Types.TPromise;
29
+ function TPromise(schema: unknown): schema is Types.TPromise;
26
30
  /** Returns true if the given schema is TRecord */
27
- function TRecord(schema: any): schema is Types.TRecord;
31
+ function TRecord(schema: unknown): schema is Types.TRecord;
28
32
  /** Returns true if the given schema is TSelf */
29
- function TSelf(schema: any): schema is Types.TSelf;
33
+ function TSelf(schema: unknown): schema is Types.TSelf;
30
34
  /** Returns true if the given schema is TRef */
31
- function TRef(schema: any): schema is Types.TRef;
35
+ function TRef(schema: unknown): schema is Types.TRef;
32
36
  /** Returns true if the given schema is TString */
33
- function TString(schema: any): schema is Types.TString;
37
+ function TString(schema: unknown): schema is Types.TString;
34
38
  /** Returns true if the given schema is TTuple */
35
- function TTuple(schema: any): schema is Types.TTuple;
39
+ function TTuple(schema: unknown): schema is Types.TTuple;
36
40
  /** Returns true if the given schema is TUndefined */
37
- function TUndefined(schema: any): schema is Types.TUndefined;
41
+ function TUndefined(schema: unknown): schema is Types.TUndefined;
38
42
  /** Returns true if the given schema is TUnion */
39
- function TUnion(schema: any): schema is Types.TUnion;
43
+ function TUnion(schema: unknown): schema is Types.TUnion;
40
44
  /** Returns true if the given schema is TUint8Array */
41
- function TUint8Array(schema: any): schema is Types.TUint8Array;
45
+ function TUint8Array(schema: unknown): schema is Types.TUint8Array;
42
46
  /** Returns true if the given schema is TUnknown */
43
- function TUnknown(schema: any): schema is Types.TUnknown;
47
+ function TUnknown(schema: unknown): schema is Types.TUnknown;
44
48
  /** Returns true if the given schema is TVoid */
45
- function TVoid(schema: any): schema is Types.TVoid;
49
+ function TVoid(schema: unknown): schema is Types.TVoid;
46
50
  /** Returns true if the given schema is TSchema */
47
- function TSchema(schema: any): schema is Types.TSchema;
51
+ function TSchema(schema: unknown): schema is Types.TSchema;
52
+ /** Asserts if this schema and associated references are valid. */
53
+ function Assert<T extends Types.TSchema>(schema: T, references?: Types.TSchema[]): void;
48
54
  }