-
Notifications
You must be signed in to change notification settings - Fork 9.2k
Expand file tree
/
Copy pathschema.test.mjs
More file actions
70 lines (59 loc) · 2.73 KB
/
schema.test.mjs
File metadata and controls
70 lines (59 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { readdirSync, readFileSync } from "node:fs";
import YAML from "yaml";
import { registerSchema, validate, setMetaSchemaOutputFormat } from "@hyperjump/json-schema/openapi-3-1";
import { BASIC, defineVocabulary } from "@hyperjump/json-schema/experimental";
import { describe, test, expect } from "vitest";
import contentTypeParser from "content-type";
import { addMediaTypePlugin } from "@hyperjump/browser";
import { buildSchemaDocument } from "@hyperjump/json-schema/experimental";
addMediaTypePlugin("application/schema+yaml", {
parse: async (response) => {
const contentType = contentTypeParser.parse(response.headers.get("content-type") ?? "");
const contextDialectId = contentType.parameters.schema ?? contentType.parameters.profile;
const foo = YAML.parse(await response.text());
return buildSchemaDocument(foo, response.url, contextDialectId);
},
fileMatcher: (path) => path.endsWith(".yaml")
});
const parseYamlFromFile = (filePath) => {
const schemaYaml = readFileSync(filePath, "utf8");
return YAML.parse(schemaYaml, { prettyErrors: true });
};
setMetaSchemaOutputFormat(BASIC);
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"));
const validateOpenApi = await validate("./src/schemas/validation/schema-base.yaml");
const fixtures = './tests/schema';
describe("v3.1", () => {
describe("Pass", () => {
readdirSync(`${fixtures}/pass`, { withFileTypes: true })
.filter((entry) => entry.isFile() && /\.yaml$/.test(entry.name))
.forEach((entry) => {
test(entry.name, () => {
const instance = parseYamlFromFile(`${fixtures}/pass/${entry.name}`);
const output = validateOpenApi(instance, BASIC);
expect(output).to.deep.equal({ valid: true });
});
});
});
describe("Fail", () => {
readdirSync(`${fixtures}/fail`, { withFileTypes: true })
.filter((entry) => entry.isFile() && /\.yaml$/.test(entry.name))
.forEach((entry) => {
test(entry.name, () => {
const instance = parseYamlFromFile(`${fixtures}/fail/${entry.name}`);
const output = validateOpenApi(instance, BASIC);
expect(output.valid).to.equal(false);
});
});
});
});