Skip to content

Commit

Permalink
fix: skipped required test
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Aug 28, 2020
1 parent 3ab1066 commit da9bed3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 34 deletions.
53 changes: 21 additions & 32 deletions lib/vocabularies/validation/required.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,49 +12,25 @@ const def: CodeKeywordDefinition = {
code(cxt: KeywordContext) {
const {gen, schema, schemaCode, data, $data, it} = cxt
if (!$data && schema.length === 0) return

const loopRequired = $data || schema.length >= <number>it.opts.loopRequired

if (it.allErrors) allErrorsMode()
else exitOnErrorMode()

function allErrorsMode(): void {
if (loopRequired) {
if ($data) {
gen.if(
`${schemaCode} && !Array.isArray(${schemaCode})`,
() => cxt.error(),
() => gen.if(`${schemaCode} !== undefined`, loopAllRequired)
)
} else {
loopAllRequired()
}
} else {
for (const prop of schema) {
checkReportMissingProp(cxt, prop)
}
check$DataAnd(loopAllRequired)
return
}
for (const prop of schema) {
checkReportMissingProp(cxt, prop)
}
}

function exitOnErrorMode(): void {
const missing = gen.let("missing")
cxt.setParams({missingProperty: missing})

if (loopRequired) {
const valid = gen.let("valid", true)

// TODO refactor and enable/fix test in errors.spec.js line 301
// it can be simpler once blocks are globally supported - endIf can be removed, so there will be 2 open blocks
if ($data) {
gen.if(`${schemaCode} === undefined`, `${valid} = true`, () =>
gen.if(`!Array.isArray(${schemaCode})`, `${valid} = false`, () =>
loopUntilMissing(missing, valid)
)
)
} else {
loopUntilMissing(missing, valid)
}

check$DataAnd(() => loopUntilMissing(missing, valid))
cxt.pass(valid)
} else {
gen.if(`${checkMissingProp(cxt, schema, missing)}`)
Expand All @@ -63,6 +39,18 @@ const def: CodeKeywordDefinition = {
}
}

function check$DataAnd(loop: () => void): void {
if ($data) {
gen.if(
`${schemaCode} && !Array.isArray(${schemaCode})`,
() => cxt.error(),
() => gen.if(`${schemaCode} !== undefined`, loop)
)
} else {
loop()
}
}

function loopAllRequired(): void {
const prop = gen.name("prop")
cxt.setParams({missingProperty: prop})
Expand All @@ -72,11 +60,12 @@ const def: CodeKeywordDefinition = {
}

function loopUntilMissing(missing: Name, valid: Name): void {
gen.for(`${missing} of ${schemaCode}`, () =>
cxt.setParams({missingProperty: missing})
gen.for(`${missing} of ${schemaCode}`, () => {
gen
.assign(valid, propertyInData(data, missing, it.opts.ownProperties))
.ifNot(valid, "break")
)
})
}
},
error: {
Expand Down
2 changes: 1 addition & 1 deletion lib/vocabularies/validation/uniqueItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const def: CodeKeywordDefinition = {
.code(_`outer:`)
.for(_`;${i}--;`, () =>
gen.for(_`${j} = ${i}; ${j}--;`, () =>
gen.if(`equal(${data}[${i}], ${data}[${j}])`, _`${valid} = false; break outer;`)
gen.if(_`equal(${data}[${i}], ${data}[${j}])`, _`${valid} = false; break outer;`)
)
)
}
Expand Down
2 changes: 1 addition & 1 deletion spec/errors.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ describe("Validation errors", () => {
})

it("should show different error when required is $data of incorrect type", () => {
// test(new Ajv({$data: true}))
test(new Ajv({$data: true}))
test(new Ajv({$data: true, allErrors: true}))

function test(_ajv) {
Expand Down

0 comments on commit da9bed3

Please sign in to comment.