Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: emit equal when needed - alternative to #1853 #1922

Merged
merged 6 commits into from Mar 22, 2022
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/vocabularies/validation/enum.ts
Expand Up @@ -20,7 +20,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 eql = useFunc(gen, equal)
let eql: Name?
function getEql(): Name {
return (eql ??= useFunc(gen, equal))
}
epoberezkin marked this conversation as resolved.
Show resolved Hide resolved

let valid: Code
if (useLoop || $data) {
valid = gen.let("valid")
Expand All @@ -36,14 +40,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