Skip to content

Commit a98db5e

Browse files
committed
Add and run JSON Schema test suite
1 parent 454f854 commit a98db5e

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "test-suite"]
2+
path = test-suite
3+
url = https://github.com/json-schema-org/JSON-Schema-Test-Suite.git

src/schema-test-suite.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { describe, it, expect } from "vitest";
2+
import { convertJsonSchemaToZod } from "./index";
3+
import * as fs from "fs";
4+
import * as path from "path";
5+
6+
interface SchemaTestCase {
7+
description: string;
8+
schema: any;
9+
tests: Array<{
10+
description: string;
11+
data: any;
12+
valid: boolean;
13+
}>;
14+
}
15+
16+
describe("JSON Schema Test Suite (Draft 2020-12)", () => {
17+
const testSuiteDir = path.join(__dirname, "../test-suite/tests/draft2020-12");
18+
19+
// Dynamically get all test files from the directory
20+
const testFiles = fs
21+
.readdirSync(testSuiteDir)
22+
.filter((file) => file.endsWith(".json"))
23+
.sort(); // Sort for consistent ordering
24+
25+
testFiles.forEach((testFile) => {
26+
describe(testFile.replace(".json", ""), () => {
27+
if (!fs.existsSync(path.join(testSuiteDir, testFile))) {
28+
it.skip(`${testFile} not found`, () => {});
29+
return;
30+
}
31+
32+
const testCases: SchemaTestCase[] = JSON.parse(fs.readFileSync(path.join(testSuiteDir, testFile), "utf8"));
33+
34+
testCases.forEach((testCase) => {
35+
describe(testCase.description, () => {
36+
const zodSchema = convertJsonSchemaToZod(testCase.schema);
37+
testCase.tests.forEach((test) => {
38+
it(test.description, () => {
39+
const result = zodSchema.safeParse(test.data);
40+
41+
if (test.valid) {
42+
expect(result.success).toBe(true);
43+
} else {
44+
expect(result.success).toBe(false);
45+
}
46+
});
47+
});
48+
});
49+
});
50+
});
51+
});
52+
});

test-suite

Submodule test-suite added at 9ad349b

0 commit comments

Comments
 (0)