From 2cce3661c31f74b8698d437e741df6481d9722cf Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Thu, 16 Dec 2021 09:42:48 -0700 Subject: [PATCH 1/5] only emit when needed: addresses #1852 --- lib/vocabularies/validation/enum.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/vocabularies/validation/enum.ts b/lib/vocabularies/validation/enum.ts index fa85373c4..20c9d621c 100644 --- a/lib/vocabularies/validation/enum.ts +++ b/lib/vocabularies/validation/enum.ts @@ -20,7 +20,8 @@ const def: CodeKeywordDefinition = { const {gen, data, $data, schema, schemaCode, it} = cxt if (!$data && schema.length === 0) throw new Error("enum must have non-empty array") const useLoop = schema.length >= it.opts.loopEnum - const eql = useFunc(gen, equal) + const needsEql = schema.some((x: unknown) => typeof x === "object") + const eql = (needsEql ? useFunc(gen, equal) : undefined) let valid: Code if (useLoop || $data) { valid = gen.let("valid") From ab9f27f57c325a048b4a7cbfdff6a96f4da47bb4 Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Thu, 16 Dec 2021 10:26:21 -0700 Subject: [PATCH 2/5] add separate check for null, since typeof null === 'object'; satisfy prettier --- lib/vocabularies/validation/enum.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/vocabularies/validation/enum.ts b/lib/vocabularies/validation/enum.ts index 20c9d621c..2065ec85d 100644 --- a/lib/vocabularies/validation/enum.ts +++ b/lib/vocabularies/validation/enum.ts @@ -20,8 +20,8 @@ const def: CodeKeywordDefinition = { const {gen, data, $data, schema, schemaCode, it} = cxt if (!$data && schema.length === 0) throw new Error("enum must have non-empty array") const useLoop = schema.length >= it.opts.loopEnum - const needsEql = schema.some((x: unknown) => typeof x === "object") - const eql = (needsEql ? useFunc(gen, equal) : undefined) + const needsEql = schema.some((x: unknown) => x !== null && typeof x === "object") + const eql = needsEql ? useFunc(gen, equal) : undefined let valid: Code if (useLoop || $data) { valid = gen.let("valid") From 7efd906cdd2707d0294c1f7a239ef67e4a4c5704 Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Thu, 16 Dec 2021 14:11:51 -0700 Subject: [PATCH 3/5] explicit cast schema to any[] to satisfy eslint --- lib/vocabularies/validation/enum.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/vocabularies/validation/enum.ts b/lib/vocabularies/validation/enum.ts index 2065ec85d..ae9a118c3 100644 --- a/lib/vocabularies/validation/enum.ts +++ b/lib/vocabularies/validation/enum.ts @@ -20,7 +20,7 @@ const def: CodeKeywordDefinition = { const {gen, data, $data, schema, schemaCode, it} = cxt if (!$data && schema.length === 0) throw new Error("enum must have non-empty array") const useLoop = schema.length >= it.opts.loopEnum - const needsEql = schema.some((x: unknown) => x !== null && typeof x === "object") + const needsEql = (schema as any[]).some((x: unknown) => x !== null && typeof x === "object") const eql = needsEql ? useFunc(gen, equal) : undefined let valid: Code if (useLoop || $data) { From 0ab046488c7512e3799b2c6d1ec6aad7ef9fe801 Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Thu, 16 Dec 2021 15:28:54 -0700 Subject: [PATCH 4/5] explicit check for isArray before calling some --- lib/vocabularies/validation/enum.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/vocabularies/validation/enum.ts b/lib/vocabularies/validation/enum.ts index ae9a118c3..205001d39 100644 --- a/lib/vocabularies/validation/enum.ts +++ b/lib/vocabularies/validation/enum.ts @@ -1,3 +1,4 @@ +/* eslint-disable */ import type {CodeKeywordDefinition, ErrorObject, KeywordErrorDefinition} from "../../types" import type {KeywordCxt} from "../../compile/validate" import {_, or, Name, Code} from "../../compile/codegen" @@ -20,7 +21,11 @@ const def: CodeKeywordDefinition = { const {gen, data, $data, schema, schemaCode, it} = cxt if (!$data && schema.length === 0) throw new Error("enum must have non-empty array") const useLoop = schema.length >= it.opts.loopEnum - const needsEql = (schema as any[]).some((x: unknown) => x !== null && typeof x === "object") + const needsEql = + useLoop || + $data || + !Array.isArray(schema) || + schema.some((x: unknown) => x !== null && typeof x === "object") const eql = needsEql ? useFunc(gen, equal) : undefined let valid: Code if (useLoop || $data) { From 201d81d2195480b1f3d7cd475824073629c56f29 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Wed, 22 Dec 2021 09:29:25 +0000 Subject: [PATCH 5/5] Update lib/vocabularies/validation/enum.ts --- lib/vocabularies/validation/enum.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/vocabularies/validation/enum.ts b/lib/vocabularies/validation/enum.ts index 205001d39..371d5d69c 100644 --- a/lib/vocabularies/validation/enum.ts +++ b/lib/vocabularies/validation/enum.ts @@ -23,8 +23,7 @@ const def: CodeKeywordDefinition = { const useLoop = schema.length >= it.opts.loopEnum const needsEql = useLoop || - $data || - !Array.isArray(schema) || + ($data && !Array.isArray(schema)) || schema.some((x: unknown) => x !== null && typeof x === "object") const eql = needsEql ? useFunc(gen, equal) : undefined let valid: Code