@sinclair/typebox 0.24.8 → 0.24.9

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.
@@ -14,6 +14,7 @@ export declare class TypeCheck<T extends Types.TSchema> {
14
14
  /** Returns true if the value matches the given type. */
15
15
  Check(value: unknown): value is Types.Static<T>;
16
16
  }
17
+ /** Compiles TypeBox Types for Runtime Type Checking */
17
18
  export declare namespace TypeCompiler {
18
19
  /** Compiles the given type for runtime type checking. This compiler only accepts known TypeBox types non-inclusive of unsafe types. */
19
20
  function Compile<T extends Types.TSchema>(schema: T, references?: Types.TSchema[]): TypeCheck<T>;
@@ -61,6 +61,7 @@ exports.TypeCheck = TypeCheck;
61
61
  // -------------------------------------------------------------------
62
62
  // TypeCompiler
63
63
  // -------------------------------------------------------------------
64
+ /** Compiles TypeBox Types for Runtime Type Checking */
64
65
  var TypeCompiler;
65
66
  (function (TypeCompiler) {
66
67
  // -------------------------------------------------------------------
@@ -275,7 +276,7 @@ var TypeCompiler;
275
276
  case 'Void':
276
277
  return yield* Void(anySchema, value);
277
278
  default:
278
- throw Error(`Unknown schema kind '${schema[Types.Kind]}'`);
279
+ throw new Error(`TypeCompiler: Unknown schema kind '${schema[Types.Kind]}'`);
279
280
  }
280
281
  }
281
282
  // -------------------------------------------------------------------
@@ -292,9 +293,9 @@ var TypeCompiler;
292
293
  function PushReferences(schemas = []) {
293
294
  for (const schema of schemas) {
294
295
  if (!schema.$id)
295
- throw Error(`Referenced schemas must specify an $id. Failed for '${JSON.stringify(schema)}'`);
296
+ throw new Error(`TypeCompiler: Referenced schemas must specify an $id.`);
296
297
  if (referenceMap.has(schema.$id))
297
- throw Error(`Duplicate schema $id detected for '${schema.$id}'`);
298
+ throw new Error(`TypeCompiler: Duplicate schema $id found for '${schema.$id}'`);
298
299
  referenceMap.set(schema.$id, schema);
299
300
  }
300
301
  }
@@ -1,2 +1,2 @@
1
+ export type { ValueError } from '../value/errors';
1
2
  export * from './compiler';
2
- export * from '../value/errors';
package/compiler/index.js CHANGED
@@ -38,4 +38,3 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
38
38
  };
39
39
  Object.defineProperty(exports, "__esModule", { value: true });
40
40
  __exportStar(require("./compiler"), exports);
41
- __exportStar(require("../value/errors"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.24.8",
3
+ "version": "0.24.9",
4
4
  "description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "json-schema",
package/readme.md CHANGED
@@ -554,7 +554,7 @@ type U = Static<typeof U> // type U = number | null
554
554
 
555
555
  ### Unsafe Types
556
556
 
557
- In some cases, you may need schema definitions that are not provided by TypeBox. In these scenarios, it's common to want to define your own schema and static type inference rules. The `Type.Unsafe(...)` function provides this functionality, allowing you to specify both schema representation and a static type to infer. Consider the following which defines a `number` schema, but will infer as a `string`.
557
+ In some scenarios, you may need specific schemas not provided by TypeBox. In these cases, it's common to want to define a custom schema with custom static inference rules. The `Type.Unsafe(...)` function provides this functionality. This function enables one to specify both schema representation and a static type to infer. Consider the following which defines a `number` schema but infers as `string`.
558
558
 
559
559
  ```typescript
560
560
  const T = Type.Unsafe<string>({ type: 'number' }) // const T = {
@@ -564,7 +564,7 @@ const T = Type.Unsafe<string>({ type: 'number' }) // const T = {
564
564
  type T = Static<typeof T> // type T = string
565
565
  ```
566
566
 
567
- The `Type.Unsafe(...)` function can be used with function generics to create custom schema representations for validators requiring specific schema representations. An example of which would be OpenAPI's `nullable` and `string-enum` representations which are not provided by TypeBox by default. The following demonstrates creating these schemas using the `Type.Unsafe(...)` function.
567
+ The `Type.Unsafe(...)` function can be combined with function generics to create user defined schemas for validators that need specific schema representations. An example of this might be the OpenAPI `nullable` and `string-enum` schema representations which are not provided by TypeBox. The following demonstrates creating these schemas using the `Type.Unsafe(...)` function.
568
568
 
569
569
  ```typescript
570
570
  import { Type, Static, TSchema } from '@sinclair/typebox'
package/typebox.js CHANGED
@@ -227,7 +227,7 @@ class TypeBuilder {
227
227
  /** Creates a reference schema */
228
228
  Ref(schema, options = {}) {
229
229
  if (schema.$id === undefined)
230
- throw Error('Cannot create reference schema as target schema as has no $id');
230
+ throw Error('Type.Ref: Referenced schema must specify an $id');
231
231
  return this.Create({ ...options, [exports.Kind]: 'Ref', $ref: schema.$id });
232
232
  }
233
233
  /** Creates a string type from a regular expression */
package/value/cast.js CHANGED
@@ -72,7 +72,6 @@ var UnionValueCast;
72
72
  })(UnionValueCast || (UnionValueCast = {}));
73
73
  var ValueCast;
74
74
  (function (ValueCast) {
75
- const ids = new Map();
76
75
  function Any(schema, references, value) {
77
76
  return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
78
77
  }
@@ -121,7 +120,6 @@ var ValueCast;
121
120
  return value;
122
121
  if (value === null || typeof value !== 'object')
123
122
  return create_1.ValueCreate.Create(schema, references);
124
- ids.set(schema.$id, schema);
125
123
  const required = new Set(schema.required || []);
126
124
  const result = {};
127
125
  for (const [key, property] of globalThis.Object.entries(schema.properties)) {
@@ -148,7 +146,7 @@ var ValueCast;
148
146
  return result;
149
147
  }
150
148
  function Recursive(schema, references, value) {
151
- throw Error('Cannot patch recursive schemas');
149
+ throw new Error('CastValue.Recursive: Cannot cast recursive schemas');
152
150
  }
153
151
  function Ref(schema, references, value) {
154
152
  const reference = references.find((reference) => reference.$id === schema.$ref);
package/value/check.js CHANGED
@@ -273,7 +273,7 @@ var ValueCheck;
273
273
  case 'Void':
274
274
  return Void(anySchema, anyReferences, value);
275
275
  default:
276
- throw Error(`Unknown schema kind '${schema[Types.Kind]}'`);
276
+ throw new Error(`CheckValue: Unknown schema kind '${schema[Types.Kind]}'`);
277
277
  }
278
278
  }
279
279
  // -------------------------------------------------------------------------
package/value/create.js CHANGED
@@ -70,8 +70,8 @@ var ValueCreate;
70
70
  return class {
71
71
  constructor() {
72
72
  for (const [key, val] of globalThis.Object.entries(value)) {
73
- const facade = this;
74
- facade[key] = val;
73
+ const self = this;
74
+ self[key] = val;
75
75
  }
76
76
  }
77
77
  };
@@ -169,7 +169,7 @@ var ValueCreate;
169
169
  return schema.default;
170
170
  }
171
171
  else {
172
- throw new Error('Rec types require a default value');
172
+ throw new Error('ValueCreate.Recursive: Recursive types require a default value');
173
173
  }
174
174
  }
175
175
  function Ref(schema, references) {
@@ -197,7 +197,7 @@ var ValueCreate;
197
197
  function String(schema, references) {
198
198
  if (schema.pattern !== undefined) {
199
199
  if (schema.default === undefined) {
200
- throw Error('String types with patterns must specify a default value');
200
+ throw new Error('ValueCreate.String: String types with patterns must specify a default value');
201
201
  }
202
202
  else {
203
203
  return schema.default;
@@ -231,7 +231,7 @@ var ValueCreate;
231
231
  return schema.default;
232
232
  }
233
233
  else if (schema.anyOf.length === 0) {
234
- throw Error('Cannot generate Union with empty set');
234
+ throw new Error('ValueCreate: Cannot create Union with zero variants');
235
235
  }
236
236
  else {
237
237
  return ValueCreate.Create(schema.anyOf[0], references);
@@ -311,7 +311,7 @@ var ValueCreate;
311
311
  case 'Void':
312
312
  return Void(anySchema, anyReferences);
313
313
  default:
314
- throw Error(`Unknown schema kind '${schema[Types.Kind]}'`);
314
+ throw new Error(`ValueCreate: Unknown schema kind '${schema[Types.Kind]}'`);
315
315
  }
316
316
  }
317
317
  ValueCreate.Visit = Visit;
package/value/errors.js CHANGED
@@ -159,13 +159,13 @@ var ValueErrors;
159
159
  function* Ref(schema, references, path, value) {
160
160
  const reference = references.find((reference) => reference.$id === schema.$ref);
161
161
  if (reference === undefined)
162
- throw new Error(`CheckValue.Ref: Cannot find schema with $id '${schema.$ref}'.`);
162
+ throw new Error(`ValueErrors.Ref: Cannot find schema with $id '${schema.$ref}'.`);
163
163
  yield* Visit(reference, references, path, value);
164
164
  }
165
165
  function* Self(schema, references, path, value) {
166
166
  const reference = references.find((reference) => reference.$id === schema.$ref);
167
167
  if (reference === undefined)
168
- throw new Error(`CheckValue.Ref: Cannot find schema with $id '${schema.$ref}'.`);
168
+ throw new Error(`ValueErrors.Self: Cannot find schema with $id '${schema.$ref}'.`);
169
169
  yield* Visit(reference, references, path, value);
170
170
  }
171
171
  function* String(schema, references, path, value) {
@@ -280,7 +280,7 @@ var ValueErrors;
280
280
  case 'Void':
281
281
  return yield* Void(anySchema, anyReferences, path, value);
282
282
  default:
283
- throw Error(`Unknown schema kind '${schema[Types.Kind]}'`);
283
+ throw new Error(`ValueErrors: Unknown schema kind '${schema[Types.Kind]}'`);
284
284
  }
285
285
  }
286
286
  function* Errors(schema, references, value) {
package/value/index.d.ts CHANGED
@@ -1 +1,2 @@
1
+ export type { ValueError } from './errors';
1
2
  export * from './value';