Skip to content
Merged
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
85 changes: 76 additions & 9 deletions scripts/schema-test-coverage.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { readFileSync } from "node:fs";
import { readdir, readFile } from "node:fs/promises";
import YAML from "yaml";
import { join } from "node:path";
import { argv } from "node:process";
import { validate } from "@hyperjump/json-schema/draft-2020-12";
import { registerSchema, validate } from "@hyperjump/json-schema/draft-2020-12";
import "@hyperjump/json-schema/draft-04";
import { BASIC } from "@hyperjump/json-schema/experimental";
import { BASIC, addKeyword, defineVocabulary } from "@hyperjump/json-schema/experimental";

/**
* @import { EvaluationPlugin } from "@hyperjump/json-schema/experimental"
Expand Down Expand Up @@ -45,7 +46,14 @@ class TestCoveragePlugin {
this.allLocations = [];

for (const schemaLocation in context.ast) {
if (schemaLocation === "metaData") {
if (
schemaLocation === "metaData" ||
// Do not require coverage of standard JSON Schema
schemaLocation.includes("json-schema.org") ||
// Do not require coverage of default $dynamicAnchor
// schemas, as they are not expected to be reached
schemaLocation.endsWith("/schema/WORK-IN-PROGRESS#/$defs/schema")
) {
continue;
}

Expand Down Expand Up @@ -110,6 +118,68 @@ const runTests = async (schemaUri, testDirectory) => {
};
};

addKeyword({
id: "https://spec.openapis.org/oas/schema/vocab/keyword/discriminator",
interpret: (discriminator, instance, context) => {
return true;
},
/* discriminator is not exactly an annotation, but it's not allowed
* to change the validation outcome (hence returing true from interopret())
* and for our purposes of testing, this is sufficient.
*/
annotation: (discriminator) => {
return discriminator;
},
});

addKeyword({
id: "https://spec.openapis.org/oas/schema/vocab/keyword/example",
interpret: (example, instance, context) => {
return true;
},
annotation: (example) => {
return example;
},
});

addKeyword({
id: "https://spec.openapis.org/oas/schema/vocab/keyword/externalDocs",
interpret: (externalDocs, instance, context) => {
return true;
},
annotation: (externalDocs) => {
return externalDocs;
},
});

addKeyword({
id: "https://spec.openapis.org/oas/schema/vocab/keyword/xml",
interpret: (xml, instance, context) => {
return true;
},
annotation: (xml) => {
return xml;
},
});
Comment on lines +121 to +163
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These seem to be unnecessary, commenting them out does not change the test result.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ralfhandl they make the vocabulary be handled correctly if we want to further utilize those keywords in tests. I prefer to set up the vocabulary correctly rather than stub it out just because we're not using it right now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would checks of the keyword's correct use go into the interpret stubs?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ralfhandl they're not stubs. Annotations are always valid, returning true is correct.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least I think. That was as far as I got in the library's documentation. It's at least closer to correct than it was, and we can work on it more later.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These seem to be unnecessary, commenting them out does not change the test result.

It works because the vocabulary is set as optional. If there is no keyword implementation, it's treated like an unknown keyword, which is allowed, but not quite the same thing.

These keyword implementations are already provided, so you shouldn't need to define them here. Instead of importing @hyperjump/json-schema/draft-2020-12, use @hyperjump/json-schema/openapi-3-1. Then the keywords and vocabulary definitions will be loaded and you can skip this. However, you still need to register the local schemas because you're testing the local schemas, not the ones included with the @hyperjump/json-schema.


defineVocabulary(
"https://spec.openapis.org/oas/3.1/vocab/base",
Copy link
Contributor

@ralfhandl ralfhandl Jun 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This apparently needs to be parameterized with the OpenAPI version of the branch it is running on, because the script fails if run as-is on the v3.2-dev branch.

No spontanous idea on how to reliably do that, other than parsing dialect.yaml.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ralfhandl I was just going to modify the line on the 3.2 port

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which would end up in main (and then dev and v3.1-dev) after the release unless someone fiddles with the v3.2.0-rel branch.

Automatically detecting the release seems preferable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ralfhandl Which is fine but let's fix it separately. I do not have the mental bandwidth today for that, and want to get the XML PR merged so I can unblock further work that depends on it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This apparently needs to be parameterized with the OpenAPI version of the branch it is running on

I didn't realize you were versioning the vocabulary now. So, ignore what I said about not needing defineVocabulary, but you still shouldn't need to implement the keywords as long as you use the correct import.

{
"discriminator": "https://spec.openapis.org/oas/schema/vocab/keyword/discriminator",
"example": "https://spec.openapis.org/oas/schema/vocab/keyword/example",
"externalDocs": "https://spec.openapis.org/oas/schema/vocab/keyword/externalDocs",
"xml": "https://spec.openapis.org/oas/schema/vocab/keyword/xml",
},
);

const parseYamlFromFile = (filePath) => {
const schemaYaml = readFileSync(filePath, "utf8");
return YAML.parse(schemaYaml, { prettyErrors: true });
};
registerSchema(parseYamlFromFile("./src/schemas/validation/meta.yaml"));
registerSchema(parseYamlFromFile("./src/schemas/validation/dialect.yaml"));
registerSchema(parseYamlFromFile("./src/schemas/validation/schema.yaml"));

///////////////////////////////////////////////////////////////////////////////

const { allLocations, visitedLocations } = await runTests(argv[2], argv[3]);
Expand All @@ -122,16 +192,13 @@ if (notCovered.length > 0) {
const firstNotCovered = notCovered.slice(0, maxNotCovered);
if (notCovered.length > maxNotCovered) firstNotCovered.push("...");
console.log(firstNotCovered);
process.exitCode = 1;
}

console.log(
"Covered:",
visitedLocations.size,
(allocations.length - notCovered.length),
"of",
allLocations.length,
"(" + Math.floor((visitedLocations.size / allLocations.length) * 100) + "%)",
"(" + Math.floor(((allocations.length - notCovered.length) / allLocations.length) * 100) + "%)",
);

if (visitedLocations.size != allLocations.length) {
process.exitCode = 1;
}
2 changes: 1 addition & 1 deletion scripts/schema-test-coverage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ echo
echo "Schema Test Coverage"
echo

node scripts/schema-test-coverage.mjs src/schemas/validation/schema.yaml tests/schema/pass
node scripts/schema-test-coverage.mjs src/schemas/validation/schema-base.yaml tests/schema/pass
rc=$?

[[ "$branch" == "dev" ]] || exit $rc