Skip to content

Commit

Permalink
Optimize Union Encode Check
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Oct 17, 2023
1 parent 20f9fc0 commit 1ca9393
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/value/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,20 +403,19 @@ export namespace EncodeTransform {
return IsArray(schema.items) ? schema.items.map((schema, index) => Visit(schema, references, value1[index])) : []
}
function TUnion(schema: Types.TUnion, references: Types.TSchema[], value: any) {
const value1 = Default(schema, value)
// encoded union pass
// test value against union variants
for (const subschema of schema.anyOf) {
if (!checkFunction(subschema, references, value1)) continue
const value2 = Visit(subschema, references, value)
return Default(schema, value2)
if (!checkFunction(subschema, references, value)) continue
const value1 = Visit(subschema, references, value)
return Default(schema, value1)
}
// non-encoded union pass
// test transformed value against union variants
for (const subschema of schema.anyOf) {
const value2 = Visit(subschema, references, value)
if (!checkFunction(schema, references, value2)) continue
return Default(schema, value2)
const value1 = Visit(subschema, references, value)
if (!checkFunction(schema, references, value1)) continue
return Default(schema, value1)
}
return value1
return Default(schema, value)
}
function Visit(schema: Types.TSchema, references: Types.TSchema[], value: any): any {
const references_ = typeof schema.$id === 'string' ? [...references, schema] : references
Expand Down

0 comments on commit 1ca9393

Please sign in to comment.