Skip to content

Commit bf94ae1

Browse files
committed
add schema validation
Signed-off-by: shmck <[email protected]>
1 parent f29235a commit bf94ae1

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

Diff for: src/build.ts

+20-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as util from "util";
66
import { parse } from "./utils/parse";
77
import { getArg } from "./utils/args";
88
import { getCommits, CommitLogObject } from "./utils/commits";
9+
import { validateSchema } from "./utils/validate";
910
import * as T from "../typings/tutorial";
1011

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

61-
// load files
62+
// load markdown and files
6263
let _markdown: string;
6364
let _yaml: string;
6465
try {
@@ -72,6 +73,7 @@ async function build(args: string[]) {
7273
return;
7374
}
7475

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

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

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

112+
// validate tutorial based on json schema
113+
try {
114+
const valid = validateSchema(tutorial);
115+
if (!valid) {
116+
console.error("Tutorial validation failed. See above to see what to fix");
117+
return;
118+
}
119+
} catch (e) {
120+
console.error("Error validating tutorial schema:");
121+
console.error(e.message);
122+
}
123+
124+
// write tutorial
109125
if (tutorial) {
110126
try {
111127
await write(options.output, JSON.stringify(tutorial), "utf8");
128+
console.info(`Success! See output at ${options.output}`);
112129
} catch (e) {
113-
console.error("Error writing tutorial json:");
130+
console.error("Error writing tutorial json file:");
114131
console.error(e.message);
115132
}
116133
}

Diff for: src/utils/logs.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ const _info = console.info;
77

88
console.error = function () {
99
// @ts-ignore
10-
_error(red.bold.apply(console, arguments));
10+
_error(red.apply(console, arguments));
1111
};
1212

1313
console.warn = function () {
1414
// @ts-ignore
15-
_warn(yellow.bold.apply(console, arguments));
15+
_warn(yellow.apply(console, arguments));
1616
};
1717

1818
console.info = function () {

Diff for: src/utils/validate.ts

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ export function validateSchema(json: any): boolean | PromiseLike<boolean> {
1616
if (!valid) {
1717
// log errors
1818
if (process.env.NODE_ENV !== "test") {
19-
console.error("Validation failed. See below for details");
2019
jsonSchema.errors?.forEach((error: JsonSchema.ErrorObject) => {
2120
console.warn(
2221
`Validation error at ${error.dataPath} - ${error.message}`

0 commit comments

Comments
 (0)