Skip to content

Commit bd7a645

Browse files
committed
3.1 Schema: Move scripts and tests to root
1 parent eccada9 commit bd7a645

File tree

6 files changed

+67
-40
lines changed

6 files changed

+67
-40
lines changed

.gitmodules

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
[submodule "schemas/v3.1/openapi3-examples"]
2-
path = schemas/v3.1/openapi3-examples
1+
[submodule "tests/v3.1/openapi3-examples"]
2+
path = tests/openapi3-examples
33
url = [email protected]:Mermade/openapi3-examples.git

schemas/v3.1/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ The TSC will then:
3131
The test suite is included as a git submodule of https://github.com/Mermade/openapi3-examples.
3232

3333
```bash
34-
npx mocha test.js
34+
npx mocha --recursive [repo root]/tests
3535
```
3636

3737
You can also validate a document individually.
3838

3939
```bash
40-
node validate.js path/to/document/to/validate.yaml
40+
node [repo root]/scripts/validate.js path/to/document/to/validate.yaml
4141
```

schemas/v3.1/validate.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

scripts/validate.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env node
2+
3+
const fs = require("fs");
4+
const yaml = require("yaml");
5+
const JsonSchema = require("@hyperjump/json-schema");
6+
const dialect = require("../schemas/v3.1/dialect/base.schema.json");
7+
const vocabulary = require("../schemas/v3.1/meta/base.schema.json");
8+
9+
10+
if (process.argv.length < 3) {
11+
console.log("Usage: validate [--schema=schema] [--version=2021-03-02] [--format=BASIC] path-to-file.yaml");
12+
console.log("\t--schema: (Default: schema) The name of the yaml schema file to use");
13+
console.log("\t--version: (Default: 2021-03-02) The version of the yaml schema file to use");
14+
console.log("\t--format: (Default: BASIC) The JSON Schema output format to use. Options: FLAG, BASIC, DETAILED, VERBOSE");
15+
process.exit(1);
16+
}
17+
18+
const args = process.argv.reduce((acc, arg) => {
19+
if (!arg.startsWith("--")) return acc;
20+
21+
const [argName, argValue] = arg.substring(2).split("=", 2);
22+
return { ...acc, [argName]: argValue };
23+
}, {});
24+
25+
(async function () {
26+
try {
27+
// Config
28+
JsonSchema.setMetaOutputFormat(outputFormat);
29+
//JsonSchema.setShouldMetaValidate(false);
30+
31+
const schemaType = args.schema || "schema";
32+
const schemaVersion = args.version || "2021-03-02";
33+
const ouputFormat = args.format || JsonSchema.BASIC;
34+
35+
// Load schemas
36+
JsonSchema.add(dialect);
37+
JsonSchema.add(vocabulary);
38+
fs.readdirSync(`${__dirname}/../schemas/v3.1`, { withFileTypes: true })
39+
.filter((entry) => entry.isFile() && /\.yaml$/.test(entry.name))
40+
.map((entry) => fs.readFileSync(`${__dirname}/../schemas/v3.1/${entry.name}`, "utf8"))
41+
.map((schemaYaml) => yaml.parse(schemaYaml))
42+
.forEach((schema) => JsonSchema.add(schema));
43+
44+
// Compile / meta-validate
45+
const schema = await JsonSchema.get(`https://spec.openapis.org/oas/3.1/${schemaType}/${schemaVersion}`);
46+
const validateSchema = await JsonSchema.validate(schema);
47+
48+
// Validate instance
49+
const instanceYaml = fs.readFileSync(`${process.cwd()}/${process.argv[process.argv.length - 1]}`, "utf8");
50+
const instance = yaml.parse(instanceYaml);
51+
const results = validateSchema(instance, outputFormat);
52+
console.log(JSON.stringify(results, null, " "));
53+
} catch (error) {
54+
console.log("************* Error ***************");
55+
console.log(error);
56+
console.log(JSON.stringify(error.output, null, " "));
57+
}
58+
}());

schemas/v3.1/test.js renamed to tests/v3.1/test.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ const fs = require("fs");
22
const yaml = require("yaml");
33
const JsonSchema = require("@hyperjump/json-schema");
44
const { expect } = require("chai");
5-
const dialect = require("./dialect/base.schema.json");
6-
const vocabulary = require("./meta/base.schema.json");
5+
const dialect = require("../../schemas/v3.1/dialect/base.schema.json");
6+
const vocabulary = require("../../schemas/v3.1/meta/base.schema.json");
77

88

9-
const testSuitePath = `${__dirname}/openapi3-examples/3.1`;
9+
const testSuitePath = `${__dirname}/../openapi3-examples/3.1`;
1010

1111
JsonSchema.setMetaOutputFormat(JsonSchema.BASIC);
1212
//JsonSchema.setShouldMetaValidate(false);
@@ -15,9 +15,8 @@ let metaSchema;
1515
before(async () => {
1616
JsonSchema.add(dialect);
1717
JsonSchema.add(vocabulary);
18-
JsonSchema.add(yaml.parse(fs.readFileSync(`${__dirname}/schema.yaml`, "utf8"), { prettyErrors: true }));
19-
JsonSchema.add(yaml.parse(fs.readFileSync(`${__dirname}/schema-base.yaml`, "utf8"), { prettyErrors: true }));
20-
metaSchema = await JsonSchema.get("https://spec.openapis.org/oas/3.1/schema-base/2021-03-02");
18+
JsonSchema.add(yaml.parse(fs.readFileSync(`${__dirname}/../../schemas/v3.1/schema.yaml`, "utf8"), { prettyErrors: true }));
19+
metaSchema = await JsonSchema.get("https://spec.openapis.org/oas/3.1/schema/2021-03-02");
2120
});
2221

2322
describe("Pass", () => {

0 commit comments

Comments
 (0)