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

Allow validate command to save validation results to file #233

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
13 changes: 7 additions & 6 deletions src/commands/ajv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,14 @@ export default function (argv: ParsedArgs): AjvCore {
try {
registerer = require("ts-node").register()
} catch (err) {
/* istanbul ignore next */
if (err.code === "MODULE_NOT_FOUND") {
throw new Error(
`'ts-node' is required for the TypeScript configuration files. Make sure it is installed\nError: ${err.message}`
)
if (err instanceof Error) {
/* istanbul ignore next */
if ((err as any).code === "MODULE_NOT_FOUND") {
throw new Error(
`'ts-node' is required for the TypeScript configuration files. Make sure it is installed\nError: ${err.message}`
)
}
}

throw err
}

Expand Down
1 change: 1 addition & 0 deletions src/commands/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ parameters
-r referenced schema(s)
-m meta schema(s)
-c custom keywords/formats definitions
-o output file to save any validation results

-d, -r, -m, -c can be globs and can be used multiple times
glob should be enclosed in double quotes
Expand Down
12 changes: 8 additions & 4 deletions src/commands/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ export function openFile(filename: string, suffix: string): any {
json = require(file)
}
} catch (err) {
const msg: string = err.message
console.error(`error: ${msg.replace(" module", " " + suffix)}`)
if (err instanceof Error) {
const msg: string = err.message
console.error(`error: ${msg.replace(" module", " " + suffix)}`)
}
process.exit(2)
}
return json
Expand Down Expand Up @@ -81,8 +83,10 @@ export function compile(ajv: Ajv, schemaFile: string): AnyValidateFunction {
try {
return ajv.compile(schema)
} catch (err) {
console.error(`schema ${schemaFile} is invalid`)
console.error(`error: ${err.message}`)
if (err instanceof Error) {
console.error(`schema ${schemaFile} is invalid`)
console.error(`error: ${err.message}`)
}
process.exit(1)
}
}
8 changes: 7 additions & 1 deletion src/commands/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {ParsedArgs} from "minimist"
import {compile, getFiles, openFile, logJSON} from "./util"
import getAjv from "./ajv"
import * as jsonPatch from "fast-json-patch"
import * as fs from "fs"

const cmd: Command = {
execute,
Expand All @@ -18,6 +19,7 @@ const cmd: Command = {
r: {$ref: "#/$defs/stringOrArray"},
m: {$ref: "#/$defs/stringOrArray"},
c: {$ref: "#/$defs/stringOrArray"},
o: {type: "string", format: "notGlob"},
errors: {enum: ["json", "line", "text", "js", "no"]},
changes: {enum: [true, "json", "line", "js"]},
spec: {enum: ["draft7", "draft2019", "draft2020", "jtd"]},
Expand Down Expand Up @@ -54,7 +56,11 @@ function execute(argv: ParsedArgs): boolean {
}
} else {
console.error(file, "invalid")
console.error(logJSON(argv.errors, validate.errors, ajv))
if (argv.o) {
fs.writeFileSync(argv.o, JSON.stringify(validate.errors))
} else {
console.error(logJSON(argv.errors, validate.errors, ajv))
}
}
return validData
}
Expand Down
20 changes: 20 additions & 0 deletions test/validate.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import cli from "./cli"
import assert = require("assert")
import fs = require("fs")
import type {DefinedError} from "ajv"

describe("validate", function () {
Expand Down Expand Up @@ -348,6 +349,25 @@ describe("validate", function () {
}
)
})

it("should validate invalid data to output file", (done) => {
cli(
"validate -s test/custom/schema -c ajv-keywords/dist/keywords/typeof -d test/custom/invalid_data -o test/validate_schema.json",
(error, stdout, stderr) => {
const validate = require("./validate_schema.json")
fs.unlinkSync("test/validate_schema.json")
assert(error instanceof Error)
assert.strictEqual(stdout, "")
assert.strictEqual(stderr, "test/custom/invalid_data invalid\n")
assert.strictEqual(1, validate.length)
const validateOutput = validate[0]
assert.strictEqual(validateOutput.schemaPath, "#/typeof")
assert.strictEqual(validateOutput.keyword, "typeof")
assert.strictEqual(validateOutput.message, `must pass "typeof" keyword validation`)
done()
}
)
})
})
})

Expand Down