Skip to content

Commit

Permalink
fix: avoid loading schemas in -r that exist in -s
Browse files Browse the repository at this point in the history
  • Loading branch information
massfords committed Dec 22, 2021
1 parent 58d6f07 commit 92cd7b3
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 7 deletions.
16 changes: 11 additions & 5 deletions src/commands/ajv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,25 @@ export default function (argv: ParsedArgs): AjvCore {
const ajv = new Ajv(opts)
let invalid: boolean | undefined
if (argv.spec !== "jtd") ajv.addMetaSchema(draft6metaSchema)
const sources = util.getFiles(argv.s)
addSchemas(argv.m, "addMetaSchema", "meta-schema")
addSchemas(argv.r, "addSchema", "schema")
addSchemas(argv.r, "addSchema", "schema", (filename) => !sources.includes(filename))
customFormatsKeywords(argv.c)
if (invalid) process.exit(1)
return ajv

function addSchemas(
args: string | string[] | undefined,
method: AjvMethod,
fileType: string
fileType: string,
test?: (filename: string) => boolean
): void {
if (!args) return
const files = util.getFiles(args)
files.forEach((file) => {
if (test && !test(file)) {
return
}
const schema = util.openFile(file, fileType)
try {
ajv[method](schema)
Expand Down Expand Up @@ -84,11 +89,12 @@ 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") {
const {code, message} = err
if (code === "MODULE_NOT_FOUND") {
throw new Error(
`'ts-node' is required for the TypeScript configuration files. Make sure it is installed\nError: ${err.message}`
`'ts-node' is required for the TypeScript configuration files. Make sure it is installed\nError: ${message}`
)
}

Expand Down
4 changes: 2 additions & 2 deletions src/commands/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ 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)
Expand Down Expand Up @@ -80,7 +80,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
16 changes: 16 additions & 0 deletions test/models/dependency.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$id": "dependency.json",

"properties": {
"name": {
"type": "string"
},

"root": {
"$ref": "./root.json"
}
},

"type": "object",
"additionalProperties": false
}
16 changes: 16 additions & 0 deletions test/models/root.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$id": "root.json",

"properties": {
"dependency": {
"$ref": "./dependency.json"
},

"root": {
"$ref": "./root.json"
}
},

"type": "object",
"additionalProperties": false
}
5 changes: 5 additions & 0 deletions test/models/valid_data.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependency": {
"name": "Hello World"
}
}
13 changes: 13 additions & 0 deletions test/validate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,19 @@ describe("validate", function () {
})
})

// See https://github.com/ajv-validator/ajv-cli/issues/172.
it("should resolve references that are also provided via the primary schema", (done) => {
cli(
'-s test/models/root.json -r "test/models/*.json" -d test/models/valid_data.jsonc',
(error, stdout, stderr) => {
assert.strictEqual(error, null)
assertValid(stdout, 1)
assert.strictEqual(stderr, "")
done()
}
)
})

it("should resolve reference and validate invalid data", (done) => {
cli(
"-s test/schema_with_ref -r test/schema -d test/invalid_data --errors=line",
Expand Down

0 comments on commit 92cd7b3

Please sign in to comment.