Skip to content

Commit

Permalink
fix: emit equal when needed - alternative to #1853 (#1922)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
bhvngt and epoberezkin committed Mar 22, 2022
1 parent bd7cf15 commit 09f67f2
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/vocabularies/validation/enum.ts
Expand Up @@ -20,7 +20,9 @@ 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)
let eql: Name | undefined
const getEql = (): Name => (eql ??= useFunc(gen, equal))

let valid: Code
if (useLoop || $data) {
valid = gen.let("valid")
Expand All @@ -36,14 +38,14 @@ const def: CodeKeywordDefinition = {
function loopEnum(): void {
gen.assign(valid, false)
gen.forOf("v", schemaCode as Code, (v) =>
gen.if(_`${eql}(${data}, ${v})`, () => gen.assign(valid, true).break())
gen.if(_`${getEql()}(${data}, ${v})`, () => gen.assign(valid, true).break())
)
}

function equalCode(vSchema: Name, i: number): Code {
const sch = schema[i]
return typeof sch === "object" && sch !== null
? _`${eql}(${data}, ${vSchema}[${i}])`
? _`${getEql()}(${data}, ${vSchema}[${i}])`
: _`${data} === ${sch}`
}
},
Expand Down

0 comments on commit 09f67f2

Please sign in to comment.