Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,4 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
node_modules/
172 changes: 172 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
{
"name": "json-schema-test-suite",
"version": "0.1.0",
"type": "module",
"description": "A language agnostic test suite for the JSON Schema specifications",
"repository": "github:json-schema-org/JSON-Schema-Test-Suite",
"keywords": [
"json-schema",
"tests"
],
"author": "http://json-schema.org",
"license": "MIT"
"license": "MIT",
"dependencies": {
"@hyperjump/browser": "^1.3.1",
"@hyperjump/json-pointer": "^1.1.1",
"@hyperjump/json-schema": "^1.17.2",
"@hyperjump/pact": "^1.4.0",
"@hyperjump/uri": "^1.3.2",
"json-stringify-deterministic": "^1.0.12"
},
"devDependencies": {
"jsonc-parser": "^3.3.1"
}
}
91 changes: 91 additions & 0 deletions scripts/add-test-ids.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import * as fs from "node:fs";

import { parse, modify, applyEdits } from "jsonc-parser";
import { normalize } from "./normalize.js";
import { loadRemotes } from "./load-remotes.js";
import generateTestId from "./utils/generateTestIds.js";


const DIALECT_MAP = {
"draft2020-12": "https://json-schema.org/draft/2020-12/schema",
"draft2019-09": "https://json-schema.org/draft/2019-09/schema",
"draft7": "http://json-schema.org/draft-07/schema#",
"draft6": "http://json-schema.org/draft-06/schema#",
"draft4": "http://json-schema.org/draft-04/schema#"
};




async function addIdsToFile(filePath, dialectUri) {
console.log("Reading:", filePath);

const text = fs.readFileSync(filePath, "utf8");
const tests = parse(text);
let edits = [];
let added = 0;

for (let i = 0; i < tests.length; i++) {
const testCase = tests[i];
const normalizedSchema = await normalize(testCase.schema, dialectUri);

for (let j = 0; j < testCase.tests.length; j++) {
const test = testCase.tests[j];

if (!test.id) {
const id = generateTestId(
normalizedSchema,
test.data,
test.valid
);

const path = [i, "tests", j, "id"];

edits.push(
...modify(text, path, id, {
formattingOptions: {
insertSpaces: true,
tabSize: 4
}
})
);

added++;
}
}
}

if (added > 0) {
const updatedText = applyEdits(text, edits);
fs.writeFileSync(filePath, updatedText);
console.log(` Added ${added} IDs`);
} else {
console.log(" All tests already have IDs");
}
}

//CLI stuff

const dialectArg = process.argv[2];
if (!dialectArg || !DIALECT_MAP[dialectArg]) {
console.error("Usage: node add-test-ids.js <dialect> [file-path]");
console.error("Available dialects:", Object.keys(DIALECT_MAP).join(", "));
process.exit(1);
}

const dialectUri = DIALECT_MAP[dialectArg];
const filePath = process.argv[3];

// Load remotes only for the specified dialect
loadRemotes(dialectUri, "./remotes");

if (filePath) {
await addIdsToFile(filePath, dialectUri);
} else {
const testDir = `tests/${dialectArg}`;
const files = fs.readdirSync(testDir).filter(f => f.endsWith(".json"));

for (const file of files) {
await addIdsToFile(`${testDir}/${file}`, dialectUri);
}
}
Loading
Loading