Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 9.2k
Use full schema (schema-base.yaml) for coverage#4701
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
3a0fb3d0fadf2ef66d94cd986f8820eec3a0c75419File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| 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" | ||
| @@ -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; | ||
| } | ||
| @@ -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]); | ||
ralfhandl marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| @@ -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; | ||
| } | ||
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.
These seem to be unnecessary, commenting them out does not change the test result.
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.
@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.
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.
Would checks of the keyword's correct use go into the
interpretstubs?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.
@ralfhandl they're not stubs. Annotations are always valid, returning
trueis correct.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.
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.
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.
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.