@sinclair/typebox 0.34.36 → 0.34.38
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.
|
@@ -41,6 +41,11 @@ function ScoreUnion(schema, references, value) {
|
|
|
41
41
|
return acc + (literal + checks + exists);
|
|
42
42
|
}, 0);
|
|
43
43
|
}
|
|
44
|
+
else if (schema[index_3.Kind] === 'Union') {
|
|
45
|
+
const schemas = schema.anyOf.map((schema) => (0, index_7.Deref)(schema, references));
|
|
46
|
+
const scores = schemas.map((schema) => ScoreUnion(schema, references, value));
|
|
47
|
+
return Math.max(...scores);
|
|
48
|
+
}
|
|
44
49
|
else {
|
|
45
50
|
return (0, index_5.Check)(schema, references, value) ? 1 : 0;
|
|
46
51
|
}
|
|
@@ -196,6 +196,13 @@ function FromUndefined(schema, references, value) {
|
|
|
196
196
|
return TryConvertUndefined(value);
|
|
197
197
|
}
|
|
198
198
|
function FromUnion(schema, references, value) {
|
|
199
|
+
// Check if original value already matches one of the union variants
|
|
200
|
+
for (const subschema of schema.anyOf) {
|
|
201
|
+
if ((0, index_2.Check)(subschema, references, value)) {
|
|
202
|
+
return value;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
// Attempt conversion for each variant
|
|
199
206
|
for (const subschema of schema.anyOf) {
|
|
200
207
|
const converted = Visit(subschema, references, (0, index_1.Clone)(value));
|
|
201
208
|
if (!(0, index_2.Check)(subschema, references, converted))
|
|
@@ -35,6 +35,11 @@ function ScoreUnion(schema, references, value) {
|
|
|
35
35
|
return acc + (literal + checks + exists);
|
|
36
36
|
}, 0);
|
|
37
37
|
}
|
|
38
|
+
else if (schema[Kind] === 'Union') {
|
|
39
|
+
const schemas = schema.anyOf.map((schema) => Deref(schema, references));
|
|
40
|
+
const scores = schemas.map((schema) => ScoreUnion(schema, references, value));
|
|
41
|
+
return Math.max(...scores);
|
|
42
|
+
}
|
|
38
43
|
else {
|
|
39
44
|
return Check(schema, references, value) ? 1 : 0;
|
|
40
45
|
}
|
|
@@ -192,6 +192,13 @@ function FromUndefined(schema, references, value) {
|
|
|
192
192
|
return TryConvertUndefined(value);
|
|
193
193
|
}
|
|
194
194
|
function FromUnion(schema, references, value) {
|
|
195
|
+
// Check if original value already matches one of the union variants
|
|
196
|
+
for (const subschema of schema.anyOf) {
|
|
197
|
+
if (Check(subschema, references, value)) {
|
|
198
|
+
return value;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
// Attempt conversion for each variant
|
|
195
202
|
for (const subschema of schema.anyOf) {
|
|
196
203
|
const converted = Visit(subschema, references, Clone(value));
|
|
197
204
|
if (!Check(subschema, references, converted))
|