Fixing/item descriptors (#122)
* Adding correct samples, but know to fail * Renamed * Updated validator
This commit is contained in:
@@ -3,6 +3,7 @@ import { Files } from "./Utillity";
|
||||
import * as fs from "fs";
|
||||
import * as JSONC from "comment-json";
|
||||
import { expect } from "chai";
|
||||
import { ErrorAnnotation, Github } from "./Github";
|
||||
|
||||
describe("Validate", function () {
|
||||
const folder = path.join(Files.TestFolder(), "..", "source");
|
||||
@@ -12,31 +13,22 @@ describe("Validate", function () {
|
||||
files.forEach((filepath) => {
|
||||
const filename = filepath.slice(folder.length);
|
||||
|
||||
describe(filename, function () {
|
||||
it(`Validating schema parts: ${filename}`, function () {
|
||||
let object: JsonSchema | undefined = undefined;
|
||||
let data: string;
|
||||
|
||||
it("Can read file", function () {
|
||||
data = fs.readFileSync(filepath, "utf8");
|
||||
});
|
||||
data = fs.readFileSync(filepath, "utf8");
|
||||
object = <JsonSchema>JSONC.parse(data);
|
||||
expect(object).to.not.be.undefined;
|
||||
expect(object).to.not.be.null;
|
||||
|
||||
it("Can parse to json", function () {
|
||||
object = <JsonSchema>JSONC.parse(data);
|
||||
});
|
||||
if (!object) {
|
||||
this.skip();
|
||||
return;
|
||||
}
|
||||
|
||||
it("Not Undefined or null", function () {
|
||||
expect(object).to.not.be.undefined;
|
||||
expect(object).to.not.be.null;
|
||||
});
|
||||
|
||||
it("Check refs", function () {
|
||||
if (!object) {
|
||||
this.skip();
|
||||
return;
|
||||
}
|
||||
|
||||
explore_refs(object, path.dirname(filepath));
|
||||
});
|
||||
const explorer = new Explorer(data, filepath);
|
||||
explorer.explore_refs(object, path.dirname(filepath));
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -46,24 +38,61 @@ interface JsonSchema {
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
function explore_refs(data: JsonSchema, folder: string): void {
|
||||
if (data.$ref) {
|
||||
const ref = data.$ref;
|
||||
class Explorer {
|
||||
text: string;
|
||||
filepath: string;
|
||||
|
||||
if (!ref.startsWith("#")) {
|
||||
const filepath = path.isAbsolute(ref) ? ref : path.join(folder, ref);
|
||||
constructor(text: string, filepath: string) {
|
||||
this.text = text;
|
||||
this.filepath = filepath;
|
||||
}
|
||||
|
||||
expect(fs.existsSync(filepath), `ref ${ref} exists`).to.be.true;
|
||||
public 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);
|
||||
|
||||
if (!fs.existsSync(filepath)) {
|
||||
const anno = this.find(ref);
|
||||
anno.title = "Ref not found";
|
||||
anno.file = this.filepath;
|
||||
|
||||
Github.createError(`Ref not found: ${ref}`, anno);
|
||||
expect.fail(`ref ${ref} does not exists`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const key in data) {
|
||||
const element = data[key];
|
||||
|
||||
switch (typeof element) {
|
||||
case "object":
|
||||
this.explore_refs(element, folder);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const key in data) {
|
||||
const element = data[key];
|
||||
find(data: string): ErrorAnnotation {
|
||||
const index = this.text.indexOf(data);
|
||||
let lines = 1;
|
||||
let lastindex = 0;
|
||||
|
||||
switch (typeof element) {
|
||||
case "object":
|
||||
explore_refs(element, folder);
|
||||
break;
|
||||
for (let i = lastindex; i < index; i++) {
|
||||
const char = this.text[i];
|
||||
|
||||
if (char === "\n") {
|
||||
lastindex = i;
|
||||
lines++;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
line: lines,
|
||||
column: index - lastindex,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user