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
71 changes: 14 additions & 57 deletions scripts/schema-test-coverage.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { readdir, readFile } from "node:fs/promises";
import YAML from "yaml";
import { join } from "node:path";
import { argv } from "node:process";
import { registerSchema, validate } from "@hyperjump/json-schema/draft-2020-12";
import { registerSchema, validate } from "@hyperjump/json-schema/openapi-3-1";
import "@hyperjump/json-schema/draft-04";
import { BASIC, addKeyword, defineVocabulary } from "@hyperjump/json-schema/experimental";
import { BASIC, defineVocabulary } from "@hyperjump/json-schema/experimental";

/**
* @import { EvaluationPlugin } from "@hyperjump/json-schema/experimental"
Expand Down Expand Up @@ -118,65 +118,22 @@ 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"));

const meta = parseYamlFromFile("./src/schemas/validation/meta.yaml");
const oasBaseVocab = Object.keys(meta.$vocabulary)[0];

defineVocabulary(oasBaseVocab, {
"discriminator": "https://spec.openapis.org/oas/3.0/keyword/discriminator",
"example": "https://spec.openapis.org/oas/3.0/keyword/example",
"externalDocs": "https://spec.openapis.org/oas/3.0/keyword/externalDocs",
"xml": "https://spec.openapis.org/oas/3.0/keyword/xml"
});

registerSchema(meta);
registerSchema(parseYamlFromFile("./src/schemas/validation/dialect.yaml"));
registerSchema(parseYamlFromFile("./src/schemas/validation/schema.yaml"));

Expand Down
63 changes: 10 additions & 53 deletions tests/schema/schema.test.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readdirSync, readFileSync } from "node:fs";
import YAML from "yaml";
import { registerSchema, validate, setMetaSchemaOutputFormat } from "@hyperjump/json-schema/openapi-3-1";
import { BASIC, addKeyword, defineVocabulary } from "@hyperjump/json-schema/experimental";
import { BASIC, defineVocabulary } from "@hyperjump/json-schema/experimental";
import { describe, test, expect } from "vitest";

import contentTypeParser from "content-type";
Expand All @@ -26,63 +26,20 @@ const parseYamlFromFile = (filePath) => {

setMetaSchemaOutputFormat(BASIC);

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;
},
});
const meta = parseYamlFromFile("./src/schemas/validation/meta.yaml");
const oasBaseVocab = Object.keys(meta.$vocabulary)[0];

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

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",
},
);

registerSchema(parseYamlFromFile("./src/schemas/validation/meta.yaml"));
registerSchema(meta);
registerSchema(parseYamlFromFile("./src/schemas/validation/dialect.yaml"));
registerSchema(parseYamlFromFile("./src/schemas/validation/schema.yaml"));

const validateOpenApi = await validate("./src/schemas/validation/schema-base.yaml");
const fixtures = './tests/schema';

Expand Down