@sinclair/typebox 0.32.30 → 0.32.31

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.
@@ -45,8 +45,9 @@ function ScoreUnion(schema, references, value) {
45
45
  }
46
46
  }
47
47
  function SelectUnion(union, references, value) {
48
- let [select, best] = [union.anyOf[0], 0];
49
- for (const schema of union.anyOf) {
48
+ const schemas = union.anyOf.map((schema) => (0, index_7.Deref)(schema, references));
49
+ let [select, best] = [schemas[0], 0];
50
+ for (const schema of schemas) {
50
51
  const score = ScoreUnion(schema, references, value);
51
52
  if (score > best) {
52
53
  select = schema;
@@ -7,4 +7,4 @@ export declare class TypeDereferenceError extends TypeBoxError {
7
7
  constructor(schema: TRef | TThis);
8
8
  }
9
9
  /** Dereferences a schema from the references array or throws if not found */
10
- export declare function Deref(schema: TRef | TThis, references: TSchema[]): TSchema;
10
+ export declare function Deref(schema: TSchema, references: TSchema[]): TSchema;
@@ -3,6 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.Deref = exports.TypeDereferenceError = void 0;
5
5
  const index_1 = require("../../type/error/index");
6
+ const index_2 = require("../../type/symbols/index");
6
7
  class TypeDereferenceError extends index_1.TypeBoxError {
7
8
  constructor(schema) {
8
9
  super(`Unable to dereference schema with $id '${schema.$id}'`);
@@ -10,11 +11,17 @@ class TypeDereferenceError extends index_1.TypeBoxError {
10
11
  }
11
12
  }
12
13
  exports.TypeDereferenceError = TypeDereferenceError;
14
+ function Resolve(schema, references) {
15
+ const target = references.find((target) => target.$id === schema.$ref);
16
+ if (target === undefined)
17
+ throw new TypeDereferenceError(schema);
18
+ return Deref(target, references);
19
+ }
13
20
  /** Dereferences a schema from the references array or throws if not found */
14
21
  function Deref(schema, references) {
15
- const index = references.findIndex((target) => target.$id === schema.$ref);
16
- if (index === -1)
17
- throw new TypeDereferenceError(schema);
18
- return references[index];
22
+ // prettier-ignore
23
+ return (schema[index_2.Kind] === 'This' || schema[index_2.Kind] === 'Ref')
24
+ ? Resolve(schema, references)
25
+ : schema;
19
26
  }
20
27
  exports.Deref = Deref;
@@ -41,8 +41,9 @@ function ScoreUnion(schema, references, value) {
41
41
  }
42
42
  }
43
43
  function SelectUnion(union, references, value) {
44
- let [select, best] = [union.anyOf[0], 0];
45
- for (const schema of union.anyOf) {
44
+ const schemas = union.anyOf.map((schema) => Deref(schema, references));
45
+ let [select, best] = [schemas[0], 0];
46
+ for (const schema of schemas) {
46
47
  const score = ScoreUnion(schema, references, value);
47
48
  if (score > best) {
48
49
  select = schema;
@@ -7,4 +7,4 @@ export declare class TypeDereferenceError extends TypeBoxError {
7
7
  constructor(schema: TRef | TThis);
8
8
  }
9
9
  /** Dereferences a schema from the references array or throws if not found */
10
- export declare function Deref(schema: TRef | TThis, references: TSchema[]): TSchema;
10
+ export declare function Deref(schema: TSchema, references: TSchema[]): TSchema;
@@ -1,4 +1,5 @@
1
1
  import { TypeBoxError } from '../../type/error/index.mjs';
2
+ import { Kind } from '../../type/symbols/index.mjs';
2
3
  export class TypeDereferenceError extends TypeBoxError {
3
4
  schema;
4
5
  constructor(schema) {
@@ -6,10 +7,16 @@ export class TypeDereferenceError extends TypeBoxError {
6
7
  this.schema = schema;
7
8
  }
8
9
  }
10
+ function Resolve(schema, references) {
11
+ const target = references.find((target) => target.$id === schema.$ref);
12
+ if (target === undefined)
13
+ throw new TypeDereferenceError(schema);
14
+ return Deref(target, references);
15
+ }
9
16
  /** Dereferences a schema from the references array or throws if not found */
10
17
  export function Deref(schema, references) {
11
- const index = references.findIndex((target) => target.$id === schema.$ref);
12
- if (index === -1)
13
- throw new TypeDereferenceError(schema);
14
- return references[index];
18
+ // prettier-ignore
19
+ return (schema[Kind] === 'This' || schema[Kind] === 'Ref')
20
+ ? Resolve(schema, references)
21
+ : schema;
15
22
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.32.30",
3
+ "version": "0.32.31",
4
4
  "description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",