Skip to content

Commit

Permalink
JTD: improve error reporting, remove extra error for properties
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Feb 10, 2021
1 parent d6acee4 commit 9e5f247
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 26 deletions.
10 changes: 2 additions & 8 deletions lib/compile/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@ import {SchemaCxt, SchemaObjCxt} from "./index"
import {JSONType} from "./rules"
import {checkDataTypes, DataType} from "./validate/dataType"
import {schemaRefOrVal, unescapeJsonPointer, mergeEvaluated} from "./util"
import {
reportError,
reportExtraError,
resetErrorsCount,
keywordError,
keyword$DataError,
} from "./errors"
import {reportError, reportExtraError, resetErrorsCount, keyword$DataError} from "./errors"
import {CodeGen, _, nil, or, not, getProperty, Code, Name} from "./codegen"
import N from "./names"
import {applySubschema, SubschemaArgs} from "./subschema"
Expand Down Expand Up @@ -102,7 +96,7 @@ export default class KeywordCxt implements KeywordErrorCxt {
}

error(append?: true): void {
;(append ? reportExtraError : reportError)(this, this.def.error || keywordError)
;(append ? reportExtraError : reportError)(this, this.def.error)
}

$dataError(): void {
Expand Down
9 changes: 6 additions & 3 deletions lib/compile/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const keyword$DataError: KeywordErrorDefinition = {

export function reportError(
cxt: KeywordErrorCxt,
error: KeywordErrorDefinition,
error: KeywordErrorDefinition = keywordError,
overrideAllErrors?: boolean
): void {
const {it} = cxt
Expand All @@ -30,7 +30,10 @@ export function reportError(
}
}

export function reportExtraError(cxt: KeywordErrorCxt, error: KeywordErrorDefinition): void {
export function reportExtraError(
cxt: KeywordErrorCxt,
error: KeywordErrorDefinition = keywordError
): void {
const {it} = cxt
const {gen, compositeRule, allErrors} = it
const errObj = errorObjectCode(cxt, error)
Expand Down Expand Up @@ -110,7 +113,7 @@ const E = {
function errorObjectCode(cxt: KeywordErrorCxt, error: KeywordErrorDefinition): Code {
const {createErrors, opts} = cxt.it
if (createErrors === false) return _`{}`
return opts.jtd && !opts.ajvErrors ? jtdErrorObject(cxt, error) : ajvErrorObject(cxt, error)
return (opts.jtd && !opts.ajvErrors ? jtdErrorObject : ajvErrorObject)(cxt, error)
}

function jtdErrorObject(cxt: KeywordErrorCxt, {message}: KeywordErrorDefinition): Code {
Expand Down
21 changes: 14 additions & 7 deletions lib/vocabularies/jtd/properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,27 @@ export function validateProperties(cxt: KeywordCxt): void {
}

function validateProps(props: string[], keyword: string, required?: boolean): void {
const _valid = gen.var("valid")
for (const prop of props) {
gen.if(
propertyInData(data, prop, it.opts.ownProperties),
() => gen.assign(valid, and(valid, applyPropertySchema(prop, keyword))),
required ? () => gen.assign(valid, false) : undefined
() => applyPropertySchema(prop, keyword, _valid),
missingProperty
)
cxt.ok(valid)
cxt.ok(_valid)
}

function missingProperty(): void {
if (required) {
gen.assign(_valid, false)
cxt.error()
} else {
gen.assign(_valid, true)
}
}
}

function applyPropertySchema(prop: string, keyword: string): Name {
const _valid = gen.name("valid")
function applyPropertySchema(prop: string, keyword: string, _valid: Name): void {
cxt.subschema(
{
keyword,
Expand All @@ -80,7 +89,6 @@ export function validateProperties(cxt: KeywordCxt): void {
},
_valid
)
return _valid
}

function validateAdditional(): void {
Expand All @@ -97,7 +105,6 @@ export function validateProperties(cxt: KeywordCxt): void {
} else {
// cxt.setParams({additionalProperty: key})
cxt.error()
gen.assign(valid, false)
if (!it.opts.allErrors) gen.break()
}
})
Expand Down
4 changes: 2 additions & 2 deletions lib/vocabularies/jtd/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const def: CodeKeywordDefinition = {
gen.var(valid, false)
validateJtdRef()
}
cxt.pass(valid)
cxt.ok(valid)

function validateJtdRef(): void {
const refSchema = (root.schema as AnySchemaObject).definitions?.[ref]
Expand Down Expand Up @@ -52,7 +52,7 @@ const def: CodeKeywordDefinition = {
dataTypes: [],
schemaPath: nil,
topSchemaRef: schName,
errSchemaPath: `#/definitions/${ref}`,
errSchemaPath: `/definitions/${ref}`,
},
valid
)
Expand Down
10 changes: 4 additions & 6 deletions spec/jtd-schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,10 @@ describe("JSON Type Definition", () => {
}

// function convertErrors(errors: TestCaseError[]): JTDError[] {
// return errors.map((e) =>
// ({
// instancePath: jsonPointer(e.instancePath),
// schemaPath: jsonPointer(e.schemaPath)
// })
// )
// return errors.map((e) => ({
// instancePath: jsonPointer(e.instancePath),
// schemaPath: jsonPointer(e.schemaPath),
// }))
// }

// function jsonPointer(error: string[]): string {
Expand Down

0 comments on commit 9e5f247

Please sign in to comment.