From 1ca93939e97ee7f2a625496563b8523c16424198 Mon Sep 17 00:00:00 2001 From: sinclair Date: Tue, 17 Oct 2023 22:47:33 +0900 Subject: [PATCH] Optimize Union Encode Check --- src/value/transform.ts | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/value/transform.ts b/src/value/transform.ts index 5e278bfd9..281917f18 100644 --- a/src/value/transform.ts +++ b/src/value/transform.ts @@ -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