ajv-cli
Version:
Command line interface for Ajv JSON schema validator
54 lines • 1.81 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = require("./util");
const ajv_1 = require("./ajv");
const cmd = {
execute,
schema: {
type: "object",
required: ["s", "d"],
oneOf: [{ required: ["valid"] }, { required: ["invalid"] }],
properties: {
s: {
type: "string",
format: "notGlob",
},
d: { $ref: "#/$defs/stringOrArray" },
r: { $ref: "#/$defs/stringOrArray" },
m: { $ref: "#/$defs/stringOrArray" },
c: { $ref: "#/$defs/stringOrArray" },
valid: { type: "boolean" },
invalid: { type: "boolean", enum: [true] },
errors: { enum: ["json", "line", "text", "js", "no"] },
spec: { enum: ["draft7", "draft2019", "draft2020", "jtd"] },
},
ajvOptions: true,
},
};
exports.default = cmd;
function execute(argv) {
const ajv = ajv_1.default(argv);
const validate = util_1.compile(ajv, argv.s);
const shouldBeValid = !!argv.valid && argv.valid !== "false";
return util_1.getFiles(argv.d)
.map(testDataFile)
.every((x) => x);
function testDataFile(file) {
const data = util_1.openFile(file, `data file ${file}`);
const validData = validate(data);
let errors;
if (!validData)
errors = util_1.logJSON(argv.errors, validate.errors, ajv);
if (validData === shouldBeValid) {
console.log(`${file} passed test`);
if (errors)
console.log(errors);
return true;
}
console.error(`${file} failed test`);
if (errors)
console.error(errors);
return false;
}
}
//# sourceMappingURL=test.js.map
;