Skip to content

Commit 7ce21e8

Browse files
author
Sascha Goldhofer
committed
update: get latest json-schema-tests for draft tests
1 parent 8a999b3 commit 7ce21e8

File tree

6 files changed

+307
-215
lines changed

6 files changed

+307
-215
lines changed

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
},
3535
"homepage": "https://github.com/sagold/json-schema-library",
3636
"devDependencies": {
37-
"@json-schema-org/tests": "^2.0.0",
3837
"@types/chai": "^4.2.14",
3938
"@types/mocha": "^9.1.1",
4039
"@types/node": "^14.14.10",
@@ -48,13 +47,13 @@
4847
"eslint-plugin-optimize-regex": "^1.2.0",
4948
"eslint-plugin-promise": "^6.0.0",
5049
"glob": "^8.0.1",
51-
"json-schema-test-suite": "^0.0.10",
50+
"json-schema-test-suite": "https://github.com/json-schema-org/JSON-Schema-Test-Suite.git",
5251
"mocha": "^9.2.2",
5352
"nyc": "^15.1.0",
5453
"terser-webpack-plugin": "^5.0.3",
5554
"ts-loader": "^9.2.8",
5655
"ts-node": "^10.7.0",
57-
"typescript": "^4.6.3",
56+
"typescript": "^4.6.4",
5857
"watch": "^1.0.1",
5958
"webpack": "^5.9.0",
6059
"webpack-cli": "^4.2.0"
@@ -67,7 +66,7 @@
6766
"valid-url": "^1.0.9"
6867
},
6968
"resolutions": {
70-
"json-schema-test-suite": "json-schema-org/JSON-Schema-Test-Suite.git#bcf1dc81ae099ade2a9642c672c06ee1af1bb489",
69+
"json-schema-test-suite": "https://github.com/json-schema-org/JSON-Schema-Test-Suite.git#060caae0dd58e34af0449baec1103606a0ef4977",
7170
"lodash": "4.x",
7271
"merge": "2.x"
7372
},

test/getDraftTests.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import glob from "glob";
2+
import path from "path";
3+
import fs from "fs";
4+
5+
type JsonSchemaTestFileContents = {
6+
description: string;
7+
schema: Record<string, any>;
8+
tests: {
9+
description: string;
10+
data: any;
11+
valid: boolean;
12+
}[]
13+
}[];
14+
15+
function readTestFile(filepath: string): TestCase[] {
16+
const contents = fs.readFileSync(filepath, "utf-8").toString();
17+
const testCaseData : JsonSchemaTestFileContents = JSON.parse(contents);
18+
return testCaseData;
19+
}
20+
21+
function getFilenameAttributes(filename: string) {
22+
let relative = filename.split(/draft[^/]+\//).pop();
23+
relative = relative.replace(".json", "").replace(/^\//, "");
24+
const attributes = relative.replace(".json", "").split("/");
25+
const optional = attributes[0] === "optional" ? (attributes.shift() && true) : false;
26+
return { optional, name: attributes.join("-") };
27+
}
28+
29+
type Draft = "4"|"6"|"7"|"2019-09"|"2020-12";
30+
31+
export type FeatureTest = {
32+
/** name of feature being tests, e.g. additionalItems */
33+
name: string;
34+
/** if the test is retrieved from optional tests */
35+
optional?: boolean;
36+
/** list of test cases of feature */
37+
testCases: TestCase[]
38+
}
39+
40+
export type TestCase = {
41+
description: string;
42+
schema: Record<string, any>;
43+
tests: Test[];
44+
}
45+
46+
export type Test = {
47+
description: string;
48+
data: any;
49+
valid: boolean;
50+
}
51+
52+
export function getDraftTests(draft: Draft): FeatureTest[] {
53+
const source = path.resolve(`./node_modules/json-schema-test-suite/tests/draft${draft}`);
54+
const filenames = glob.sync(`${source}/**/*.json`);
55+
const testCases: FeatureTest[] = filenames
56+
.map(filename => {
57+
const testCaseData = readTestFile(filename);
58+
const { optional, name } = getFilenameAttributes(filename);
59+
return {
60+
name,
61+
testCases: testCaseData,
62+
optional
63+
}
64+
});
65+
66+
return testCases;
67+
}

test/spec/v4/draft04.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import chalk from "chalk";
44
import Draft04 from "../../../lib/cores/Draft04";
55
import addSchema from "../../../lib/addSchema";
66
import { addRemotes } from "../utils/addRemotes";
7-
import TestSuite from "@json-schema-org/tests";
7+
import { getDraftTests, FeatureTest } from "../../getDraftTests"
88
import draft04 from "../../../remotes/draft04.json";
99

1010
addRemotes(addSchema);
@@ -16,14 +16,14 @@ RangeError: Maximum call stack size exceeded
1616
*/
1717

1818
const supportedTestCases = t => t.optional ? !["non-bmp-regex", "zeroTerminatedFloats", "float-overflow"].includes(t.name) : true;
19-
const testCases = TestSuite.draft4()
19+
const draftFeatureTests = getDraftTests("4")
2020
// .filter(testcase => testcase.name === "float-overflow")
2121
.filter(supportedTestCases);
2222

2323

24-
function runTestCase(Core, tc, skipTest = []) {
24+
function runTestCase(Core, tc: FeatureTest, skipTest = []) {
2525
describe(`${tc.name}${tc.optional ? " (optional)" : ""}`, () => {
26-
tc.schemas.forEach(testCase => {
26+
tc.testCases.forEach(testCase => {
2727
const schema = testCase.schema;
2828
if (skipTest.includes(testCase.description)) {
2929
console.log(chalk.red(`Unsupported '${testCase.description}'`));
@@ -47,7 +47,7 @@ function runTestCase(Core, tc, skipTest = []) {
4747

4848
export default function runAllTestCases(Core, skipTest = []) {
4949
describe("draft04", () => {
50-
testCases.forEach(testCase => runTestCase(Core, testCase, skipTest));
50+
draftFeatureTests.forEach(testCase => runTestCase(Core, testCase, skipTest));
5151
});
5252
}
5353

test/spec/v6/draft06.test.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,37 @@
11
/* eslint max-len: 0 */
2-
import { expect } from "chai";
2+
import addSchema from "../../../lib/draft06/addSchema";
33
import chalk from "chalk";
44
import Draft06 from "../../../lib/cores/Draft06";
5-
import addSchema from "../../../lib/draft06/addSchema";
6-
import { addRemotes } from "../utils/addRemotes";
7-
import TestSuite from "@json-schema-org/tests";
85
import draft06 from "../../../remotes/draft06.json";
6+
import { addRemotes } from "../utils/addRemotes";
7+
import { expect } from "chai";
8+
import { getDraftTests, FeatureTest, TestCase, Test } from "../../getDraftTests"
99

1010
addRemotes(addSchema);
1111
addSchema("http://json-schema.org/draft-06/schema", draft06);
1212

1313
const supportedTestCases = t => t.optional ? !["ecmascript-regex", "float-overflow", "non-bmp-regex"].includes(t.name) : true;
14-
15-
const testCases = TestSuite.draft6()
14+
const draftFeatureTests = getDraftTests("6")
1615
// .filter(testcase => testcase.name === "definitions")
1716
.filter(supportedTestCases);
1817

18+
function runTestCase(Core, tc: FeatureTest, skipTest = []) {
1919

20-
function runTestCase(Core, tc, skipTest = []) {
2120
describe(`${tc.name}${tc.optional ? " (optional)" : ""}`, () => {
22-
tc.schemas.forEach(testCase => {
21+
22+
tc.testCases.forEach((testCase: TestCase) => {
23+
2324
const schema = testCase.schema;
2425
if (skipTest.includes(testCase.description)) {
2526
console.log(chalk.red(`Unsupported '${testCase.description}'`));
2627
return;
2728
}
2829

2930
describe(testCase.description, () => {
30-
testCase.tests.forEach(testData => {
31-
const test = skipTest.includes(testData.description) ? it.skip : it;
3231

32+
testCase.tests.forEach((testData: Test) => {
33+
34+
const test = skipTest.includes(testData.description) ? it.skip : it;
3335
test(testData.description, () => {
3436
const validator = new Core(schema);
3537
const isValid = validator.isValid(testData.data);
@@ -43,7 +45,7 @@ function runTestCase(Core, tc, skipTest = []) {
4345

4446
export default function runAllTestCases(Core, skipTest = []) {
4547
describe("draft06", () => {
46-
testCases.forEach(testCase => runTestCase(Core, testCase, skipTest));
48+
draftFeatureTests.forEach(testCase => runTestCase(Core, testCase, skipTest));
4749
});
4850
}
4951

test/spec/v7/draft07.test.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,20 @@ import chalk from "chalk";
44
import Draft07 from "../../../lib/cores/Draft07";
55
import addSchema from "../../../lib/draft06/addSchema";
66
import { addRemotes } from "../utils/addRemotes";
7-
import TestSuite from "@json-schema-org/tests";
7+
import { getDraftTests, FeatureTest } from "../../getDraftTests"
88
import draft07 from "../../../remotes/draft07.json";
99

1010
addRemotes(addSchema);
1111
addSchema("http://json-schema.org/draft-07/schema", draft07);
1212

1313
const supportedTestCases = t => t.optional ? !["ecmascript-regex", "content", "iri", "iri-reference", "idn", "idn-reference", "idn-hostname", "idn-email", "float-overflow", "non-bmp-regex"].includes(t.name) : true;
14-
15-
const testCases = TestSuite.draft7()
16-
// .filter(testcase => testcase.name === "refRemote")
17-
// .filter(testcase => testcase.optional)
14+
const draftFeatureTests = getDraftTests("7")
15+
// .filter(testcase => testcase.name === "definitions")
1816
.filter(supportedTestCases);
1917

20-
21-
function runTestCase(Core, tc, skipTest = []) {
18+
function runTestCase(Core, tc: FeatureTest, skipTest = []) {
2219
describe(`${tc.name}${tc.optional ? " (optional)" : ""}`, () => {
23-
tc.schemas.forEach(testCase => {
20+
tc.testCases.forEach(testCase => {
2421
const schema = testCase.schema;
2522
if (skipTest.includes(testCase.description)) {
2623
console.log(chalk.red(`Unsupported '${testCase.description}'`));
@@ -44,7 +41,7 @@ function runTestCase(Core, tc, skipTest = []) {
4441

4542
export default function runAllTestCases(Core, skipTest = []) {
4643
describe("draft07", () => {
47-
testCases.forEach(testCase => runTestCase(Core, testCase, skipTest));
44+
draftFeatureTests.forEach(testCase => runTestCase(Core, testCase, skipTest));
4845
});
4946
}
5047

0 commit comments

Comments
 (0)