@sinclair/typebox 0.24.26 → 0.24.27

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
  // -------------------------------------------------------------------
@@ -93,13 +94,13 @@ var Property;
93
94
  // -------------------------------------------------------------------
94
95
  // TypeCompiler
95
96
  // -------------------------------------------------------------------
96
- class TypeCompilerInvalidTypeError extends Error {
97
+ class TypeCompilerUnknownTypeError extends Error {
97
98
  constructor(schema) {
98
- super('TypeCompiler: Invalid type');
99
+ super('TypeCompiler: Unknown type');
99
100
  this.schema = schema;
100
101
  }
101
102
  }
102
- exports.TypeCompilerInvalidTypeError = TypeCompilerInvalidTypeError;
103
+ exports.TypeCompilerUnknownTypeError = TypeCompilerUnknownTypeError;
103
104
  /** Compiles Types for Runtime Type Checking */
104
105
  var TypeCompiler;
105
106
  (function (TypeCompiler) {
@@ -145,11 +146,8 @@ var TypeCompiler;
145
146
  if (typeof schema.const === 'number' || typeof schema.const === 'boolean') {
146
147
  yield `(${value} === ${schema.const})`;
147
148
  }
148
- else if (typeof schema.const === 'string') {
149
- yield `(${value} === '${schema.const}')`;
150
- }
151
149
  else {
152
- throw Error('TypeCompiler: Literal value should be string, number or boolean');
150
+ yield `(${value} === '${schema.const}')`;
153
151
  }
154
152
  }
155
153
  function* Null(schema, value) {
@@ -276,71 +274,52 @@ var TypeCompiler;
276
274
  yield `(${name}(${value}))`;
277
275
  return;
278
276
  }
279
- if (index_2.TypeGuard.TAny(schema)) {
280
- return yield* Any(schema, value);
281
- }
282
- else if (index_2.TypeGuard.TArray(schema)) {
283
- return yield* Array(schema, value);
284
- }
285
- else if (index_2.TypeGuard.TBoolean(schema)) {
286
- return yield* Boolean(schema, value);
287
- }
288
- else if (index_2.TypeGuard.TConstructor(schema)) {
289
- return yield* Constructor(schema, value);
290
- }
291
- else if (index_2.TypeGuard.TFunction(schema)) {
292
- return yield* Function(schema, value);
293
- }
294
- else if (index_2.TypeGuard.TInteger(schema)) {
295
- return yield* Integer(schema, value);
296
- }
297
- else if (index_2.TypeGuard.TLiteral(schema)) {
298
- return yield* Literal(schema, value);
299
- }
300
- else if (index_2.TypeGuard.TNull(schema)) {
301
- return yield* Null(schema, value);
302
- }
303
- else if (index_2.TypeGuard.TNumber(schema)) {
304
- return yield* Number(schema, value);
305
- }
306
- else if (index_2.TypeGuard.TObject(schema)) {
307
- return yield* Object(schema, value);
308
- }
309
- else if (index_2.TypeGuard.TPromise(schema)) {
310
- return yield* Promise(schema, value);
311
- }
312
- else if (index_2.TypeGuard.TRecord(schema)) {
313
- return yield* Record(schema, value);
314
- }
315
- else if (index_2.TypeGuard.TRef(schema)) {
316
- return yield* Ref(schema, value);
317
- }
318
- else if (index_2.TypeGuard.TSelf(schema)) {
319
- return yield* Self(schema, value);
320
- }
321
- else if (index_2.TypeGuard.TString(schema)) {
322
- return yield* String(schema, value);
323
- }
324
- else if (index_2.TypeGuard.TTuple(schema)) {
325
- return yield* Tuple(schema, value);
326
- }
327
- else if (index_2.TypeGuard.TUndefined(schema)) {
328
- return yield* Undefined(schema, value);
329
- }
330
- else if (index_2.TypeGuard.TUnion(schema)) {
331
- return yield* Union(schema, value);
332
- }
333
- else if (index_2.TypeGuard.TUint8Array(schema)) {
334
- return yield* Uint8Array(schema, value);
335
- }
336
- else if (index_2.TypeGuard.TUnknown(schema)) {
337
- return yield* Unknown(schema, value);
338
- }
339
- else if (index_2.TypeGuard.TVoid(schema)) {
340
- return yield* Void(schema, value);
341
- }
342
- else {
343
- throw new TypeCompilerInvalidTypeError(schema);
277
+ const anySchema = schema;
278
+ switch (anySchema[Types.Kind]) {
279
+ case 'Any':
280
+ return yield* Any(anySchema, value);
281
+ case 'Array':
282
+ return yield* Array(anySchema, value);
283
+ case 'Boolean':
284
+ return yield* Boolean(anySchema, value);
285
+ case 'Constructor':
286
+ return yield* Constructor(anySchema, value);
287
+ case 'Function':
288
+ return yield* Function(anySchema, value);
289
+ case 'Integer':
290
+ return yield* Integer(anySchema, value);
291
+ case 'Literal':
292
+ return yield* Literal(anySchema, value);
293
+ case 'Null':
294
+ return yield* Null(anySchema, value);
295
+ case 'Number':
296
+ return yield* Number(anySchema, value);
297
+ case 'Object':
298
+ return yield* Object(anySchema, value);
299
+ case 'Promise':
300
+ return yield* Promise(anySchema, value);
301
+ case 'Record':
302
+ return yield* Record(anySchema, value);
303
+ case 'Ref':
304
+ return yield* Ref(anySchema, value);
305
+ case 'Self':
306
+ return yield* Self(anySchema, value);
307
+ case 'String':
308
+ return yield* String(anySchema, value);
309
+ case 'Tuple':
310
+ return yield* Tuple(anySchema, value);
311
+ case 'Undefined':
312
+ return yield* Undefined(anySchema, value);
313
+ case 'Union':
314
+ return yield* Union(anySchema, value);
315
+ case 'Uint8Array':
316
+ return yield* Uint8Array(anySchema, value);
317
+ case 'Unknown':
318
+ return yield* Unknown(anySchema, value);
319
+ case 'Void':
320
+ return yield* Void(anySchema, value);
321
+ default:
322
+ throw new TypeCompilerUnknownTypeError(schema);
344
323
  }
345
324
  }
346
325
  // -------------------------------------------------------------------
@@ -396,6 +375,7 @@ var TypeCompiler;
396
375
  }
397
376
  /** Compiles the given type for runtime type checking. This compiler only accepts known TypeBox types non-inclusive of unsafe types. */
398
377
  function Compile(schema, references = []) {
378
+ index_2.TypeGuard.Assert(schema, references);
399
379
  const code = Build(schema, references);
400
380
  const func = globalThis.Function(code);
401
381
  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) { }
@@ -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
  }
package/guard/guard.js CHANGED
@@ -27,8 +27,15 @@ THE SOFTWARE.
27
27
 
28
28
  ---------------------------------------------------------------------------*/
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.TypeGuard = void 0;
30
+ exports.TypeGuard = exports.TypeGuardInvalidTypeError = void 0;
31
31
  const Types = require("../typebox");
32
+ class TypeGuardInvalidTypeError extends Error {
33
+ constructor(schema) {
34
+ super('TypeGuard: Invalid type');
35
+ this.schema = schema;
36
+ }
37
+ }
38
+ exports.TypeGuardInvalidTypeError = TypeGuardInvalidTypeError;
32
39
  /** TypeGuard tests that values conform to a known TypeBox type specification */
33
40
  var TypeGuard;
34
41
  (function (TypeGuard) {
@@ -81,22 +88,29 @@ var TypeGuard;
81
88
  }
82
89
  /** Returns true if the given schema is TAny */
83
90
  function TAny(schema) {
84
- return IsObject(schema) && schema[Types.Kind] === 'Any';
91
+ return IsObject(schema) && schema[Types.Kind] === 'Any' && IsOptionalString(schema.$id);
85
92
  }
86
93
  TypeGuard.TAny = TAny;
87
94
  /** Returns true if the given schema is TArray */
88
95
  function TArray(schema) {
89
- return IsObject(schema) && schema[Types.Kind] === 'Array' && schema.type === 'array' && TSchema(schema.items) && IsOptionalNumber(schema.minItems) && IsOptionalNumber(schema.maxItems) && IsOptionalBoolean(schema.uniqueItems);
96
+ return (IsObject(schema) &&
97
+ schema[Types.Kind] === 'Array' &&
98
+ schema.type === 'array' &&
99
+ IsOptionalString(schema.$id) &&
100
+ TSchema(schema.items) &&
101
+ IsOptionalNumber(schema.minItems) &&
102
+ IsOptionalNumber(schema.maxItems) &&
103
+ IsOptionalBoolean(schema.uniqueItems));
90
104
  }
91
105
  TypeGuard.TArray = TArray;
92
106
  /** Returns true if the given schema is TBoolean */
93
107
  function TBoolean(schema) {
94
- return IsObject(schema) && schema[Types.Kind] === 'Boolean' && schema.type === 'boolean';
108
+ return IsObject(schema) && schema[Types.Kind] === 'Boolean' && schema.type === 'boolean' && IsOptionalString(schema.$id);
95
109
  }
96
110
  TypeGuard.TBoolean = TBoolean;
97
111
  /** Returns true if the given schema is TConstructor */
98
112
  function TConstructor(schema) {
99
- if (!(IsObject(schema) && schema[Types.Kind] === 'Constructor' && schema.type === 'constructor' && IsArray(schema.parameters) && TSchema(schema.returns))) {
113
+ if (!(IsObject(schema) && schema[Types.Kind] === 'Constructor' && schema.type === 'constructor' && IsOptionalString(schema.$id) && IsArray(schema.parameters) && TSchema(schema.returns))) {
100
114
  return false;
101
115
  }
102
116
  for (const parameter of schema.parameters) {
@@ -108,7 +122,7 @@ var TypeGuard;
108
122
  TypeGuard.TConstructor = TConstructor;
109
123
  /** Returns true if the given schema is TFunction */
110
124
  function TFunction(schema) {
111
- if (!(IsObject(schema) && schema[Types.Kind] === 'Function' && schema.type === 'function' && IsArray(schema.parameters) && TSchema(schema.returns))) {
125
+ if (!(IsObject(schema) && schema[Types.Kind] === 'Function' && schema.type === 'function' && IsOptionalString(schema.$id) && IsArray(schema.parameters) && TSchema(schema.returns))) {
112
126
  return false;
113
127
  }
114
128
  for (const parameter of schema.parameters) {
@@ -123,6 +137,7 @@ var TypeGuard;
123
137
  return (IsObject(schema) &&
124
138
  schema[Types.Kind] === 'Integer' &&
125
139
  schema.type === 'integer' &&
140
+ IsOptionalString(schema.$id) &&
126
141
  IsOptionalNumber(schema.multipleOf) &&
127
142
  IsOptionalNumber(schema.minimum) &&
128
143
  IsOptionalNumber(schema.maximum) &&
@@ -132,12 +147,12 @@ var TypeGuard;
132
147
  TypeGuard.TInteger = TInteger;
133
148
  /** Returns true if the given schema is TLiteral */
134
149
  function TLiteral(schema) {
135
- return IsObject(schema) && schema[Types.Kind] === 'Literal' && (IsString(schema.const) || IsNumber(schema.const) || IsBoolean(schema.const));
150
+ return IsObject(schema) && schema[Types.Kind] === 'Literal' && IsOptionalString(schema.$id) && (IsString(schema.const) || IsNumber(schema.const) || IsBoolean(schema.const));
136
151
  }
137
152
  TypeGuard.TLiteral = TLiteral;
138
153
  /** Returns true if the given schema is TNull */
139
154
  function TNull(schema) {
140
- return IsObject(schema) && schema[Types.Kind] === 'Null' && schema.type === 'null';
155
+ return IsObject(schema) && schema[Types.Kind] === 'Null' && schema.type === 'null' && IsOptionalString(schema.$id);
141
156
  }
142
157
  TypeGuard.TNull = TNull;
143
158
  /** Returns true if the given schema is TNumber */
@@ -145,6 +160,7 @@ var TypeGuard;
145
160
  return (IsObject(schema) &&
146
161
  schema[Types.Kind] === 'Number' &&
147
162
  schema.type === 'number' &&
163
+ IsOptionalString(schema.$id) &&
148
164
  IsOptionalNumber(schema.multipleOf) &&
149
165
  IsOptionalNumber(schema.minimum) &&
150
166
  IsOptionalNumber(schema.maximum) &&
@@ -157,6 +173,7 @@ var TypeGuard;
157
173
  if (!(IsObject(schema) &&
158
174
  schema[Types.Kind] === 'Object' &&
159
175
  schema.type === 'object' &&
176
+ IsOptionalString(schema.$id) &&
160
177
  IsObject(schema.properties) &&
161
178
  IsOptionalBoolean(schema.additionalProperties) &&
162
179
  IsOptionalNumber(schema.minProperties) &&
@@ -174,12 +191,12 @@ var TypeGuard;
174
191
  TypeGuard.TObject = TObject;
175
192
  /** Returns true if the given schema is TPromise */
176
193
  function TPromise(schema) {
177
- return IsObject(schema) && schema[Types.Kind] === 'Promise' && schema.type === 'promise' && TSchema(schema.item);
194
+ return IsObject(schema) && schema[Types.Kind] === 'Promise' && schema.type === 'promise' && IsOptionalString(schema.$id) && TSchema(schema.item);
178
195
  }
179
196
  TypeGuard.TPromise = TPromise;
180
197
  /** Returns true if the given schema is TRecord */
181
198
  function TRecord(schema) {
182
- if (!(IsObject(schema) && schema[Types.Kind] === 'Record' && schema.type === 'object' && schema.additionalProperties === false && IsObject(schema.patternProperties))) {
199
+ if (!(IsObject(schema) && schema[Types.Kind] === 'Record' && schema.type === 'object' && IsOptionalString(schema.$id) && schema.additionalProperties === false && IsObject(schema.patternProperties))) {
183
200
  return false;
184
201
  }
185
202
  const keys = Object.keys(schema.patternProperties);
@@ -197,22 +214,29 @@ var TypeGuard;
197
214
  TypeGuard.TRecord = TRecord;
198
215
  /** Returns true if the given schema is TSelf */
199
216
  function TSelf(schema) {
200
- return IsObject(schema) && schema[Types.Kind] === 'Self' && IsString(schema.$ref);
217
+ return IsObject(schema) && schema[Types.Kind] === 'Self' && IsOptionalString(schema.$id) && IsString(schema.$ref);
201
218
  }
202
219
  TypeGuard.TSelf = TSelf;
203
220
  /** Returns true if the given schema is TRef */
204
221
  function TRef(schema) {
205
- return IsObject(schema) && schema[Types.Kind] === 'Ref' && IsString(schema.$ref);
222
+ return IsObject(schema) && schema[Types.Kind] === 'Ref' && IsOptionalString(schema.$id) && IsString(schema.$ref);
206
223
  }
207
224
  TypeGuard.TRef = TRef;
208
225
  /** Returns true if the given schema is TString */
209
226
  function TString(schema) {
210
- return IsObject(schema) && schema[Types.Kind] === 'String' && schema.type === 'string' && IsOptionalNumber(schema.minLength) && IsOptionalNumber(schema.maxLength) && IsOptionalPattern(schema.pattern) && IsOptionalString(schema.format);
227
+ return (IsObject(schema) &&
228
+ schema[Types.Kind] === 'String' &&
229
+ schema.type === 'string' &&
230
+ IsOptionalString(schema.$id) &&
231
+ IsOptionalNumber(schema.minLength) &&
232
+ IsOptionalNumber(schema.maxLength) &&
233
+ IsOptionalPattern(schema.pattern) &&
234
+ IsOptionalString(schema.format));
211
235
  }
212
236
  TypeGuard.TString = TString;
213
237
  /** Returns true if the given schema is TTuple */
214
238
  function TTuple(schema) {
215
- if (!(IsObject(schema) && schema[Types.Kind] === 'Tuple' && schema.type === 'array' && IsNumber(schema.minItems) && IsNumber(schema.maxItems) && schema.minItems === schema.maxItems)) {
239
+ if (!(IsObject(schema) && schema[Types.Kind] === 'Tuple' && schema.type === 'array' && IsOptionalString(schema.$id) && IsNumber(schema.minItems) && IsNumber(schema.maxItems) && schema.minItems === schema.maxItems)) {
216
240
  return false;
217
241
  }
218
242
  if (schema.items === undefined && schema.additionalItems === undefined && schema.minItems === 0) {
@@ -230,12 +254,12 @@ var TypeGuard;
230
254
  TypeGuard.TTuple = TTuple;
231
255
  /** Returns true if the given schema is TUndefined */
232
256
  function TUndefined(schema) {
233
- return IsObject(schema) && schema[Types.Kind] === 'Undefined' && schema.type === 'object' && schema.specialized === 'Undefined';
257
+ return IsObject(schema) && schema[Types.Kind] === 'Undefined' && schema.type === 'object' && IsOptionalString(schema.$id) && schema.specialized === 'Undefined';
234
258
  }
235
259
  TypeGuard.TUndefined = TUndefined;
236
260
  /** Returns true if the given schema is TUnion */
237
261
  function TUnion(schema) {
238
- if (!(IsObject(schema) && schema[Types.Kind] === 'Union' && IsArray(schema.anyOf))) {
262
+ if (!(IsObject(schema) && schema[Types.Kind] === 'Union' && IsArray(schema.anyOf) && IsOptionalString(schema.$id))) {
239
263
  return false;
240
264
  }
241
265
  for (const inner of schema.anyOf) {
@@ -247,17 +271,23 @@ var TypeGuard;
247
271
  TypeGuard.TUnion = TUnion;
248
272
  /** Returns true if the given schema is TUint8Array */
249
273
  function TUint8Array(schema) {
250
- return IsObject(schema) && schema[Types.Kind] === 'Uint8Array' && schema.type === 'object' && schema.specialized === 'Uint8Array' && IsOptionalNumber(schema.minByteLength) && IsOptionalNumber(schema.maxByteLength);
274
+ return (IsObject(schema) &&
275
+ schema[Types.Kind] === 'Uint8Array' &&
276
+ schema.type === 'object' &&
277
+ IsOptionalString(schema.$id) &&
278
+ schema.specialized === 'Uint8Array' &&
279
+ IsOptionalNumber(schema.minByteLength) &&
280
+ IsOptionalNumber(schema.maxByteLength));
251
281
  }
252
282
  TypeGuard.TUint8Array = TUint8Array;
253
283
  /** Returns true if the given schema is TUnknown */
254
284
  function TUnknown(schema) {
255
- return IsObject(schema) && schema[Types.Kind] === 'Unknown';
285
+ return IsObject(schema) && schema[Types.Kind] === 'Unknown' && IsOptionalString(schema.$id);
256
286
  }
257
287
  TypeGuard.TUnknown = TUnknown;
258
288
  /** Returns true if the given schema is TVoid */
259
289
  function TVoid(schema) {
260
- return IsObject(schema) && schema[Types.Kind] === 'Void' && schema.type === 'null';
290
+ return IsObject(schema) && schema[Types.Kind] === 'Void' && schema.type === 'null' && IsOptionalString(schema.$id);
261
291
  }
262
292
  TypeGuard.TVoid = TVoid;
263
293
  /** Returns true if the given schema is TSchema */
@@ -285,4 +315,14 @@ var TypeGuard;
285
315
  TVoid(schema));
286
316
  }
287
317
  TypeGuard.TSchema = TSchema;
318
+ /** Asserts if this schema and associated references are valid. */
319
+ function Assert(schema, references = []) {
320
+ if (!TSchema(schema))
321
+ throw new TypeGuardInvalidTypeError(schema);
322
+ for (const schema of references) {
323
+ if (!TSchema(schema))
324
+ throw new TypeGuardInvalidTypeError(schema);
325
+ }
326
+ }
327
+ TypeGuard.Assert = Assert;
288
328
  })(TypeGuard = exports.TypeGuard || (exports.TypeGuard = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.24.26",
3
+ "version": "0.24.27",
4
4
  "description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",