Skip to content

Commit 09f67f2

Browse files
bhvngtepoberezkin
andauthoredMar 22, 2022
fix: emit equal when needed - alternative to #1853 (#1922)
* fix: emit equal when needed - alternative to #1853 * fix: prettier errors * fix: tslint errors * Update lib/vocabularies/validation/enum.ts * Update lib/vocabularies/validation/enum.ts Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com>
1 parent bd7cf15 commit 09f67f2

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed
 

‎lib/vocabularies/validation/enum.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ const def: CodeKeywordDefinition = {
2020
const {gen, data, $data, schema, schemaCode, it} = cxt
2121
if (!$data && schema.length === 0) throw new Error("enum must have non-empty array")
2222
const useLoop = schema.length >= it.opts.loopEnum
23-
const eql = useFunc(gen, equal)
23+
let eql: Name | undefined
24+
const getEql = (): Name => (eql ??= useFunc(gen, equal))
25+
2426
let valid: Code
2527
if (useLoop || $data) {
2628
valid = gen.let("valid")
@@ -36,14 +38,14 @@ const def: CodeKeywordDefinition = {
3638
function loopEnum(): void {
3739
gen.assign(valid, false)
3840
gen.forOf("v", schemaCode as Code, (v) =>
39-
gen.if(_`${eql}(${data}, ${v})`, () => gen.assign(valid, true).break())
41+
gen.if(_`${getEql()}(${data}, ${v})`, () => gen.assign(valid, true).break())
4042
)
4143
}
4244

4345
function equalCode(vSchema: Name, i: number): Code {
4446
const sch = schema[i]
4547
return typeof sch === "object" && sch !== null
46-
? _`${eql}(${data}, ${vSchema}[${i}])`
48+
? _`${getEql()}(${data}, ${vSchema}[${i}])`
4749
: _`${data} === ${sch}`
4850
}
4951
},

0 commit comments

Comments
 (0)
Please sign in to comment.