-
-
Notifications
You must be signed in to change notification settings - Fork 245
Add normalized hash-based test IDs to draft2020-12/enum.json (POC for #698) #796
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
44fc7b2
d2980e4
8b990ad
7a2270b
6cc0f8f
7b86302
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,21 @@ | ||
| { | ||
| "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" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import * as fs from "node:fs"; | ||
| import * as crypto from "node:crypto"; | ||
| import jsonStringify from "json-stringify-deterministic"; | ||
| import { normalize } from "./normalize.js"; | ||
| import { loadRemotes } from "./load-remotes.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#" | ||
| }; | ||
|
|
||
| function generateTestId(normalizedSchema, testData, testValid) { | ||
| return crypto | ||
| .createHash("md5") | ||
| .update(jsonStringify(normalizedSchema) + jsonStringify(testData) + testValid) | ||
| .digest("hex"); | ||
| } | ||
|
|
||
| async function addIdsToFile(filePath, dialectUri) { | ||
| console.log("Reading:", filePath); | ||
| const tests = JSON.parse(fs.readFileSync(filePath, "utf8")); | ||
| let added = 0; | ||
|
|
||
| for (const testCase of tests) { | ||
| // Pass dialectUri from directory, not from schema | ||
| // @hyperjump/json-schema handles the schema's $schema internally | ||
| const normalizedSchema = await normalize(testCase.schema, dialectUri); | ||
|
|
||
| for (const test of testCase.tests) { | ||
| if (!test.id) { | ||
| test.id = generateTestId(normalizedSchema, test.data, test.valid); | ||
| added++; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (added > 0) { | ||
| fs.writeFileSync(filePath, JSON.stringify(tests, null, 4) + "\n"); | ||
| console.log(` Added ${added} IDs`); | ||
| } else { | ||
| console.log(" All tests already have IDs"); | ||
| } | ||
| } | ||
|
|
||
| // Get dialect from command line argument (e.g., "draft2020-12") | ||
| 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) { | ||
| // Process single file | ||
| addIdsToFile(filePath, dialectUri); | ||
| } else { | ||
| // Process all files in the dialect directory | ||
| const testDir = `tests/${dialectArg}`; | ||
| const files = fs.readdirSync(testDir).filter(f => f.endsWith('.json')); | ||
|
|
||
| for (const file of files) { | ||
| await addIdsToFile(`${testDir}/${file}`, dialectUri); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,143 @@ | ||||||
| import * as fs from "node:fs"; | ||||||
| import * as path from "node:path"; | ||||||
| import * as crypto from "node:crypto"; | ||||||
| import jsonStringify from "json-stringify-deterministic"; | ||||||
| import { normalize } from "./normalize.js"; | ||||||
| import { loadRemotes } from "./load-remotes.js"; | ||||||
|
|
||||||
| const DIALECT_MAP = { | ||||||
| "https://json-schema.org/draft/2020-12/schema": "https://json-schema.org/draft/2020-12/schema", | ||||||
| "https://json-schema.org/draft/2019-09/schema": "https://json-schema.org/draft/2019-09/schema", | ||||||
| "http://json-schema.org/draft-07/schema#": "http://json-schema.org/draft-07/schema#", | ||||||
| "http://json-schema.org/draft-06/schema#": "http://json-schema.org/draft-06/schema#", | ||||||
| "http://json-schema.org/draft-04/schema#": "http://json-schema.org/draft-04/schema#" | ||||||
| }; | ||||||
|
|
||||||
| function* jsonFiles(dir) { | ||||||
| for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { | ||||||
| const full = path.join(dir, entry.name); | ||||||
| if (entry.isDirectory()) { | ||||||
| yield* jsonFiles(full); | ||||||
| } else if (entry.isFile() && entry.name.endsWith(".json")) { | ||||||
| yield full; | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| function getDialectUri(schema) { | ||||||
| if (schema.$schema && DIALECT_MAP[schema.$schema]) { | ||||||
| return DIALECT_MAP[schema.$schema]; | ||||||
| } | ||||||
| return "https://json-schema.org/draft/2020-12/schema"; | ||||||
| } | ||||||
|
|
||||||
| function generateTestId(normalizedSchema, testData, testValid) { | ||||||
| return crypto | ||||||
| .createHash("md5") | ||||||
| .update(jsonStringify(normalizedSchema) + jsonStringify(testData) + testValid) | ||||||
| .digest("hex"); | ||||||
| } | ||||||
|
|
||||||
| async function checkVersion(dir) { | ||||||
| const missingIdFiles = new Set(); | ||||||
| const duplicateIdFiles = new Set(); | ||||||
| const mismatchedIdFiles = new Set(); | ||||||
| const idMap = new Map(); | ||||||
|
|
||||||
| console.log(`Checking tests in ${dir}...`); | ||||||
|
|
||||||
| for (const file of jsonFiles(dir)) { | ||||||
| const tests = JSON.parse(fs.readFileSync(file, "utf8")); | ||||||
|
|
||||||
| for (let i = 0; i < tests.length; i++) { | ||||||
| const testCase = tests[i]; | ||||||
| if (!Array.isArray(testCase.tests)) continue; | ||||||
|
|
||||||
| const dialectUri = getDialectUri(testCase.schema || {}); | ||||||
| const normalizedSchema = await normalize(testCase.schema || true, dialectUri); | ||||||
|
||||||
|
|
||||||
| for (let j = 0; j < testCase.tests.length; j++) { | ||||||
|
||||||
| for (let j = 0; j < testCase.tests.length; j++) { | |
| for (const test of testCase.tests) { |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm thinking that we probably don't need to check for duplicate IDs. Checking for duplicates would be necessary if people were assigning IDs, but these ids are generated based on the content of the test, so there should never be duplicates. If someone uses the ID from another test by copy-paste mistake, we should get a Mismatched ID error, so we don't need a duplicate check as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While making this, I briefly thought about whether two different tests could somehow end up with the same ID, but now I realize that it doesn’t really make sense for two distinct tests to share the same identifier.( still a little fearful that maybe or somehow it does end up being same , i know it will not happen but just cant really remove the doubt myself)
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need to catch if you're not doing to something meaningful to recover from the error. You can just let if throw.
| checkVersion(dir).catch(console.error); | |
| await checkVersion(dir); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, agreed!
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // scripts/load-remotes.js | ||
| import * as fs from "node:fs"; | ||
| import { toAbsoluteIri } from "@hyperjump/uri"; | ||
| import { registerSchema } from "@hyperjump/json-schema/draft-2020-12"; | ||
|
|
||
| // Keep track of which remote URLs we've already registered | ||
| const loadedRemotes = new Set(); | ||
|
|
||
| export const loadRemotes = (dialectId, filePath, url = "") => { | ||
| if (!fs.existsSync(filePath)) { | ||
| console.warn(`Warning: Remotes path not found: ${filePath}`); | ||
| return; | ||
| } | ||
|
|
||
| fs.readdirSync(filePath, { withFileTypes: true }).forEach((entry) => { | ||
| if (entry.isFile() && entry.name.endsWith(".json")) { | ||
| const remotePath = `${filePath}/${entry.name}`; | ||
| const remoteUrl = `http://localhost:1234${url}/${entry.name}`; | ||
|
|
||
| // If we've already registered this URL once, skip it | ||
| if (loadedRemotes.has(remoteUrl)) { | ||
| return; | ||
| } | ||
|
|
||
| const remote = JSON.parse(fs.readFileSync(remotePath, "utf8")); | ||
|
|
||
| // Only register if $schema matches dialect OR there's no $schema | ||
| if (!remote.$schema || toAbsoluteIri(remote.$schema) === dialectId) { | ||
| registerSchema(remote, remoteUrl, dialectId); | ||
| loadedRemotes.add(remoteUrl); // ✅ Remember we've registered it | ||
| } | ||
| } else if (entry.isDirectory()) { | ||
| loadRemotes(dialectId, `${filePath}/${entry.name}`, `${url}/${entry.name}`); | ||
| } | ||
| }); | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, that's useless 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
true:)