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

Add better-ajv-errors support #189

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
},
"dependencies": {
"ajv": "^8.0.0",
"better-ajv-errors": "^0.7.0",
"fast-json-patch": "^2.0.0",
"glob": "^7.1.0",
"js-yaml": "^3.14.0",
Expand Down
2 changes: 1 addition & 1 deletion src/commands/ajv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export default function (argv: ParsedArgs): AjvCore {

try {
registerer = require("ts-node").register()
} catch (err) {
} catch (err: any) {
/* istanbul ignore next */
if (err.code === "MODULE_NOT_FOUND") {
throw new Error(
Expand Down
2 changes: 1 addition & 1 deletion src/commands/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function execute(argv: ParsedArgs): boolean {
const data = openFile(file, `data file ${file}`)
const validData = validate(data)
let errors
if (!validData) errors = logJSON(argv.errors, validate.errors, ajv)
if (!validData) errors = logJSON(argv.errors, validate.errors, ajv, data, argv.s)

if (validData === shouldBeValid) {
console.log(`${file} passed test`)
Expand Down
17 changes: 14 additions & 3 deletions src/commands/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as fs from "fs"
import * as yaml from "js-yaml"
import * as JSON5 from "json5"
import {AnyValidateFunction} from "ajv/dist/core"
import * as betterAjvErrors from "better-ajv-errors"

export function getFiles(args: string | string[]): string[] {
let files: string[] = []
Expand Down Expand Up @@ -51,15 +52,15 @@ export function openFile(filename: string, suffix: string): any {
} catch (e) {
json = require(file)
}
} catch (err) {
} catch (err: any) {
const msg: string = err.message
console.error(`error: ${msg.replace(" module", " " + suffix)}`)
process.exit(2)
}
return json
}

export function logJSON(mode: string, data: any, ajv?: Ajv): string {
export function logJSON(mode: string, data: any, ajv?: Ajv, original?: any, schema?: any): string {
switch (mode) {
case "json":
data = JSON.stringify(data, null, " ")
Expand All @@ -72,6 +73,16 @@ export function logJSON(mode: string, data: any, ajv?: Ajv): string {
break
case "text":
if (ajv) data = ajv.errorsText(data)
break
case "pretty":
if (original && schema) {
for (const error of data) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore Workaround https://github.com/atlassian/better-ajv-errors/issues/90
error.dataPath = error.instancePath
}
data = betterAjvErrors(JSON.stringify(schema), JSON.stringify(original), data)
}
}
return data
}
Expand All @@ -80,7 +91,7 @@ export function compile(ajv: Ajv, schemaFile: string): AnyValidateFunction {
const schema = openFile(schemaFile, "schema")
try {
return ajv.compile(schema)
} catch (err) {
} catch (err: any) {
console.error(`schema ${schemaFile} is invalid`)
console.error(`error: ${err.message}`)
process.exit(1)
Expand Down
6 changes: 3 additions & 3 deletions src/commands/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const cmd: Command = {
r: {$ref: "#/$defs/stringOrArray"},
m: {$ref: "#/$defs/stringOrArray"},
c: {$ref: "#/$defs/stringOrArray"},
errors: {enum: ["json", "line", "text", "js", "no"]},
errors: {enum: ["json", "line", "text", "js", "no", "pretty"]},
changes: {enum: [true, "json", "line", "js"]},
spec: {enum: ["draft7", "draft2019", "draft2020", "jtd"]},
},
Expand All @@ -38,7 +38,7 @@ function execute(argv: ParsedArgs): boolean {
function validateDataFile(file: string): boolean {
const data = openFile(file, `data file ${file}`)
let original
if (argv.changes) original = JSON.parse(JSON.stringify(data))
if (argv.changes || argv.errors === "pretty") original = JSON.parse(JSON.stringify(data))
const validData = validate(data) as boolean

if (validData) {
Expand All @@ -54,7 +54,7 @@ function execute(argv: ParsedArgs): boolean {
}
} else {
console.error(file, "invalid")
console.error(logJSON(argv.errors, validate.errors, ajv))
console.error(logJSON(argv.errors, validate.errors, ajv, original, argv.s))
}
return validData
}
Expand Down