Skip to content

Commit 209d9a4

Browse files
authored
Merge pull request #3784 from Bellangelo/add-tests-for-example-files
Reinstate validation of JSON Schemas (and examples) in build process
2 parents a920ed4 + 1e36a8b commit 209d9a4

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

tests/v3.0/examples.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { readdirSync, readFileSync } from "node:fs";
2+
import YAML from "yaml";
3+
import { validate, setMetaSchemaOutputFormat } from "@hyperjump/json-schema/openapi-3-0";
4+
import { BASIC } from "@hyperjump/json-schema/experimental";
5+
import { describe, test, expect } from "vitest";
6+
7+
const parseYamlFromFile = (filePath) => {
8+
const schemaYaml = readFileSync(filePath, "utf8");
9+
return YAML.parse(schemaYaml, { prettyErrors: true });
10+
};
11+
12+
setMetaSchemaOutputFormat(BASIC);
13+
14+
const validateOpenApi = await validate("./schemas/v3.0/schema.json");
15+
const folder = './examples/v3.0/';
16+
17+
describe("v3.0 - Validate examples", async () => {
18+
readdirSync(folder, { withFileTypes: true })
19+
.filter((entry) => entry.isFile() && /\.yaml$/.test(entry.name))
20+
.forEach((entry) => {
21+
test(entry.name, () => {
22+
const instance = parseYamlFromFile(folder + entry.name);
23+
const output = validateOpenApi(instance, BASIC);
24+
expect(output.valid).to.equal(true);
25+
});
26+
});
27+
});

tests/v3.1/examples.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { readdirSync, readFileSync } from "node:fs";
2+
import YAML from "yaml";
3+
import { validate, setMetaSchemaOutputFormat } from "@hyperjump/json-schema/openapi-3-1";
4+
import { BASIC } from "@hyperjump/json-schema/experimental";
5+
import { describe, test, expect } from "vitest";
6+
7+
const parseYamlFromFile = (filePath) => {
8+
const schemaYaml = readFileSync(filePath, "utf8");
9+
return YAML.parse(schemaYaml, { prettyErrors: true });
10+
};
11+
12+
setMetaSchemaOutputFormat(BASIC);
13+
14+
const validateOpenApi = await validate("./schemas/v3.1/schema.json");
15+
const folder = './examples/v3.1/';
16+
17+
describe("v3.1 - Validate examples", async () => {
18+
readdirSync(folder, { withFileTypes: true })
19+
.filter((entry) => entry.isFile() && /\.yaml$/.test(entry.name))
20+
.forEach((entry) => {
21+
test(entry.name, () => {
22+
const instance = parseYamlFromFile(folder + entry.name);
23+
const output = validateOpenApi(instance, BASIC);
24+
expect(output.valid).to.equal(true);
25+
});
26+
});
27+
});

tests/v3.1/schema.test.mjs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@ import { validate, setMetaSchemaOutputFormat } from "@hyperjump/json-schema/open
44
import { BASIC } from "@hyperjump/json-schema/experimental";
55
import { describe, test, expect } from "vitest";
66

7-
87
const parseYamlFromFile = (filePath) => {
98
const schemaYaml = readFileSync(filePath, "utf8");
109
return YAML.parse(schemaYaml, { prettyErrors: true });
1110
};
1211

1312
setMetaSchemaOutputFormat(BASIC);
14-
// setShouldValidateSchema(false);
1513

1614
const validateOpenApi = await validate("./schemas/v3.1/schema.json");
1715

0 commit comments

Comments
 (0)