Skip to content
Merged
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
15 changes: 15 additions & 0 deletions src/commands/ajv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {getOptions} from "./options"
import * as util from "./util"
import * as path from "path"
import * as draft6metaSchema from "ajv/lib/refs/json-schema-draft-06.json"
import * as draft7metaSchema from "ajv/lib/refs/json-schema-draft-07.json"

type AjvMethod = "addSchema" | "addMetaSchema"

Expand All @@ -35,12 +36,26 @@ export default function (argv: ParsedArgs): AjvCore {
const ajv = new Ajv(opts)
let invalid: boolean | undefined
if (argv.spec !== "jtd") ajv.addMetaSchema(draft6metaSchema)
// json-schema.org now serves draft-07 over https (e.g. schemas generated by
// recent tooling use "https://json-schema.org/draft-07/schema"), but Ajv7
// only registers the historical "http://…" $id. Alias the same meta-schema
// under the https URL so schemas using either form resolve.
if (Ajv === Ajv7) addMetaSchemaAlias(draft7metaSchema, "https://json-schema.org/draft-07/schema")
addSchemas(argv.m, "addMetaSchema", "meta-schema")
addSchemas(argv.r, "addSchema", "schema")
customFormatsKeywords(argv.c)
if (invalid) process.exit(1)
return ajv

function addMetaSchemaAlias(schema: Record<string, unknown>, key: string): void {
// Drop $id so Ajv registers the clone under `key` instead of the
// original $id; skip re-validation since it's the already-validated
// meta-schema under a different URI.
const alias = {...schema}
delete alias.$id
ajv.addMetaSchema(alias, key, false)
}

function addSchemas(
args: string | string[] | undefined,
method: AjvMethod,
Expand Down
1 change: 1 addition & 0 deletions test/https_draft07/invalid_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name": 123}
8 changes: 8 additions & 0 deletions test/https_draft07/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"$schema": "https://json-schema.org/draft-07/schema",
"type": "object",
"properties": {
"name": {"type": "string"}
},
"required": ["name"]
}
1 change: 1 addition & 0 deletions test/https_draft07/valid_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name": "foo"}
26 changes: 26 additions & 0 deletions test/validate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,32 @@ describe("validate", function () {
})
})

describe("validate schema with https draft-07 $schema", () => {
it("should validate valid data", (done) => {
cli(
"-s test/https_draft07/schema -d test/https_draft07/valid_data",
(error, stdout, stderr) => {
assert.strictEqual(error, null)
assertValid(stdout, 1)
assert.strictEqual(stderr, "")
done()
}
)
})

it("should validate invalid data", (done) => {
cli(
"-s test/https_draft07/schema -d test/https_draft07/invalid_data --errors=line",
(error, stdout, stderr) => {
assert(error instanceof Error)
assert.strictEqual(stdout, "")
assert.ok(/must be string/.exec(stderr))
done()
}
)
})
})

describe("validate with schema using added meta-schema", () => {
it("should validate valid data", (done) => {
cli(
Expand Down