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 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