From 8471ef2bdfd8318891ca439a5455e0436b32c616 Mon Sep 17 00:00:00 2001 From: Martin Reinhardt Date: Mon, 14 Sep 2026 10:18:27 +0200 Subject: [PATCH] feat: Adding https support resolves #47 --- src/commands/ajv.ts | 15 +++++++++++++++ test/https_draft07/invalid_data.json | 1 + test/https_draft07/schema.json | 8 ++++++++ test/https_draft07/valid_data.json | 1 + test/validate.spec.ts | 26 ++++++++++++++++++++++++++ 5 files changed, 51 insertions(+) create mode 100644 test/https_draft07/invalid_data.json create mode 100644 test/https_draft07/schema.json create mode 100644 test/https_draft07/valid_data.json diff --git a/src/commands/ajv.ts b/src/commands/ajv.ts index 8d4d8e7..f2a014b 100644 --- a/src/commands/ajv.ts +++ b/src/commands/ajv.ts @@ -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" @@ -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, 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, diff --git a/test/https_draft07/invalid_data.json b/test/https_draft07/invalid_data.json new file mode 100644 index 0000000..d8ff363 --- /dev/null +++ b/test/https_draft07/invalid_data.json @@ -0,0 +1 @@ +{"name": 123} diff --git a/test/https_draft07/schema.json b/test/https_draft07/schema.json new file mode 100644 index 0000000..23d7555 --- /dev/null +++ b/test/https_draft07/schema.json @@ -0,0 +1,8 @@ +{ + "$schema": "https://json-schema.org/draft-07/schema", + "type": "object", + "properties": { + "name": {"type": "string"} + }, + "required": ["name"] +} diff --git a/test/https_draft07/valid_data.json b/test/https_draft07/valid_data.json new file mode 100644 index 0000000..7c4c2f5 --- /dev/null +++ b/test/https_draft07/valid_data.json @@ -0,0 +1 @@ +{"name": "foo"} diff --git a/test/validate.spec.ts b/test/validate.spec.ts index e2e1535..fb5b79e 100644 --- a/test/validate.spec.ts +++ b/test/validate.spec.ts @@ -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(