Skip to content

Feature/validate #14

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 4 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
Prev Previous commit
add schema validation
Signed-off-by: shmck <[email protected]>
  • Loading branch information
ShMcK committed May 31, 2020
commit bf94ae11c338dfa0596585aaa8f0992824ac843c
23 changes: 20 additions & 3 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as util from "util";
import { parse } from "./utils/parse";
import { getArg } from "./utils/args";
import { getCommits, CommitLogObject } from "./utils/commits";
import { validateSchema } from "./utils/validate";
import * as T from "../typings/tutorial";

const write = util.promisify(fs.writeFile);
Expand Down Expand Up @@ -58,7 +59,7 @@ async function build(args: string[]) {
// path to run build from
const localPath = path.join(process.cwd(), options.dir);

// load files
// load markdown and files
let _markdown: string;
let _yaml: string;
try {
Expand All @@ -72,6 +73,7 @@ async function build(args: string[]) {
return;
}

// parse yaml config
let config;
try {
config = yamlParser.load(_yaml);
Expand All @@ -80,6 +82,7 @@ async function build(args: string[]) {
console.error(e.message);
}

// load git commits to use in parse step
let commits: CommitLogObject;
try {
commits = await getCommits({
Expand All @@ -92,7 +95,7 @@ async function build(args: string[]) {
return;
}

// Otherwise, continue with the other options
// parse tutorial from markdown and yaml
let tutorial: T.Tutorial;
try {
tutorial = await parse({
Expand All @@ -106,11 +109,25 @@ async function build(args: string[]) {
return;
}

// validate tutorial based on json schema
try {
const valid = validateSchema(tutorial);
if (!valid) {
console.error("Tutorial validation failed. See above to see what to fix");
return;
}
} catch (e) {
console.error("Error validating tutorial schema:");
console.error(e.message);
}

// write tutorial
if (tutorial) {
try {
await write(options.output, JSON.stringify(tutorial), "utf8");
console.info(`Success! See output at ${options.output}`);
} catch (e) {
console.error("Error writing tutorial json:");
console.error("Error writing tutorial json file:");
console.error(e.message);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ const _info = console.info;

console.error = function () {
// @ts-ignore
_error(red.bold.apply(console, arguments));
_error(red.apply(console, arguments));
};

console.warn = function () {
// @ts-ignore
_warn(yellow.bold.apply(console, arguments));
_warn(yellow.apply(console, arguments));
};

console.info = function () {
Expand Down
1 change: 0 additions & 1 deletion src/utils/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export function validateSchema(json: any): boolean | PromiseLike<boolean> {
if (!valid) {
// log errors
if (process.env.NODE_ENV !== "test") {
console.error("Validation failed. See below for details");
jsonSchema.errors?.forEach((error: JsonSchema.ErrorObject) => {
console.warn(
`Validation error at ${error.dataPath} - ${error.message}`
Expand Down