Skip to content

Commit 3a0fb3d

Browse files
committed
Use full schema (schema-base.yaml) for coverage
But don't count standard JSON Schema keywords.
1 parent 195a063 commit 3a0fb3d

File tree

2 files changed

+71
-5
lines changed

2 files changed

+71
-5
lines changed

scripts/schema-test-coverage.mjs

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import { readFileSync } from "node:fs";
12
import { readdir, readFile } from "node:fs/promises";
23
import YAML from "yaml";
34
import { join } from "node:path";
45
import { argv } from "node:process";
5-
import { validate } from "@hyperjump/json-schema/draft-2020-12";
6+
import { registerSchema, validate } from "@hyperjump/json-schema/draft-2020-12";
67
import "@hyperjump/json-schema/draft-04";
7-
import { BASIC } from "@hyperjump/json-schema/experimental";
8+
import { BASIC, addKeyword, defineVocabulary } from "@hyperjump/json-schema/experimental";
89

910
/**
1011
* @import { EvaluationPlugin } from "@hyperjump/json-schema/experimental"
@@ -45,7 +46,10 @@ class TestCoveragePlugin {
4546
this.allLocations = [];
4647

4748
for (const schemaLocation in context.ast) {
48-
if (schemaLocation === "metaData") {
49+
if (
50+
schemaLocation === "metaData" ||
51+
schemaLocation.includes("json-schema.org")
52+
) {
4953
continue;
5054
}
5155

@@ -110,6 +114,68 @@ const runTests = async (schemaUri, testDirectory) => {
110114
};
111115
};
112116

117+
addKeyword({
118+
id: "https://spec.openapis.org/oas/schema/vocab/keyword/discriminator",
119+
interpret: (discriminator, instance, context) => {
120+
return true;
121+
},
122+
/* discriminator is not exactly an annotation, but it's not allowed
123+
* to change the validation outcome (hence returing true from interopret())
124+
* and for our purposes of testing, this is sufficient.
125+
*/
126+
annotation: (discriminator) => {
127+
return discriminator;
128+
},
129+
});
130+
131+
addKeyword({
132+
id: "https://spec.openapis.org/oas/schema/vocab/keyword/example",
133+
interpret: (example, instance, context) => {
134+
return true;
135+
},
136+
annotation: (example) => {
137+
return example;
138+
},
139+
});
140+
141+
addKeyword({
142+
id: "https://spec.openapis.org/oas/schema/vocab/keyword/externalDocs",
143+
interpret: (externalDocs, instance, context) => {
144+
return true;
145+
},
146+
annotation: (externalDocs) => {
147+
return externalDocs;
148+
},
149+
});
150+
151+
addKeyword({
152+
id: "https://spec.openapis.org/oas/schema/vocab/keyword/xml",
153+
interpret: (xml, instance, context) => {
154+
return true;
155+
},
156+
annotation: (xml) => {
157+
return xml;
158+
},
159+
});
160+
161+
defineVocabulary(
162+
"https://spec.openapis.org/oas/3.1/vocab/base",
163+
{
164+
"discriminator": "https://spec.openapis.org/oas/schema/vocab/keyword/discriminator",
165+
"example": "https://spec.openapis.org/oas/schema/vocab/keyword/example",
166+
"externalDocs": "https://spec.openapis.org/oas/schema/vocab/keyword/externalDocs",
167+
"xml": "https://spec.openapis.org/oas/schema/vocab/keyword/xml",
168+
},
169+
);
170+
171+
const parseYamlFromFile = (filePath) => {
172+
const schemaYaml = readFileSync(filePath, "utf8");
173+
return YAML.parse(schemaYaml, { prettyErrors: true });
174+
};
175+
registerSchema(parseYamlFromFile("./src/schemas/validation/meta.yaml"));
176+
registerSchema(parseYamlFromFile("./src/schemas/validation/dialect.yaml"));
177+
registerSchema(parseYamlFromFile("./src/schemas/validation/schema.yaml"));
178+
113179
///////////////////////////////////////////////////////////////////////////////
114180

115181
const { allLocations, visitedLocations } = await runTests(argv[2], argv[3]);
@@ -134,4 +200,4 @@ console.log(
134200

135201
if (visitedLocations.size != allLocations.length) {
136202
process.exitCode = 1;
137-
}
203+
}

scripts/schema-test-coverage.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ echo
1212
echo "Schema Test Coverage"
1313
echo
1414

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

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

0 commit comments

Comments
 (0)