3.1 Schema: Move scripts and tests to root
MODIFY
.gitmodules
.gitmodules
-[submodule "schemas/v3.1/openapi3-examples"]
- path = schemas/v3.1/openapi3-examples
+[submodule "tests/v3.1/openapi3-examples"]
+ path = tests/openapi3-examples
url = git@github.com:Mermade/openapi3-examples.git

MODIFY
schemas/v3.1/README.md
schemas/v3.1/README.md
@@ -31,11 +31,11 @@ The TSC will then:
The test suite is included as a git submodule of https://github.com/Mermade/openapi3-examples.
```bash
-npx mocha test.js
+npx mocha --recursive [repo root]/tests
```
You can also validate a document individually.
```bash
-node validate.js path/to/document/to/validate.yaml
+node [repo root]/scripts/validate.js path/to/document/to/validate.yaml
```

DELETE
schemas/v3.1/validate.js
-const fs = require("fs");
-const yaml = require("yaml");
-const JsonSchema = require("@hyperjump/json-schema");
-const dialect = require("./dialect/base.schema.json");
-const vocabulary = require("./meta/base.schema.json");
-
-
-JsonSchema.setMetaOutputFormat(JsonSchema.BASIC);
-//JsonSchema.setShouldMetaValidate(false);
-
-(async function () {
- try {
- // Compile / meta-validate
- JsonSchema.add(dialect);
- JsonSchema.add(vocabulary);
- JsonSchema.add(yaml.parse(fs.readFileSync(`${__dirname}/schema.yaml`, "utf8")));
- const schema = await JsonSchema.get("https://spec.openapis.org/oas/3.1/schema/2021-03-02");
- const validateSchema = await JsonSchema.validate(schema);
-
- // Validate instance
- const instance = yaml.parse(fs.readFileSync(`${__dirname}/${process.argv[2]}`, "utf8"));
- const results = validateSchema(instance, JsonSchema.BASIC);
- console.log(JSON.stringify(results, null, " "));
- } catch (error) {
- console.log("************* Error ***************");
- console.log(error);
- console.log(JSON.stringify(error.output, null, " "));
- //console.log(error.output);
- }
-}());

ADD
scripts/validate.js
+#!/usr/bin/env node
+
+const fs = require("fs");
+const yaml = require("yaml");
+const JsonSchema = require("@hyperjump/json-schema");
+const dialect = require("../schemas/v3.1/dialect/base.schema.json");
+const vocabulary = require("../schemas/v3.1/meta/base.schema.json");
+
+
+if (process.argv.length < 3) {
+ console.log("Usage: validate [--schema=schema] [--version=2021-03-02] [--format=BASIC] path-to-file.yaml");
+ console.log("\t--schema: (Default: schema) The name of the yaml schema file to use");
+ console.log("\t--version: (Default: 2021-03-02) The version of the yaml schema file to use");
+ console.log("\t--format: (Default: BASIC) The JSON Schema output format to use. Options: FLAG, BASIC, DETAILED, VERBOSE");
+ process.exit(1);
+}
+
+const args = process.argv.reduce((acc, arg) => {
+ if (!arg.startsWith("--")) return acc;
+
+ const [argName, argValue] = arg.substring(2).split("=", 2);
+ return { ...acc, [argName]: argValue };
+}, {});
+
+(async function () {
+ try {
+ // Config
+ JsonSchema.setMetaOutputFormat(outputFormat);
+ //JsonSchema.setShouldMetaValidate(false);
+
+ const schemaType = args.schema || "schema";
+ const schemaVersion = args.version || "2021-03-02";
+ const ouputFormat = args.format || JsonSchema.BASIC;
+
+ // Load schemas
+ JsonSchema.add(dialect);
+ JsonSchema.add(vocabulary);
+ fs.readdirSync(`${__dirname}/../schemas/v3.1`, { withFileTypes: true })
+ .filter((entry) => entry.isFile() && /\.yaml$/.test(entry.name))
+ .map((entry) => fs.readFileSync(`${__dirname}/../schemas/v3.1/${entry.name}`, "utf8"))
+ .map((schemaYaml) => yaml.parse(schemaYaml))
+ .forEach((schema) => JsonSchema.add(schema));
+
+ // Compile / meta-validate
+ const schema = await JsonSchema.get(`https://spec.openapis.org/oas/3.1/${schemaType}/${schemaVersion}`);
+ const validateSchema = await JsonSchema.validate(schema);
+
+ // Validate instance
+ const instanceYaml = fs.readFileSync(`${process.cwd()}/${process.argv[process.argv.length - 1]}`, "utf8");
+ const instance = yaml.parse(instanceYaml);
+ const results = validateSchema(instance, outputFormat);
+ console.log(JSON.stringify(results, null, " "));
+ } catch (error) {
+ console.log("************* Error ***************");
+ console.log(error);
+ console.log(JSON.stringify(error.output, null, " "));
+ }
+}());

RENAME
schemas/v3.1/openapi3-examples
tests/openapi3-examples
RENAME
schemas/v3.1/test.js
tests/v3.1/test.js
@@ -2,11 +2,11 @@ const fs = require("fs");
const yaml = require("yaml");
const JsonSchema = require("@hyperjump/json-schema");
const { expect } = require("chai");
-const dialect = require("./dialect/base.schema.json");
-const vocabulary = require("./meta/base.schema.json");
+const dialect = require("../../schemas/v3.1/dialect/base.schema.json");
+const vocabulary = require("../../schemas/v3.1/meta/base.schema.json");
-const testSuitePath = `${__dirname}/openapi3-examples/3.1`;
+const testSuitePath = `${__dirname}/../openapi3-examples/3.1`;
JsonSchema.setMetaOutputFormat(JsonSchema.BASIC);
//JsonSchema.setShouldMetaValidate(false);
@@ -15,9 +15,8 @@ let metaSchema;
before(async () => {
JsonSchema.add(dialect);
JsonSchema.add(vocabulary);
- JsonSchema.add(yaml.parse(fs.readFileSync(`${__dirname}/schema.yaml`, "utf8"), { prettyErrors: true }));
- JsonSchema.add(yaml.parse(fs.readFileSync(`${__dirname}/schema-base.yaml`, "utf8"), { prettyErrors: true }));
- metaSchema = await JsonSchema.get("https://spec.openapis.org/oas/3.1/schema-base/2021-03-02");
+ JsonSchema.add(yaml.parse(fs.readFileSync(`${__dirname}/../../schemas/v3.1/schema.yaml`, "utf8"), { prettyErrors: true }));
+ metaSchema = await JsonSchema.get("https://spec.openapis.org/oas/3.1/schema/2021-03-02");
});
describe("Pass", () => {

3.1 Schema: Move scripts and tests to root