-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidateSchema.ts
49 lines (43 loc) · 1.25 KB
/
validateSchema.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import fs from "fs";
import mustache from "mustache";
import Ajv from "ajv";
import schema from "./yaml.schema.json";
import { load } from "js-yaml";
type Variable = {
[key: string]: boolean | string;
};
function extractRenderVariables(template: string) {
let variables = [{}];
const regex = /([\{]+)([^{}}]+)([\}]+)/g;
let match;
while ((match = regex.exec(template)) !== null) {
const variable = match[2].replace(/[#^\/]/g, "");
if (variable !== match[2]) {
if (!variables.some((v: Variable) => v[variable])) {
variables = [
...variables.map((v) => {
return { ...v, [variable]: true };
}),
...variables.map((v) => {
return { ...v, [variable]: false };
}),
];
}
} else {
variables = variables.map((v) => {
return { ...v, [variable]: variable };
});
}
}
return variables;
}
const validator = new Ajv({ allowUnionTypes: true }).compile(schema);
const ymlFile = fs.readFileSync("teamsapp.yaml.tpl", "utf8");
const variables = extractRenderVariables(ymlFile);
variables.forEach((v) => {
const res = mustache.render(ymlFile, v);
const ymlDoc = load(res);
if (!validator(ymlDoc)) {
console.error(validator.errors);
}
});