Skip to content

Validate/schema #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 31, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
setup validator
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
  • Loading branch information
ShMcK committed May 31, 2020
commit 8490cecd222cbe63e3f2cde8e52fd485d9b8ac26
23 changes: 13 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"author": "Argemiro Neto",
"license": "ISC",
"dependencies": {
"ajv": "^6.12.2",
"arg": "^4.1.3",
"esm": "^3.2.25",
"inquirer": "^7.1.0",
Expand All @@ -31,6 +32,7 @@
},
"devDependencies": {
"@babel/preset-typescript": "^7.10.1",
"@types/ajv": "^1.0.0",
"@types/inquirer": "^6.5.0",
"@types/jest": "^25.2.3",
"@types/js-yaml": "^3.12.4",
Expand Down
1 change: 0 additions & 1 deletion src/build.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as yamlParser from "js-yaml";
import * as path from "path";
import * as _ from "lodash";
import * as fs from "fs";
Expand Down
23 changes: 23 additions & 0 deletions src/utils/validate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as T from "../../typings/tutorial";
import schema from "./schema";

// https://www.npmjs.com/package/ajv
// @ts-ignore ajv typings not working
import JsonSchema from "ajv";

export function validateSchema(json: T.Tutorial) {
// validate using https://json-schema.org/
const jsonSchema = new JsonSchema({ allErrors: true, verbose: true });
// support draft-07 of json schema
jsonSchema.addMetaSchema(require("ajv/lib/refs/json-schema-draft-07.json"));

const valid = jsonSchema.compile(schema, json);

if (!valid) {
// log errors
console.log(jsonSchema.errorsText());
throw new Error("Invalid schema. See log for details");
}

return true;
}