Skip to content

Commit f4d6bc7

Browse files
committed
Add support for validating formats
1 parent f5afd3c commit f4d6bc7

File tree

4 files changed

+148
-67
lines changed

4 files changed

+148
-67
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ import { jsonSchemaCoveragePlugin } from "@hyperjump/json-schema-coverage/vitest
6666
export default defineConfig({
6767
plugins: [jsonSchemaCoveragePlugin()],
6868
test: {
69-
globalSetup: ["./register-my-dialect.ts"], // Optional
69+
globalSetup: ["./register-dialect-or-formats-or-whatever.ts"], // Optional
7070
include: ["schema-tests/**/*.test.ts"], // Optional
7171
coverage: {
7272
include: ["schemas/**/*.json"] // Optional

src/__snapshots__/coverage-map-service.test.js.snap

Lines changed: 108 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,114 @@ exports[`CoverageMapService JSON > $ref doesn't branch 1`] = `
215215
}
216216
`;
217217

218+
exports[`CoverageMapService JSON > 2019-09 format doesn't branch 1`] = `
219+
{
220+
".coverage-map-service-fixtures/subject.schema.json": {
221+
"b": {},
222+
"branchMap": {},
223+
"f": {
224+
".coverage-map-service-fixtures/subject.schema.json#": 0,
225+
},
226+
"fnMap": {
227+
".coverage-map-service-fixtures/subject.schema.json#": {
228+
"decl": {
229+
"end": {
230+
"column": 0,
231+
"line": 4,
232+
},
233+
"start": {
234+
"column": 0,
235+
"line": 1,
236+
},
237+
},
238+
"line": 1,
239+
"loc": {
240+
"end": {
241+
"column": 0,
242+
"line": 4,
243+
},
244+
"start": {
245+
"column": 0,
246+
"line": 1,
247+
},
248+
},
249+
"name": ".coverage-map-service-fixtures/subject.schema.json#",
250+
},
251+
},
252+
"path": ".coverage-map-service-fixtures/subject.schema.json",
253+
"s": {
254+
".coverage-map-service-fixtures/subject.schema.json#/format": 0,
255+
},
256+
"statementMap": {
257+
".coverage-map-service-fixtures/subject.schema.json#/format": {
258+
"end": {
259+
"column": 17,
260+
"line": 3,
261+
},
262+
"start": {
263+
"column": 2,
264+
"line": 3,
265+
},
266+
},
267+
},
268+
},
269+
}
270+
`;
271+
272+
exports[`CoverageMapService JSON > 2020-12 format doesn't branch 1`] = `
273+
{
274+
".coverage-map-service-fixtures/subject.schema.json": {
275+
"b": {},
276+
"branchMap": {},
277+
"f": {
278+
".coverage-map-service-fixtures/subject.schema.json#": 0,
279+
},
280+
"fnMap": {
281+
".coverage-map-service-fixtures/subject.schema.json#": {
282+
"decl": {
283+
"end": {
284+
"column": 0,
285+
"line": 4,
286+
},
287+
"start": {
288+
"column": 0,
289+
"line": 1,
290+
},
291+
},
292+
"line": 1,
293+
"loc": {
294+
"end": {
295+
"column": 0,
296+
"line": 4,
297+
},
298+
"start": {
299+
"column": 0,
300+
"line": 1,
301+
},
302+
},
303+
"name": ".coverage-map-service-fixtures/subject.schema.json#",
304+
},
305+
},
306+
"path": ".coverage-map-service-fixtures/subject.schema.json",
307+
"s": {
308+
".coverage-map-service-fixtures/subject.schema.json#/format": 0,
309+
},
310+
"statementMap": {
311+
".coverage-map-service-fixtures/subject.schema.json#/format": {
312+
"end": {
313+
"column": 17,
314+
"line": 3,
315+
},
316+
"start": {
317+
"column": 2,
318+
"line": 3,
319+
},
320+
},
321+
},
322+
},
323+
}
324+
`;
325+
218326
exports[`CoverageMapService JSON > additionalItems doesn't branch 1`] = `
219327
{
220328
".coverage-map-service-fixtures/subject.schema.json": {
@@ -987,60 +1095,6 @@ exports[`CoverageMapService JSON > examples doesn't branch 1`] = `
9871095
}
9881096
`;
9891097

990-
exports[`CoverageMapService JSON > format doesn't branch 1`] = `
991-
{
992-
".coverage-map-service-fixtures/subject.schema.json": {
993-
"b": {},
994-
"branchMap": {},
995-
"f": {
996-
".coverage-map-service-fixtures/subject.schema.json#": 0,
997-
},
998-
"fnMap": {
999-
".coverage-map-service-fixtures/subject.schema.json#": {
1000-
"decl": {
1001-
"end": {
1002-
"column": 0,
1003-
"line": 4,
1004-
},
1005-
"start": {
1006-
"column": 0,
1007-
"line": 1,
1008-
},
1009-
},
1010-
"line": 1,
1011-
"loc": {
1012-
"end": {
1013-
"column": 0,
1014-
"line": 4,
1015-
},
1016-
"start": {
1017-
"column": 0,
1018-
"line": 1,
1019-
},
1020-
},
1021-
"name": ".coverage-map-service-fixtures/subject.schema.json#",
1022-
},
1023-
},
1024-
"path": ".coverage-map-service-fixtures/subject.schema.json",
1025-
"s": {
1026-
".coverage-map-service-fixtures/subject.schema.json#/format": 0,
1027-
},
1028-
"statementMap": {
1029-
".coverage-map-service-fixtures/subject.schema.json#/format": {
1030-
"end": {
1031-
"column": 17,
1032-
"line": 3,
1033-
},
1034-
"start": {
1035-
"column": 2,
1036-
"line": 3,
1037-
},
1038-
},
1039-
},
1040-
},
1041-
}
1042-
`;
1043-
10441098
exports[`CoverageMapService JSON > if doesn't branch 1`] = `
10451099
{
10461100
".coverage-map-service-fixtures/subject.schema.json": {

src/coverage-map-service.js

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { readFile } from "node:fs/promises";
22
import { extname } from "node:path";
33
import { pathToFileURL } from "node:url";
4+
import { getShouldValidateFormat } from "@hyperjump/json-schema";
45
import { compile, getKeyword, getSchema } from "@hyperjump/json-schema/experimental";
56
import { jrefTypeOf, Reference } from "@hyperjump/browser/jref";
67
import * as JsonPointer from "@hyperjump/json-pointer";
@@ -27,17 +28,34 @@ export class CoverageMapService {
2728
"https://json-schema.org/keyword/definitions"
2829
]);
2930

30-
#nonBranchingKeywords = new Set([
31-
"https://json-schema.org/keyword/title",
32-
"https://json-schema.org/keyword/description",
33-
"https://json-schema.org/keyword/default",
34-
"https://json-schema.org/keyword/deprecated",
35-
"https://json-schema.org/keyword/readOnly",
36-
"https://json-schema.org/keyword/writeOnly",
37-
"https://json-schema.org/keyword/examples",
38-
"https://json-schema.org/keyword/format",
39-
"https://json-schema.org/keyword/unknown"
40-
]);
31+
/** @type Set<string> */
32+
#nonBranchingKeywords;
33+
34+
constructor() {
35+
this.#nonBranchingKeywords = new Set([
36+
"https://json-schema.org/keyword/title",
37+
"https://json-schema.org/keyword/description",
38+
"https://json-schema.org/keyword/default",
39+
"https://json-schema.org/keyword/deprecated",
40+
"https://json-schema.org/keyword/readOnly",
41+
"https://json-schema.org/keyword/writeOnly",
42+
"https://json-schema.org/keyword/examples",
43+
"https://json-schema.org/keyword/unknown"
44+
]);
45+
46+
switch (getShouldValidateFormat()) {
47+
case false:
48+
this.#nonBranchingKeywords.add("https://json-schema.org/keyword/draft-04/format");
49+
this.#nonBranchingKeywords.add("https://json-schema.org/keyword/draft-06/format");
50+
this.#nonBranchingKeywords.add("https://json-schema.org/keyword/draft-07/format");
51+
// Missing "break" is intentional
52+
case undefined:
53+
this.#nonBranchingKeywords.add("https://json-schema.org/keyword/draft-2019-09/format");
54+
this.#nonBranchingKeywords.add("https://json-schema.org/keyword/draft-2020-12/format");
55+
this.#nonBranchingKeywords.add("https://json-schema.org/keyword/draft/format");
56+
break;
57+
}
58+
}
4159

4260
/** @type (schemaPath: string) => Promise<string> */
4361
async addFromFile(schemaPath) {

src/coverage-map-service.test.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ describe("CoverageMapService JSON", () => {
146146
expect(coverageMap).toMatchSnapshot();
147147
});
148148

149-
test("format doesn't branch", async () => {
149+
test("2020-12 format doesn't branch", async () => {
150150
const coverageMap = await coverageMapFor("subject.schema.json", `{
151151
"$schema": "https://json-schema.org/draft/2020-12/schema",
152152
"format": "date"
@@ -155,6 +155,15 @@ describe("CoverageMapService JSON", () => {
155155
expect(coverageMap).toMatchSnapshot();
156156
});
157157

158+
test("2019-09 format doesn't branch", async () => {
159+
const coverageMap = await coverageMapFor("subject.schema.json", `{
160+
"$schema": "https://json-schema.org/draft/2019-09/schema",
161+
"format": "date"
162+
}`);
163+
164+
expect(coverageMap).toMatchSnapshot();
165+
});
166+
158167
test("$ref doesn't branch", async () => {
159168
const coverageMap = await coverageMapFor("subject.schema.json", `{
160169
"$schema": "https://json-schema.org/draft/2020-12/schema",

0 commit comments

Comments
 (0)