-
-
Notifications
You must be signed in to change notification settings - Fork 230
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?
Conversation
|
This should have been opened as draft PR. I've converted it this time. Next time you open a PR that's a work in progress for the purpose of getting feedback, a draft PR is the way to go. |
jdesrosiers
left a comment
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've reviewed the first file. I'll have to get to the rest another time, but that should help clear up some misunderstandings and hopefully, you can apply some of that to the rest as well.
One more thing I noticed is that you're using the wrong indentation for JSON files. In this repo, the convention is 4 spaces. Make sure you're matching that convention or it makes your changes very difficult to review. (2 spaces for JavaScript is fine. Just JSON needs to be 4 spaces.)
| if (!Array.isArray(tests)) { | ||
| console.log("Expected an array at top level, got:", typeof tests); | ||
| return; | ||
| } |
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.
This shouldn't be necessary. There's already automation to check the test file against the schema. By the time we run this script, we should be confident that the test file is in the right format.
| } | ||
|
|
||
| for (const testCase of tests) { | ||
| if (!Array.isArray(testCase.tests)) continue; |
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.
More unnecessary defensive programming.
| for (const testCase of tests) { | ||
| if (!Array.isArray(testCase.tests)) continue; | ||
|
|
||
| const dialectUri = getDialectUri(testCase.schema || {}); |
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.
Unnecessary defensive programming.
| const dialectUri = getDialectUri(testCase.schema || {}); | |
| const dialectUri = getDialectUri(testCase.schema); |
| if (!Array.isArray(testCase.tests)) continue; | ||
|
|
||
| const dialectUri = getDialectUri(testCase.schema || {}); | ||
| const normalizedSchema = await normalize(testCase.schema || true, dialectUri); |
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.
Unnecessary defensive programming.
| const normalizedSchema = await normalize(testCase.schema || true, dialectUri); | |
| const normalizedSchema = await normalize(testCase.schema, dialectUri); |
| 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 getDialectUri(schema) { | ||
| if (schema.$schema && DIALECT_MAP[schema.$schema]) { | ||
| return DIALECT_MAP[schema.$schema]; | ||
| } | ||
| return "https://json-schema.org/draft/2020-12/schema"; | ||
| } |
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.
There are a couple of issues here.
First, you can't use 2020-12 as the default dialect. The default needs to be the dialect associated with the directory the test file is in. For example, the convention to use $schema in every test case schema was only adopted starting in 2019-09. So, tests for draft-04/6/7 don't have $schema. That means this script will treat all of those tests as 2020-12 schemas, which would be very wrong.
This whole function should be unnecessary anyway. This is already handled internally by @hyperjump/json-schema. The result of this function is only used to pass to the registerSchema function, which should be the dialect from the directory the test file is in, not from the schema.
| .digest("hex"); | ||
| } | ||
|
|
||
| async function addIdsToFile(filePath) { |
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 should need to pass the dialect based on the directory to this function.
| } | ||
| } | ||
|
|
||
| if (changed) { |
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.
Do you really need a changed flag? Can't you just check if added is greater than 0?
| // Load remotes for all dialects | ||
| const remotesPaths = ["./remotes"]; | ||
| for (const dialectUri of Object.values(DIALECT_MAP)) { | ||
| for (const path of remotesPaths) { | ||
| if (fs.existsSync(path)) { | ||
| loadRemotes(dialectUri, path); | ||
| } | ||
| } | ||
| } |
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're not going to be able to add remotes for all dialects at the same time. You're going to have to run this script separately for each dialect. Most remotes don't have $schema and are expected to run as the dialect of test case that's being tested. So, you can't load them at the same time because you can't have two schemas with the same id and different dialects. Your edits to loadRemotes are a result of this misunderstanding. I don't think you should need to make changes to that function.
I suggest making the first argument of the script the local draft identifier, then convert that to the right dialect URI to pass to addIdsToFile. Then the second argument can be the optional filePath, where all files are processed if the none is given.
| } | ||
|
|
||
| const filePath = process.argv[2] || "tests/draft2020-12/enum.json"; | ||
| addIdsToFile(filePath).catch(console.error); No newline at end of file |
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.
The catch is unnecessary here. Just let it throw if there's an error.
Context
This is a small proof-of-concept for #698, implementing option 3 from our discussion:
The goal is:
normalize.js
load-remotes.js
add-test-ids.js
check-test-ids.js
enum.json
How to run
For now, only enum.json has IDs. The checker will still report "Missing ID"
for other files in draft2020-12
Questions for reviewers
If this direction looks good, I can:
Extend the tooling as needed based on feedback
Add a small GitHub Action that runs check-test-ids.js on the touched version
Then start updating more files, one small PR at a time.