Skip to content

Commit

Permalink
feat: context data$Error method, separate error definition for $data …
Browse files Browse the repository at this point in the history
…errors
  • Loading branch information
epoberezkin committed Aug 29, 2020
1 parent 057cdfc commit cd2aeb7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
4 changes: 4 additions & 0 deletions lib/compile/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ export default class KeywordContext implements KeywordErrorContext {
;(append ? reportExtraError : reportError)(this, this.def.error || keywordError)
}

$dataError(): void {
reportError(this, this.def.$dataError || this.def.error || keywordError)
}

reset(): void {
if (this.errsCount === undefined) throw new Error('add "trackErrors" to keyword definition')
resetErrorsCount(this.gen, this.errsCount)
Expand Down
3 changes: 2 additions & 1 deletion lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ interface _KeywordDef {
metaSchema?: object
validateSchema?: ValidateFunction // compiled keyword metaSchema - should not be passed
dependencies?: string[] // keywords that must be present in the same schema
error?: KeywordErrorDefinition // TODO all keyword types should support error
error?: KeywordErrorDefinition
$dataError?: KeywordErrorDefinition
}

export interface CodeKeywordDefinition extends _KeywordDef {
Expand Down
17 changes: 8 additions & 9 deletions lib/vocabularies/validation/required.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const def: CodeKeywordDefinition = {
if (loopRequired) {
if ($data) {
gen.if(_`${schemaCode} !== undefined`, () => {
gen.if(_`!Array.isArray(${schemaCode})`, () => cxt.error(), loopAllRequired)
gen.if(_`Array.isArray(${schemaCode})`, loopAllRequired, () => cxt.$dataError())
})
} else {
loopAllRequired()
Expand All @@ -40,7 +40,7 @@ const def: CodeKeywordDefinition = {
gen.if(_`${schemaCode} === undefined`)
gen.assign(valid, true)
gen.elseIf(_`!Array.isArray(${schemaCode})`)
cxt.error()
cxt.$dataError()
gen.assign(valid, false)
gen.else()
loopUntilMissing(missing, valid)
Expand Down Expand Up @@ -76,13 +76,12 @@ const def: CodeKeywordDefinition = {
}
},
error: {
message: ({params: {missingProperty}}) => {
return missingProperty
? str`should have required property '${missingProperty}'`
: str`"required" keyword value must be array`
},
params: ({params: {missingProperty}}) =>
missingProperty ? _`{missingProperty: ${missingProperty}}` : _`{}`,
message: ({params: {missingProperty}}) =>
str`should have required property '${missingProperty}'`,
params: ({params: {missingProperty}}) => _`{missingProperty: ${missingProperty}}`,
},
$dataError: {
message: '"required" keyword value must be array',
},
}

Expand Down
11 changes: 6 additions & 5 deletions lib/vocabularies/validation/uniqueItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const def: CodeKeywordDefinition = {
gen.if(_`${schemaCode} === false || ${schemaCode} === undefined`)
gen.assign(valid, true)
gen.elseIf(_`typeof ${schemaCode} != "boolean"`)
cxt.error()
cxt.$dataError()
gen.assign(valid, false)
gen.else()
validateUniqueItems()
Expand Down Expand Up @@ -78,10 +78,11 @@ const def: CodeKeywordDefinition = {
},
error: {
message: ({params: {i, j}}) =>
i
? str`should NOT have duplicate items (items ## ${j} and ${i} are identical)`
: str`uniqueItems must be boolean ($data)`,
params: ({params: {i, j}}) => (i ? _`{i: ${i}, j: ${j}}` : _`{}`),
str`should NOT have duplicate items (items ## ${j} and ${i} are identical)`,
params: ({params: {i, j}}) => _`{i: ${i}, j: ${j}}`,
},
$dataError: {
message: "uniqueItems must be boolean ($data)",
},
}

Expand Down

0 comments on commit cd2aeb7

Please sign in to comment.