Skip to content

Commit 40ab822

Browse files
committed
Change registerSchema to take a file path
1 parent 1daff10 commit 40ab822

File tree

3 files changed

+54
-9
lines changed

3 files changed

+54
-9
lines changed

README.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ All files | 81.81 | 66.66 | 80 | 88.88 |
2828

2929
The following are known limitations I'm hopeful can be addressed.
3030

31+
- Coverage can't be reported for embedded schemas.
3132
- Coverage can only be reported for `**/*.schema.json` and `**/schema.json`
3233
files.
33-
- Coverage can't be reported for embedded schemas.
3434
- Schemas in YAML aren't supported.
35+
- TODO: Make sure types work
3536

3637
## Vitest
3738

@@ -76,8 +77,35 @@ describe("Worksheet", () => {
7677
});
7778
```
7879

80+
Instead of referring to the file path, you can register the schema and use its
81+
`$id`. Another reason to register a schema is if your schema references another
82+
schema.
83+
84+
```JavaScript
85+
import { describe, expect, test } from "vitest";
86+
import { registerSchema, unregisterSchema } from "@hyperjump/json-schema-coverage/vitest-matchers";
87+
88+
describe("Worksheet", () => {
89+
beforeEach(() => {
90+
registerSchema("./schema.json");
91+
});
92+
93+
afterEach(() => {
94+
unregisterSchema("./schema.json");
95+
});
96+
97+
test("matches with uri", async () => {
98+
await expect({ foo: 42 }).toMatchJsonSchema("https://example.com/main");
99+
});
100+
101+
test("doesn't match with uri", async () => {
102+
await expect({ foo: null }).not.toMatchJsonSchema("https://example.com/main");
103+
});
104+
});
105+
```
106+
79107
You can also use the matcher with inline schemas, but you only get coverage for
80-
file-based schemas.
108+
schemas from files in your code base.
81109

82110
```JavaScript
83111
import { describe, expect, test } from "vitest";

src/vitest/json-schema-matcher.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import type { SchemaObject } from "@hyperjump/json-schema";
2+
import type { AsyncExpectationResult } from "@vitest/expect";
23

34
// eslint-disable-next-line @typescript-eslint/no-explicit-any
45
export const matchJsonSchema: (instance: any, uriOrSchema: string | SchemaObject | boolean) => AsyncExpectationResult;
56
export const toMatchJsonSchema: typeof matchJsonSchema;
67

7-
export type { registerSchema, unregisterSchema } from "@hyperjump/json-schema/draft-2020-12";
8+
export const registerSchema: (filePath: string) => Promise<void>;
9+
10+
export type { unregisterSchema } from "@hyperjump/json-schema/draft-2020-12";
811
export type { loadDialect, defineVocabulary, addKeyword } from "@hyperjump/json-schema/experimental";

src/vitest/json-schema-matcher.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { randomUUID } from "node:crypto";
22
import { existsSync } from "node:fs";
3-
import { writeFile } from "node:fs/promises";
4-
import { registerSchema, unregisterSchema, validate } from "@hyperjump/json-schema/draft-2020-12";
3+
import { readFile, writeFile } from "node:fs/promises";
4+
import {
5+
registerSchema as register,
6+
unregisterSchema,
7+
validate
8+
} from "@hyperjump/json-schema/draft-2020-12";
59
import "@hyperjump/json-schema/draft-2019-09";
610
import "@hyperjump/json-schema/draft-07";
711
import "@hyperjump/json-schema/draft-06";
@@ -13,10 +17,10 @@ import { TestCoverageEvaluationPlugin } from "../test-coverage-evaluation-plugin
1317

1418
/**
1519
* @import { OutputUnit, SchemaObject } from "@hyperjump/json-schema"
16-
* @import { AsyncExpectationResult } from "@vitest/expect"
20+
* @import * as API from "./json-schema-matcher.d.ts"
1721
*/
1822

19-
/** @type (instance: any, uriOrSchema: string | SchemaObject | boolean) => AsyncExpectationResult */
23+
/** @type API.matchJsonSchema */
2024
export const matchJsonSchema = async (instance, uriOrSchema) => {
2125
/** @type OutputUnit */
2226
let output;
@@ -43,7 +47,7 @@ export const matchJsonSchema = async (instance, uriOrSchema) => {
4347
} else {
4448
const schema = uriOrSchema;
4549
const uri = `urn:uuid:${randomUUID()}`;
46-
registerSchema(schema, uri, "https://json-schema.org/draft/2020-12/schema");
50+
register(schema, uri, "https://json-schema.org/draft/2020-12/schema");
4751
try {
4852
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
4953
output = await validate(uri, instance, BASIC);
@@ -59,5 +63,15 @@ export const matchJsonSchema = async (instance, uriOrSchema) => {
5963
};
6064

6165
export const toMatchJsonSchema = matchJsonSchema;
62-
export { registerSchema, unregisterSchema } from "@hyperjump/json-schema/draft-2020-12";
66+
67+
/** @type API.registerSchema */
68+
export const registerSchema = async (filePath) => {
69+
const json = await readFile(filePath, "utf-8");
70+
/** @type SchemaObject */
71+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
72+
const schema = JSON.parse(json);
73+
register(schema);
74+
};
75+
76+
export { unregisterSchema } from "@hyperjump/json-schema/draft-2020-12";
6377
export { loadDialect, defineVocabulary, addKeyword } from "@hyperjump/json-schema/experimental";

0 commit comments

Comments
 (0)