|
| 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 | +}); |
0 commit comments