Skip to content

Commit 8490cec

Browse files
committed
setup validator
Signed-off-by: shmck <[email protected]>
1 parent ecb6e5e commit 8490cec

File tree

4 files changed

+38
-11
lines changed

4 files changed

+38
-11
lines changed

package-lock.json

+13-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"author": "Argemiro Neto",
2020
"license": "ISC",
2121
"dependencies": {
22+
"ajv": "^6.12.2",
2223
"arg": "^4.1.3",
2324
"esm": "^3.2.25",
2425
"inquirer": "^7.1.0",
@@ -31,6 +32,7 @@
3132
},
3233
"devDependencies": {
3334
"@babel/preset-typescript": "^7.10.1",
35+
"@types/ajv": "^1.0.0",
3436
"@types/inquirer": "^6.5.0",
3537
"@types/jest": "^25.2.3",
3638
"@types/js-yaml": "^3.12.4",

src/build.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as yamlParser from "js-yaml";
21
import * as path from "path";
32
import * as _ from "lodash";
43
import * as fs from "fs";

src/utils/validate.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as T from "../../typings/tutorial";
2+
import schema from "./schema";
3+
4+
// https://www.npmjs.com/package/ajv
5+
// @ts-ignore ajv typings not working
6+
import JsonSchema from "ajv";
7+
8+
export function validateSchema(json: T.Tutorial) {
9+
// validate using https://json-schema.org/
10+
const jsonSchema = new JsonSchema({ allErrors: true, verbose: true });
11+
// support draft-07 of json schema
12+
jsonSchema.addMetaSchema(require("ajv/lib/refs/json-schema-draft-07.json"));
13+
14+
const valid = jsonSchema.compile(schema, json);
15+
16+
if (!valid) {
17+
// log errors
18+
console.log(jsonSchema.errorsText());
19+
throw new Error("Invalid schema. See log for details");
20+
}
21+
22+
return true;
23+
}

0 commit comments

Comments
 (0)