Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/vocabularies/validation/enum.ts
Original file line number Diff line number Diff line change
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