Files
minecraft-bedrock-json-schemas/test/src/Validator.test.ts

72 lines
1.7 KiB
TypeScript
Raw Normal View History

2022-06-21 19:14:57 +02:00
import path = require("path");
import { Files } from "./Utillity";
import * as fs from "fs";
import * as JSONC from "comment-json";
import { expect } from "chai";
2022-07-13 21:15:38 +02:00
describe.only("Validate", function () {
2022-06-21 19:14:57 +02:00
const folder = path.join(Files.TestFolder(), "..", "source");
console.log(folder);
const files = Files.GetFiles(folder);
files.forEach((filepath) => {
const filename = filepath.slice(folder.length);
2022-07-13 21:15:38 +02:00
describe(filename, function () {
2022-06-21 19:14:57 +02:00
let object: JsonSchema | undefined = undefined;
let data: string;
2022-07-13 21:15:38 +02:00
it("Can read file", function () {
2022-06-21 19:14:57 +02:00
data = fs.readFileSync(filepath, "utf8");
});
2022-07-13 21:15:38 +02:00
it("Can parse to json", function () {
2022-06-21 19:14:57 +02:00
object = <JsonSchema>JSONC.parse(data);
});
2022-07-13 21:15:38 +02:00
it("Not Undefined or null", function () {
2022-06-21 19:14:57 +02:00
expect(object).to.not.be.undefined;
expect(object).to.not.be.null;
});
2022-07-13 21:15:38 +02:00
describe("Checking refs", function () {
2022-06-21 19:14:57 +02:00
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);
2022-07-13 21:15:38 +02:00
it(`expecting ${ref} to exist from ${folder}`, function () {
expect(fs.existsSync(filepath), `ref ${ref} exists`).to.be.true;
});
2022-06-21 19:14:57 +02:00
}
}
for (const key in data) {
const element = data[key];
switch (typeof element) {
case "object":
explore_refs(element, folder);
break;
}
}
}