@sinclair/typebox 0.32.29 → 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.
- package/build/cjs/value/cast/cast.js +3 -2
- package/build/cjs/value/deref/deref.d.ts +1 -1
- package/build/cjs/value/deref/deref.js +11 -4
- package/build/cjs/value/guard/guard.js +1 -1
- package/build/esm/value/cast/cast.mjs +3 -2
- package/build/esm/value/deref/deref.d.mts +1 -1
- package/build/esm/value/deref/deref.mjs +11 -4
- package/build/esm/value/guard/guard.mjs +1 -1
- package/package.json +1 -1
|
@@ -45,8 +45,9 @@ function ScoreUnion(schema, references, value) {
|
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
function SelectUnion(union, references, value) {
|
|
48
|
-
|
|
49
|
-
|
|
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:
|
|
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
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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;
|
|
@@ -20,7 +20,7 @@ exports.IsIterator = IsIterator;
|
|
|
20
20
|
// --------------------------------------------------------------------------
|
|
21
21
|
/** Returns true if this value is not an instance of a class */
|
|
22
22
|
function IsStandardObject(value) {
|
|
23
|
-
return IsObject(value) &&
|
|
23
|
+
return IsObject(value) && (Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null);
|
|
24
24
|
}
|
|
25
25
|
exports.IsStandardObject = IsStandardObject;
|
|
26
26
|
/** Returns true if this value is an instance of a class */
|
|
@@ -41,8 +41,9 @@ function ScoreUnion(schema, references, value) {
|
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
function SelectUnion(union, references, value) {
|
|
44
|
-
|
|
45
|
-
|
|
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:
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
18
|
+
// prettier-ignore
|
|
19
|
+
return (schema[Kind] === 'This' || schema[Kind] === 'Ref')
|
|
20
|
+
? Resolve(schema, references)
|
|
21
|
+
: schema;
|
|
15
22
|
}
|
|
@@ -14,7 +14,7 @@ export function IsIterator(value) {
|
|
|
14
14
|
// --------------------------------------------------------------------------
|
|
15
15
|
/** Returns true if this value is not an instance of a class */
|
|
16
16
|
export function IsStandardObject(value) {
|
|
17
|
-
return IsObject(value) &&
|
|
17
|
+
return IsObject(value) && (Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null);
|
|
18
18
|
}
|
|
19
19
|
/** Returns true if this value is an instance of a class */
|
|
20
20
|
export function IsInstanceObject(value) {
|