Skip to content

dev: update from main #4707

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

Merged
merged 7 commits into from
Jun 13, 2025
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;
},
});

defineVocabulary(
"https://spec.openapis.org/oas/3.1/vocab/base",
{
"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