@sinclair/typebox 0.25.8 → 0.25.10

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.
@@ -1,6 +1,6 @@
1
1
  import { ValueError } from '../errors/index';
2
2
  import * as Types from '../typebox';
3
- export declare type CheckFunction = (value: unknown) => boolean;
3
+ export type CheckFunction = (value: unknown) => boolean;
4
4
  export declare class TypeCheck<T extends Types.TSchema> {
5
5
  private readonly schema;
6
6
  private readonly references;
@@ -31,6 +31,7 @@ exports.TypeCompiler = exports.TypeCompilerUnknownTypeError = exports.Property =
31
31
  const index_1 = require("../errors/index");
32
32
  const index_2 = require("../guard/index");
33
33
  const index_3 = require("../format/index");
34
+ const index_4 = require("../custom/index");
34
35
  const Types = require("../typebox");
35
36
  // -------------------------------------------------------------------
36
37
  // TypeCheck
@@ -236,11 +237,11 @@ var TypeCompiler;
236
237
  // Reference: If we have seen this reference before we can just yield and return
237
238
  // the function call. If this isn't the case we defer to visit to generate and
238
239
  // set the function for subsequent passes. Consider for refactor.
239
- if (names.has(schema.$ref))
240
+ if (state_local_function_names.has(schema.$ref))
240
241
  return yield `(${CreateFunctionName(schema.$ref)}(${value}))`;
241
- if (!referenceMap.has(schema.$ref))
242
+ if (!state_reference_map.has(schema.$ref))
242
243
  throw Error(`TypeCompiler.Ref: Cannot de-reference schema with $id '${schema.$ref}'`);
243
- const reference = referenceMap.get(schema.$ref);
244
+ const reference = state_reference_map.get(schema.$ref);
244
245
  yield* Visit(reference, value);
245
246
  }
246
247
  function* Self(schema, value) {
@@ -291,12 +292,17 @@ var TypeCompiler;
291
292
  function* Void(schema, value) {
292
293
  yield `(${value} === null)`;
293
294
  }
295
+ function* UserDefined(schema, value) {
296
+ const schema_key = `schema_key_${state_remote_custom_types.size}`;
297
+ state_remote_custom_types.set(schema_key, schema);
298
+ yield `(custom('${schema[Types.Kind]}', '${schema_key}', ${value}))`;
299
+ }
294
300
  function* Visit(schema, value) {
295
301
  // Reference: Referenced schemas can originate from either additional schemas
296
302
  // or inline in the schema itself. Ideally the recursive path should align to
297
303
  // reference path. Consider for refactor.
298
- if (schema.$id && !names.has(schema.$id)) {
299
- names.add(schema.$id);
304
+ if (schema.$id && !state_local_function_names.has(schema.$id)) {
305
+ state_local_function_names.add(schema.$id);
300
306
  const name = CreateFunctionName(schema.$id);
301
307
  const body = CreateFunction(name, schema, 'value');
302
308
  PushFunction(body);
@@ -352,27 +358,31 @@ var TypeCompiler;
352
358
  case 'Void':
353
359
  return yield* Void(anySchema, value);
354
360
  default:
355
- throw new TypeCompilerUnknownTypeError(schema);
361
+ if (!index_4.Custom.Has(anySchema[Types.Kind]))
362
+ throw new TypeCompilerUnknownTypeError(schema);
363
+ return yield* UserDefined(anySchema, value);
356
364
  }
357
365
  }
358
366
  // -------------------------------------------------------------------
359
- // Compile State
367
+ // Compiler State
360
368
  // -------------------------------------------------------------------
361
- const referenceMap = new Map();
362
- const locals = new Set(); // local variables and functions
363
- const names = new Set(); // cache of local functions
369
+ const state_reference_map = new Map(); // tracks schemas with identifiers
370
+ const state_local_variables = new Set(); // local variables and functions
371
+ const state_local_function_names = new Set(); // local function names used call ref validators
372
+ const state_remote_custom_types = new Map(); // remote custom types used during compilation
364
373
  function ResetCompiler() {
365
- referenceMap.clear();
366
- locals.clear();
367
- names.clear();
374
+ state_reference_map.clear();
375
+ state_local_variables.clear();
376
+ state_local_function_names.clear();
377
+ state_remote_custom_types.clear();
368
378
  }
369
379
  function AddReferences(schemas = []) {
370
380
  for (const schema of schemas) {
371
381
  if (!schema.$id)
372
382
  throw new Error(`TypeCompiler: Referenced schemas must specify an $id.`);
373
- if (referenceMap.has(schema.$id))
383
+ if (state_reference_map.has(schema.$id))
374
384
  throw new Error(`TypeCompiler: Duplicate schema $id found for '${schema.$id}'`);
375
- referenceMap.set(schema.$id, schema);
385
+ state_reference_map.set(schema.$id, schema);
376
386
  }
377
387
  }
378
388
  function CreateExpression(schema, value) {
@@ -386,15 +396,15 @@ var TypeCompiler;
386
396
  return `function ${name}(value) {\n return (\n${expression}\n )\n}`;
387
397
  }
388
398
  function PushFunction(functionBody) {
389
- locals.add(functionBody);
399
+ state_local_variables.add(functionBody);
390
400
  }
391
401
  function PushLocal(expression) {
392
- const local = `local_${locals.size}`;
393
- locals.add(`const ${local} = ${expression}`);
402
+ const local = `local_${state_local_variables.size}`;
403
+ state_local_variables.add(`const ${local} = ${expression}`);
394
404
  return local;
395
405
  }
396
406
  function GetLocals() {
397
- return [...locals.values()];
407
+ return [...state_local_variables.values()];
398
408
  }
399
409
  // -------------------------------------------------------------------
400
410
  // Compile
@@ -410,14 +420,21 @@ var TypeCompiler;
410
420
  function Compile(schema, references = []) {
411
421
  index_2.TypeGuard.Assert(schema, references);
412
422
  const code = Build(schema, references);
413
- const func1 = globalThis.Function('format', code);
414
- const func2 = func1((format, value) => {
423
+ const custom_schemas = new Map(state_remote_custom_types);
424
+ const compiledFunction = globalThis.Function('custom', 'format', code);
425
+ const checkFunction = compiledFunction((kind, schema_key, value) => {
426
+ if (!index_4.Custom.Has(kind) || !custom_schemas.has(schema_key))
427
+ return false;
428
+ const schema = custom_schemas.get(schema_key);
429
+ const func = index_4.Custom.Get(kind);
430
+ return func(schema, value);
431
+ }, (format, value) => {
415
432
  if (!index_3.Format.Has(format))
416
433
  return false;
417
434
  const func = index_3.Format.Get(format);
418
435
  return func(value);
419
436
  });
420
- return new TypeCheck(schema, references, func2, code);
437
+ return new TypeCheck(schema, references, checkFunction, code);
421
438
  }
422
439
  TypeCompiler.Compile = Compile;
423
440
  })(TypeCompiler = exports.TypeCompiler || (exports.TypeCompiler = {}));
@@ -1,5 +1,5 @@
1
1
  import * as Types from '../typebox';
2
- export declare type TExtends<L extends Types.TSchema, R extends Types.TSchema, T extends Types.TSchema, U extends Types.TSchema> = Types.Static<L> extends Types.Static<R> ? T : U;
2
+ export type TExtends<L extends Types.TSchema, R extends Types.TSchema, T extends Types.TSchema, U extends Types.TSchema> = Types.Static<L> extends Types.Static<R> ? T : U;
3
3
  export interface TExclude<T extends Types.TUnion, U extends Types.TUnion> extends Types.TUnion<any[]> {
4
4
  static: Exclude<Types.Static<T, this['params']>, Types.Static<U, this['params']>>;
5
5
  }
@@ -49,14 +49,16 @@ var Structural;
49
49
  // ------------------------------------------------------------------------
50
50
  // Rules
51
51
  // ------------------------------------------------------------------------
52
- function AnyOrUnknownRule(right) {
52
+ function AnyUnknownOrCustomRule(right) {
53
53
  // https://github.com/microsoft/TypeScript/issues/40049
54
- if (right[Types.Kind] === 'Union' && right.anyOf.some((schema) => schema[Types.Kind] === 'Any' || schema[Types.Kind] === 'Unknown'))
54
+ if (guard_1.TypeGuard.TUnion(right) && right.anyOf.some((schema) => schema[Types.Kind] === 'Any' || schema[Types.Kind] === 'Unknown'))
55
55
  return true;
56
- if (right[Types.Kind] === 'Unknown')
56
+ if (guard_1.TypeGuard.TUnknown(right))
57
57
  return true;
58
- if (right[Types.Kind] === 'Any')
58
+ if (guard_1.TypeGuard.TAny(right))
59
59
  return true;
60
+ if (guard_1.TypeGuard.TUserDefined(right))
61
+ throw Error(`Structural: Cannot structurally compare custom type '${right[Types.Kind]}'`);
60
62
  return false;
61
63
  }
62
64
  function ObjectRightRule(left, right) {
@@ -132,10 +134,10 @@ var Structural;
132
134
  // Checks
133
135
  // ------------------------------------------------------------------------
134
136
  function Any(left, right) {
135
- return AnyOrUnknownRule(right) ? StructuralResult.True : StructuralResult.Union;
137
+ return AnyUnknownOrCustomRule(right) ? StructuralResult.True : StructuralResult.Union;
136
138
  }
137
139
  function Array(left, right) {
138
- if (AnyOrUnknownRule(right)) {
140
+ if (AnyUnknownOrCustomRule(right)) {
139
141
  return StructuralResult.True;
140
142
  }
141
143
  else if (guard_1.TypeGuard.TObject(right)) {
@@ -163,7 +165,7 @@ var Structural;
163
165
  }
164
166
  }
165
167
  function Boolean(left, right) {
166
- if (AnyOrUnknownRule(right)) {
168
+ if (AnyUnknownOrCustomRule(right)) {
167
169
  return StructuralResult.True;
168
170
  }
169
171
  else if (guard_1.TypeGuard.TObject(right) && ObjectRightRule(left, right)) {
@@ -180,7 +182,7 @@ var Structural;
180
182
  }
181
183
  }
182
184
  function Constructor(left, right) {
183
- if (AnyOrUnknownRule(right)) {
185
+ if (AnyUnknownOrCustomRule(right)) {
184
186
  return StructuralResult.True;
185
187
  }
186
188
  else if (guard_1.TypeGuard.TObject(right) && globalThis.Object.keys(right.properties).length === 0) {
@@ -205,7 +207,7 @@ var Structural;
205
207
  }
206
208
  }
207
209
  function Date(left, right) {
208
- if (AnyOrUnknownRule(right)) {
210
+ if (AnyUnknownOrCustomRule(right)) {
209
211
  return StructuralResult.True;
210
212
  }
211
213
  else if (guard_1.TypeGuard.TObject(right) && ObjectRightRule(left, right)) {
@@ -225,7 +227,7 @@ var Structural;
225
227
  }
226
228
  }
227
229
  function Function(left, right) {
228
- if (AnyOrUnknownRule(right)) {
230
+ if (AnyUnknownOrCustomRule(right)) {
229
231
  return StructuralResult.True;
230
232
  }
231
233
  else if (guard_1.TypeGuard.TObject(right)) {
@@ -254,7 +256,7 @@ var Structural;
254
256
  }
255
257
  }
256
258
  function Integer(left, right) {
257
- if (AnyOrUnknownRule(right)) {
259
+ if (AnyUnknownOrCustomRule(right)) {
258
260
  return StructuralResult.True;
259
261
  }
260
262
  else if (guard_1.TypeGuard.TObject(right) && ObjectRightRule(left, right)) {
@@ -271,7 +273,7 @@ var Structural;
271
273
  }
272
274
  }
273
275
  function Literal(left, right) {
274
- if (AnyOrUnknownRule(right)) {
276
+ if (AnyUnknownOrCustomRule(right)) {
275
277
  return StructuralResult.True;
276
278
  }
277
279
  else if (guard_1.TypeGuard.TObject(right) && ObjectRightRule(left, right)) {
@@ -308,7 +310,7 @@ var Structural;
308
310
  }
309
311
  }
310
312
  function Number(left, right) {
311
- if (AnyOrUnknownRule(right)) {
313
+ if (AnyUnknownOrCustomRule(right)) {
312
314
  return StructuralResult.True;
313
315
  }
314
316
  else if (guard_1.TypeGuard.TObject(right) && ObjectRightRule(left, right)) {
@@ -328,7 +330,7 @@ var Structural;
328
330
  }
329
331
  }
330
332
  function Null(left, right) {
331
- if (AnyOrUnknownRule(right)) {
333
+ if (AnyUnknownOrCustomRule(right)) {
332
334
  return StructuralResult.True;
333
335
  }
334
336
  else if (guard_1.TypeGuard.TNull(right)) {
@@ -356,7 +358,7 @@ var Structural;
356
358
  return StructuralResult.True;
357
359
  }
358
360
  function Object(left, right) {
359
- if (AnyOrUnknownRule(right)) {
361
+ if (AnyUnknownOrCustomRule(right)) {
360
362
  return StructuralResult.True;
361
363
  }
362
364
  else if (guard_1.TypeGuard.TObject(right)) {
@@ -375,7 +377,7 @@ var Structural;
375
377
  }
376
378
  }
377
379
  function Promise(left, right) {
378
- if (AnyOrUnknownRule(right)) {
380
+ if (AnyUnknownOrCustomRule(right)) {
379
381
  return StructuralResult.True;
380
382
  }
381
383
  else if (guard_1.TypeGuard.TObject(right)) {
@@ -395,7 +397,7 @@ var Structural;
395
397
  }
396
398
  }
397
399
  function Record(left, right) {
398
- if (AnyOrUnknownRule(right)) {
400
+ if (AnyUnknownOrCustomRule(right)) {
399
401
  return StructuralResult.True;
400
402
  }
401
403
  else if (guard_1.TypeGuard.TObject(right)) {
@@ -444,7 +446,7 @@ var Structural;
444
446
  return Visit(resolved, right);
445
447
  }
446
448
  function String(left, right) {
447
- if (AnyOrUnknownRule(right)) {
449
+ if (AnyUnknownOrCustomRule(right)) {
448
450
  return StructuralResult.True;
449
451
  }
450
452
  else if (guard_1.TypeGuard.TObject(right) && ObjectRightRule(left, right)) {
@@ -464,7 +466,7 @@ var Structural;
464
466
  }
465
467
  }
466
468
  function Tuple(left, right) {
467
- if (AnyOrUnknownRule(right)) {
469
+ if (AnyUnknownOrCustomRule(right)) {
468
470
  return StructuralResult.True;
469
471
  }
470
472
  else if (guard_1.TypeGuard.TObject(right)) {
@@ -508,7 +510,7 @@ var Structural;
508
510
  return StructuralResult.True;
509
511
  }
510
512
  function Uint8Array(left, right) {
511
- if (AnyOrUnknownRule(right)) {
513
+ if (AnyUnknownOrCustomRule(right)) {
512
514
  return StructuralResult.True;
513
515
  }
514
516
  else if (guard_1.TypeGuard.TObject(right) && ObjectRightRule(left, right)) {
@@ -528,7 +530,7 @@ var Structural;
528
530
  }
529
531
  }
530
532
  function Undefined(left, right) {
531
- if (AnyOrUnknownRule(right)) {
533
+ if (AnyUnknownOrCustomRule(right)) {
532
534
  return StructuralResult.True;
533
535
  }
534
536
  else if (guard_1.TypeGuard.TUndefined(right)) {
@@ -666,6 +668,9 @@ var Structural;
666
668
  else if (guard_1.TypeGuard.TVoid(left)) {
667
669
  return Void(left, resolvedRight);
668
670
  }
671
+ else if (guard_1.TypeGuard.TUserDefined(left)) {
672
+ throw Error(`Structural: Cannot structurally compare custom type '${left[Types.Kind]}'`);
673
+ }
669
674
  else {
670
675
  throw Error(`Structural: Unknown left operand '${left[Types.Kind]}'`);
671
676
  }
@@ -0,0 +1,12 @@
1
+ export type CustomValidationFunction<TSchema> = (schema: TSchema, value: unknown) => boolean;
2
+ /** Provides functions to create user defined types */
3
+ export declare namespace Custom {
4
+ /** Clears all user defined types */
5
+ function Clear(): void;
6
+ /** Returns true if this user defined type exists */
7
+ function Has(kind: string): boolean;
8
+ /** Sets a validation function for a user defined type */
9
+ function Set<TSchema = unknown>(kind: string, func: CustomValidationFunction<TSchema>): void;
10
+ /** Gets a custom validation function for a user defined type */
11
+ function Get(kind: string): CustomValidationFunction<any> | undefined;
12
+ }
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ /*--------------------------------------------------------------------------
3
+
4
+ @sinclair/typebox/custom
5
+
6
+ The MIT License (MIT)
7
+
8
+ Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in
18
+ all copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26
+ THE SOFTWARE.
27
+
28
+ ---------------------------------------------------------------------------*/
29
+ Object.defineProperty(exports, "__esModule", { value: true });
30
+ exports.Custom = void 0;
31
+ /** Provides functions to create user defined types */
32
+ var Custom;
33
+ (function (Custom) {
34
+ const customs = new Map();
35
+ /** Clears all user defined types */
36
+ function Clear() {
37
+ return customs.clear();
38
+ }
39
+ Custom.Clear = Clear;
40
+ /** Returns true if this user defined type exists */
41
+ function Has(kind) {
42
+ return customs.has(kind);
43
+ }
44
+ Custom.Has = Has;
45
+ /** Sets a validation function for a user defined type */
46
+ function Set(kind, func) {
47
+ customs.set(kind, func);
48
+ }
49
+ Custom.Set = Set;
50
+ /** Gets a custom validation function for a user defined type */
51
+ function Get(kind) {
52
+ return customs.get(kind);
53
+ }
54
+ Custom.Get = Get;
55
+ })(Custom = exports.Custom || (exports.Custom = {}));
@@ -0,0 +1 @@
1
+ export * from './custom';
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ /*--------------------------------------------------------------------------
3
+
4
+ @sinclair/typebox/custom
5
+
6
+ The MIT License (MIT)
7
+
8
+ Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in
18
+ all copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26
+ THE SOFTWARE.
27
+
28
+ ---------------------------------------------------------------------------*/
29
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
30
+ if (k2 === undefined) k2 = k;
31
+ var desc = Object.getOwnPropertyDescriptor(m, k);
32
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
33
+ desc = { enumerable: true, get: function() { return m[k]; } };
34
+ }
35
+ Object.defineProperty(o, k2, desc);
36
+ }) : (function(o, m, k, k2) {
37
+ if (k2 === undefined) k2 = k;
38
+ o[k2] = m[k];
39
+ }));
40
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
41
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
42
+ };
43
+ Object.defineProperty(exports, "__esModule", { value: true });
44
+ __exportStar(require("./custom"), exports);
@@ -47,7 +47,8 @@ export declare enum ValueErrorType {
47
47
  Uint8Array = 44,
48
48
  Uint8ArrayMinByteLength = 45,
49
49
  Uint8ArrayMaxByteLength = 46,
50
- Void = 47
50
+ Void = 47,
51
+ Custom = 48
51
52
  }
52
53
  export interface ValueError {
53
54
  type: ValueErrorType;
package/errors/errors.js CHANGED
@@ -30,6 +30,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
30
30
  exports.ValueErrors = exports.ValueErrorsUnknownTypeError = exports.ValueErrorType = void 0;
31
31
  const Types = require("../typebox");
32
32
  const index_1 = require("../format/index");
33
+ const index_2 = require("../custom/index");
33
34
  // -------------------------------------------------------------------
34
35
  // ValueErrorType
35
36
  // -------------------------------------------------------------------
@@ -83,6 +84,7 @@ var ValueErrorType;
83
84
  ValueErrorType[ValueErrorType["Uint8ArrayMinByteLength"] = 45] = "Uint8ArrayMinByteLength";
84
85
  ValueErrorType[ValueErrorType["Uint8ArrayMaxByteLength"] = 46] = "Uint8ArrayMaxByteLength";
85
86
  ValueErrorType[ValueErrorType["Void"] = 47] = "Void";
87
+ ValueErrorType[ValueErrorType["Custom"] = 48] = "Custom";
86
88
  })(ValueErrorType = exports.ValueErrorType || (exports.ValueErrorType = {}));
87
89
  // -------------------------------------------------------------------
88
90
  // ValueErrors
@@ -365,6 +367,12 @@ var ValueErrors;
365
367
  return yield { type: ValueErrorType.Void, schema, path, value, message: `Expected null` };
366
368
  }
367
369
  }
370
+ function* UserDefined(schema, references, path, value) {
371
+ const func = index_2.Custom.Get(schema[Types.Kind]);
372
+ if (!func(schema, value)) {
373
+ return yield { type: ValueErrorType.Custom, schema, path, value, message: `Expected kind ${schema[Types.Kind]}` };
374
+ }
375
+ }
368
376
  function* Visit(schema, references, path, value) {
369
377
  const anyReferences = schema.$id === undefined ? references : [schema, ...references];
370
378
  const anySchema = schema;
@@ -416,7 +424,9 @@ var ValueErrors;
416
424
  case 'Void':
417
425
  return yield* Void(anySchema, anyReferences, path, value);
418
426
  default:
419
- throw new ValueErrorsUnknownTypeError(schema);
427
+ if (!index_2.Custom.Has(anySchema[Types.Kind]))
428
+ throw new ValueErrorsUnknownTypeError(schema);
429
+ return yield* UserDefined(anySchema, anyReferences, path, value);
420
430
  }
421
431
  }
422
432
  function* Errors(schema, references, value) {
@@ -1,12 +1,12 @@
1
- export declare type FormatValidationFunction = (value: string) => boolean;
2
- /** Provides functions to create custom string formats */
1
+ export type FormatValidationFunction = (value: string) => boolean;
2
+ /** Provides functions to create user defined string formats */
3
3
  export declare namespace Format {
4
- /** Clears all formats */
4
+ /** Clears all user defined string formats */
5
5
  function Clear(): void;
6
- /** Returns true if the string format exists */
6
+ /** Returns true if the user defined string format exists */
7
7
  function Has(format: string): boolean;
8
- /** Sets a string format validation function */
8
+ /** Sets a validation function for a user defined string format */
9
9
  function Set(format: string, func: FormatValidationFunction): void;
10
- /** Gets a string format validation function */
10
+ /** Gets a validation function for a user defined string format */
11
11
  function Get(format: string): FormatValidationFunction | undefined;
12
12
  }
package/format/format.js CHANGED
@@ -28,26 +28,26 @@ THE SOFTWARE.
28
28
  ---------------------------------------------------------------------------*/
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
30
  exports.Format = void 0;
31
- /** Provides functions to create custom string formats */
31
+ /** Provides functions to create user defined string formats */
32
32
  var Format;
33
33
  (function (Format) {
34
34
  const formats = new Map();
35
- /** Clears all formats */
35
+ /** Clears all user defined string formats */
36
36
  function Clear() {
37
37
  return formats.clear();
38
38
  }
39
39
  Format.Clear = Clear;
40
- /** Returns true if the string format exists */
40
+ /** Returns true if the user defined string format exists */
41
41
  function Has(format) {
42
42
  return formats.has(format);
43
43
  }
44
44
  Format.Has = Has;
45
- /** Sets a string format validation function */
45
+ /** Sets a validation function for a user defined string format */
46
46
  function Set(format, func) {
47
47
  formats.set(format, func);
48
48
  }
49
49
  Format.Set = Set;
50
- /** Gets a string format validation function */
50
+ /** Gets a validation function for a user defined string format */
51
51
  function Get(format) {
52
52
  return formats.get(format);
53
53
  }
package/guard/guard.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as Types from '../typebox';
2
- export declare class TypeGuardInvalidTypeError extends Error {
2
+ export declare class TypeGuardUnknownTypeError extends Error {
3
3
  readonly schema: unknown;
4
4
  constructor(schema: unknown);
5
5
  }
@@ -51,6 +51,8 @@ export declare namespace TypeGuard {
51
51
  function TUnknown(schema: unknown): schema is Types.TUnknown;
52
52
  /** Returns true if the given schema is TVoid */
53
53
  function TVoid(schema: unknown): schema is Types.TVoid;
54
+ /** Returns true if the given schema is a registered user defined type */
55
+ function TUserDefined(schema: unknown): schema is Types.TSchema;
54
56
  /** Returns true if the given schema is TSchema */
55
57
  function TSchema(schema: unknown): schema is Types.TSchema;
56
58
  /** Asserts if this schema and associated references are valid. */
package/guard/guard.js CHANGED
@@ -27,15 +27,16 @@ THE SOFTWARE.
27
27
 
28
28
  ---------------------------------------------------------------------------*/
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.TypeGuard = exports.TypeGuardInvalidTypeError = void 0;
30
+ exports.TypeGuard = exports.TypeGuardUnknownTypeError = void 0;
31
+ const index_1 = require("../custom/index");
31
32
  const Types = require("../typebox");
32
- class TypeGuardInvalidTypeError extends Error {
33
+ class TypeGuardUnknownTypeError extends Error {
33
34
  constructor(schema) {
34
- super('TypeGuard: Invalid type');
35
+ super('TypeGuard: Unknown type');
35
36
  this.schema = schema;
36
37
  }
37
38
  }
38
- exports.TypeGuardInvalidTypeError = TypeGuardInvalidTypeError;
39
+ exports.TypeGuardUnknownTypeError = TypeGuardUnknownTypeError;
39
40
  /** Provides functionality to test if values are TypeBox types */
40
41
  var TypeGuard;
41
42
  (function (TypeGuard) {
@@ -393,6 +394,11 @@ var TypeGuard;
393
394
  IsOptionalString(schema.$id));
394
395
  }
395
396
  TypeGuard.TVoid = TVoid;
397
+ /** Returns true if the given schema is a registered user defined type */
398
+ function TUserDefined(schema) {
399
+ return IsObject(schema) && IsString(schema[Types.Kind]) && index_1.Custom.Has(schema[Types.Kind]);
400
+ }
401
+ TypeGuard.TUserDefined = TUserDefined;
396
402
  /** Returns true if the given schema is TSchema */
397
403
  function TSchema(schema) {
398
404
  return (TAny(schema) ||
@@ -417,16 +423,17 @@ var TypeGuard;
417
423
  TUnion(schema) ||
418
424
  TUint8Array(schema) ||
419
425
  TUnknown(schema) ||
420
- TVoid(schema));
426
+ TVoid(schema) ||
427
+ TUserDefined(schema));
421
428
  }
422
429
  TypeGuard.TSchema = TSchema;
423
430
  /** Asserts if this schema and associated references are valid. */
424
431
  function Assert(schema, references = []) {
425
432
  if (!TSchema(schema))
426
- throw new TypeGuardInvalidTypeError(schema);
433
+ throw new TypeGuardUnknownTypeError(schema);
427
434
  for (const schema of references) {
428
435
  if (!TSchema(schema))
429
- throw new TypeGuardInvalidTypeError(schema);
436
+ throw new TypeGuardUnknownTypeError(schema);
430
437
  }
431
438
  }
432
439
  TypeGuard.Assert = Assert;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.25.8",
3
+ "version": "0.25.10",
4
4
  "description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",
@@ -15,6 +15,7 @@
15
15
  "exports": {
16
16
  "./compiler": "./compiler/index.js",
17
17
  "./conditional": "./conditional/index.js",
18
+ "./custom": "./custom/index.js",
18
19
  "./errors": "./errors/index.js",
19
20
  "./format": "./format/index.js",
20
21
  "./guard": "./guard/index.js",
@@ -38,12 +39,12 @@
38
39
  "@sinclair/hammer": "^0.17.1",
39
40
  "@types/chai": "^4.3.3",
40
41
  "@types/mocha": "^9.1.1",
41
- "@types/node": "^18.7.13",
42
- "ajv": "^8.11.0",
42
+ "@types/node": "^18.11.9",
43
+ "ajv": "^8.11.2",
43
44
  "ajv-formats": "^2.1.1",
44
45
  "chai": "^4.3.6",
45
46
  "mocha": "^9.2.2",
46
47
  "prettier": "^2.7.1",
47
- "typescript": "^4.8.2"
48
+ "typescript": "^4.9.3"
48
49
  }
49
50
  }