@sinclair/typebox 0.34.39 → 0.34.40
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 +10 -10
- package/build/esm/value/cast/cast.mjs +10 -10
- package/package.json +1 -1
|
@@ -21,23 +21,23 @@ class ValueCastError extends index_2.TypeBoxError {
|
|
|
21
21
|
}
|
|
22
22
|
exports.ValueCastError = ValueCastError;
|
|
23
23
|
// ------------------------------------------------------------------
|
|
24
|
-
// The following
|
|
25
|
-
//
|
|
26
|
-
//
|
|
27
|
-
//
|
|
28
|
-
//
|
|
29
|
-
//
|
|
24
|
+
// The following logic assigns a score to a schema based on how well
|
|
25
|
+
// it matches a given value. For object types, the score is calculated
|
|
26
|
+
// by evaluating each property of the value against the schema's
|
|
27
|
+
// properties. To avoid bias towards objects with many properties,
|
|
28
|
+
// each property contributes equally to the total score. Properties
|
|
29
|
+
// that exactly match literal values receive the highest possible
|
|
30
|
+
// score, as literals are often used as discriminators in union types.
|
|
30
31
|
// ------------------------------------------------------------------
|
|
31
32
|
function ScoreUnion(schema, references, value) {
|
|
32
33
|
if (schema[index_3.Kind] === 'Object' && typeof value === 'object' && !(0, index_1.IsNull)(value)) {
|
|
33
34
|
const object = schema;
|
|
34
35
|
const keys = Object.getOwnPropertyNames(value);
|
|
35
36
|
const entries = Object.entries(object.properties);
|
|
36
|
-
const [point, max] = [1 / entries.length, entries.length];
|
|
37
37
|
return entries.reduce((acc, [key, schema]) => {
|
|
38
|
-
const literal = schema[index_3.Kind] === 'Literal' && schema.const === value[key] ?
|
|
39
|
-
const checks = (0, index_5.Check)(schema, references, value[key]) ?
|
|
40
|
-
const exists = keys.includes(key) ?
|
|
38
|
+
const literal = schema[index_3.Kind] === 'Literal' && schema.const === value[key] ? 100 : 0;
|
|
39
|
+
const checks = (0, index_5.Check)(schema, references, value[key]) ? 10 : 0;
|
|
40
|
+
const exists = keys.includes(key) ? 1 : 0;
|
|
41
41
|
return acc + (literal + checks + exists);
|
|
42
42
|
}, 0);
|
|
43
43
|
}
|
|
@@ -15,23 +15,23 @@ export class ValueCastError extends TypeBoxError {
|
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
17
|
// ------------------------------------------------------------------
|
|
18
|
-
// The following
|
|
19
|
-
//
|
|
20
|
-
//
|
|
21
|
-
//
|
|
22
|
-
//
|
|
23
|
-
//
|
|
18
|
+
// The following logic assigns a score to a schema based on how well
|
|
19
|
+
// it matches a given value. For object types, the score is calculated
|
|
20
|
+
// by evaluating each property of the value against the schema's
|
|
21
|
+
// properties. To avoid bias towards objects with many properties,
|
|
22
|
+
// each property contributes equally to the total score. Properties
|
|
23
|
+
// that exactly match literal values receive the highest possible
|
|
24
|
+
// score, as literals are often used as discriminators in union types.
|
|
24
25
|
// ------------------------------------------------------------------
|
|
25
26
|
function ScoreUnion(schema, references, value) {
|
|
26
27
|
if (schema[Kind] === 'Object' && typeof value === 'object' && !IsNull(value)) {
|
|
27
28
|
const object = schema;
|
|
28
29
|
const keys = Object.getOwnPropertyNames(value);
|
|
29
30
|
const entries = Object.entries(object.properties);
|
|
30
|
-
const [point, max] = [1 / entries.length, entries.length];
|
|
31
31
|
return entries.reduce((acc, [key, schema]) => {
|
|
32
|
-
const literal = schema[Kind] === 'Literal' && schema.const === value[key] ?
|
|
33
|
-
const checks = Check(schema, references, value[key]) ?
|
|
34
|
-
const exists = keys.includes(key) ?
|
|
32
|
+
const literal = schema[Kind] === 'Literal' && schema.const === value[key] ? 100 : 0;
|
|
33
|
+
const checks = Check(schema, references, value[key]) ? 10 : 0;
|
|
34
|
+
const exists = keys.includes(key) ? 1 : 0;
|
|
35
35
|
return acc + (literal + checks + exists);
|
|
36
36
|
}, 0);
|
|
37
37
|
}
|