Added $ref validator

This commit is contained in:
DaanV2
2022-06-21 19:14:57 +02:00
parent c9f0309296
commit 5a13affb66
4 changed files with 1014 additions and 611 deletions

1526
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -8,15 +8,15 @@
"test": "test"
},
"scripts": {
"compile": "tsc -b",
"build": "npm run clean && npm run compile",
"format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
"test": "mocha --debug-brk",
"clean": "rimraf lib",
"pretest": "npm run compile",
"prepublishOnly": "npm test",
"preversion": "",
"compile": "tsc -b",
"format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
"postversion": "git push && git push --tags",
"prepublishOnly": "npm test",
"pretest": "npm run compile",
"preversion": "",
"test": "mocha --debug-brk",
"version": "git add -A src",
"watch": "tsc -w -p ./src"
},
@@ -39,9 +39,11 @@
"@types/mocha": "^9.1.1",
"@types/node": "^17.0.31",
"chai": "^4.3.6",
"comment-json": "^4.1.1",
"json-loader": "^0.5.7",
"mocha": "^9.2.2",
"ts-loader": "^9.3.0",
"ts-mocha": "^10.0.0",
"ts-node": "^10.7.0",
"typescript": "^4.6.4"
},

View File

@@ -5,7 +5,6 @@ import { Files } from "./Utillity";
describe("Files", () => {
it("Root", () => {
const temp = Files.RootFolder();
console.log(temp);
expect(temp.endsWith("lib"), "ended with lib").to.be.false;
expect(temp.endsWith("lib\\test"), "ended with lib\\test").to.be.false;
@@ -14,7 +13,6 @@ describe("Files", () => {
it("Test", () => {
const temp = Files.TestFolder();
console.log(temp);
expect(temp.endsWith("lib"), "ended with lib").to.be.false;
});

View File

@@ -0,0 +1,69 @@
import path = require("path");
import { Files } from "./Utillity";
import * as fs from "fs";
import * as JSONC from "comment-json";
import { expect } from "chai";
describe.only("Validate", () => {
const folder = path.join(Files.TestFolder(), "..", "source");
console.log(folder);
const files = Files.GetFiles(folder);
files.forEach((filepath) => {
const filename = filepath.slice(folder.length);
describe(filename, () => {
let object: JsonSchema | undefined = undefined;
let data: string;
it("Can read file", () => {
data = fs.readFileSync(filepath, "utf8");
});
it("Can parse to json", () => {
object = <JsonSchema>JSONC.parse(data);
});
it("Not Undefined or null", () => {
expect(object).to.not.be.undefined;
expect(object).to.not.be.null;
});
it("Checking refs", () => {
if (!object) {
expect.fail();
return;
}
explore_refs(object, path.dirname(filepath));
});
});
});
});
interface JsonSchema {
$ref?: string;
[key: string]: any;
}
function explore_refs(data: JsonSchema, folder: string): void {
if (data.$ref) {
const ref = data.$ref;
if (!ref.startsWith("#")) {
const filepath = path.isAbsolute(ref) ? ref : path.join(folder, ref);
expect(fs.existsSync(filepath), `ref ${ref} exists`).to.be.true;
}
}
for (const key in data) {
const element = data[key];
switch (typeof element) {
case "object":
explore_refs(element, folder);
break;
}
}
}