Skip to content

Commit bc4dd2c

Browse files
committed
add skeleton validation
Signed-off-by: shmck <[email protected]>
1 parent 429cdf8 commit bc4dd2c

File tree

3 files changed

+53
-41
lines changed

3 files changed

+53
-41
lines changed

src/build.ts

+20-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as util from "util";
55
import { parse } from "./utils/parse";
66
import { getArg } from "./utils/args";
77
import { getCommits, CommitLogObject } from "./utils/commits";
8+
import skeletonSchema from "./schema/skeleton";
89
import tutorialSchema from "./schema/tutorial";
910
import { validateSchema } from "./utils/validateSchema";
1011
import * as T from "../typings/tutorial";
@@ -71,25 +72,36 @@ async function build(args: string[]) {
7172
return;
7273
}
7374

74-
// parse yaml config
75-
let config;
75+
// parse yaml skeleton config
76+
let skeleton;
7677
try {
77-
config = yamlParser.load(_yaml);
78-
// TODO: validate yaml
79-
if (!config || !config.length) {
78+
skeleton = yamlParser.load(_yaml);
79+
if (!skeleton || !skeleton.length) {
8080
throw new Error("Invalid yaml file contents");
8181
}
8282
} catch (e) {
8383
console.error("Error parsing yaml");
8484
console.error(e.message);
8585
}
8686

87+
// validate skeleton based on skeleton json schema
88+
try {
89+
const valid = validateSchema(skeletonSchema, skeleton);
90+
if (!valid) {
91+
console.error("Tutorial validation failed. See above to see what to fix");
92+
return;
93+
}
94+
} catch (e) {
95+
console.error("Error validating tutorial schema:");
96+
console.error(e.message);
97+
}
98+
8799
// load git commits to use in parse step
88100
let commits: CommitLogObject;
89101
try {
90102
commits = await getCommits({
91103
localDir: localPath,
92-
codeBranch: config.config.repo.branch,
104+
codeBranch: skeleton.config.repo.branch,
93105
});
94106
} catch (e) {
95107
console.error("Error loading commits:");
@@ -102,7 +114,7 @@ async function build(args: string[]) {
102114
try {
103115
tutorial = await parse({
104116
text: _markdown,
105-
config,
117+
skeleton,
106118
commits,
107119
});
108120
} catch (e) {
@@ -111,7 +123,7 @@ async function build(args: string[]) {
111123
return;
112124
}
113125

114-
// validate tutorial based on json schema
126+
// validate tutorial based on tutorial json schema
115127
try {
116128
const valid = validateSchema(tutorialSchema, tutorial);
117129
if (!valid) {

src/utils/parse.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,17 @@ export function parseMdContent(md: string): TutorialFrame | never {
9090

9191
type ParseParams = {
9292
text: string;
93-
config: Partial<T.Tutorial | any>;
93+
skeleton: Partial<T.Tutorial | any>;
9494
commits: CommitLogObject;
9595
};
9696

9797
export function parse(params: ParseParams): any {
9898
const mdContent: TutorialFrame = parseMdContent(params.text);
9999

100100
const parsed: Partial<T.Tutorial> = {
101-
version: params.config.version,
101+
version: params.skeleton.version,
102102
summary: mdContent.summary,
103-
config: params.config.config || {},
103+
config: params.skeleton.config || {},
104104
levels: [],
105105
};
106106

@@ -114,8 +114,8 @@ export function parse(params: ParseParams): any {
114114
}
115115

116116
// merge content and tutorial
117-
if (params.config.levels && params.config.levels.length) {
118-
parsed.levels = params.config.levels
117+
if (params.skeleton.levels && params.skeleton.levels.length) {
118+
parsed.levels = params.skeleton.levels
119119
.map((level: T.Level, levelIndex: number) => {
120120
const levelContent = mdContent.levels[level.id];
121121

tests/parse.test.ts

+28-28
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ Short description to be shown as a tutorial's subtitle.
99
1010
`;
1111

12-
const config = { version: "0.1.0" };
12+
const skeleton = { version: "0.1.0" };
1313
const result = parse({
1414
text: md,
15-
config,
15+
skeleton,
1616
commits: {},
1717
});
1818
const expected = {
@@ -37,13 +37,13 @@ Description.
3737
Some text
3838
`;
3939

40-
const config = {
40+
const skeleton = {
4141
levels: [{ id: "L1" }],
4242
};
4343

4444
const result = parse({
4545
text: md,
46-
config,
46+
skeleton,
4747
commits: {},
4848
});
4949
const expected = {
@@ -73,7 +73,7 @@ Description.
7373
Some text
7474
`;
7575

76-
const config = {
76+
const skeleton = {
7777
levels: [
7878
{
7979
id: "L1",
@@ -85,7 +85,7 @@ Some text
8585
};
8686
const result = parse({
8787
text: md,
88-
config,
88+
skeleton,
8989
commits: {},
9090
});
9191
const expected = {
@@ -115,10 +115,10 @@ Description.
115115
Some text that becomes the summary
116116
`;
117117

118-
const config = { levels: [{ id: "L1" }] };
118+
const skeleton = { levels: [{ id: "L1" }] };
119119
const result = parse({
120120
text: md,
121-
config,
121+
skeleton,
122122
commits: {},
123123
});
124124
const expected = {
@@ -145,10 +145,10 @@ Description.
145145
Some text that becomes the summary and goes beyond the maximum length of 80 so that it gets truncated at the end
146146
`;
147147

148-
const config = { levels: [{ id: "L1" }] };
148+
const skeleton = { levels: [{ id: "L1" }] };
149149
const result = parse({
150150
text: md,
151-
config,
151+
skeleton,
152152
commits: {},
153153
});
154154
const expected = {
@@ -180,10 +180,10 @@ Second line
180180
Third line
181181
`;
182182

183-
const config = { levels: [{ id: "L1" }] };
183+
const skeleton = { levels: [{ id: "L1" }] };
184184
const result = parse({
185185
text: md,
186-
config,
186+
skeleton,
187187
commits: {},
188188
});
189189
const expected = {
@@ -215,7 +215,7 @@ First line
215215
216216
The first step
217217
`;
218-
const config = {
218+
const skeleton = {
219219
levels: [
220220
{
221221
id: "L1",
@@ -229,7 +229,7 @@ The first step
229229
};
230230
const result = parse({
231231
text: md,
232-
config,
232+
skeleton,
233233
commits: {
234234
L1S1Q: ["abcdefg1"],
235235
},
@@ -271,7 +271,7 @@ First line
271271
272272
The first step
273273
`;
274-
const config = {
274+
const skeleton = {
275275
levels: [
276276
{
277277
id: "L1",
@@ -285,7 +285,7 @@ The first step
285285
};
286286
const result = parse({
287287
text: md,
288-
config,
288+
skeleton,
289289
commits: {
290290
L1S1Q: ["abcdefg1", "123456789"],
291291
},
@@ -327,7 +327,7 @@ First line
327327
328328
The first step
329329
`;
330-
const config = {
330+
const skeleton = {
331331
levels: [
332332
{
333333
id: "L1",
@@ -336,7 +336,7 @@ The first step
336336
};
337337
const result = parse({
338338
text: md,
339-
config,
339+
skeleton,
340340
commits: {
341341
L1: ["abcdefg1"],
342342
},
@@ -372,7 +372,7 @@ First line
372372
373373
The first step
374374
`;
375-
const config = {
375+
const skeleton = {
376376
levels: [
377377
{
378378
id: "L1",
@@ -397,7 +397,7 @@ The first step
397397
};
398398
const result = parse({
399399
text: md,
400-
config,
400+
skeleton,
401401
commits: {
402402
L1S1Q: ["abcdefg1", "123456789"],
403403
L1S1A: ["1gfedcba", "987654321"],
@@ -462,7 +462,7 @@ Second level content.
462462
463463
The third step
464464
`;
465-
const config = {
465+
const skeleton = {
466466
levels: [
467467
{
468468
id: "L1",
@@ -522,7 +522,7 @@ The third step
522522
};
523523
const result = parse({
524524
text: md,
525-
config,
525+
skeleton,
526526
commits: {
527527
L1S1Q: ["abcdef1", "123456789"],
528528
L1S1A: ["1fedcba", "987654321"],
@@ -623,7 +623,7 @@ First level content.
623623
The first step
624624
625625
`;
626-
const config = {
626+
const skeleton = {
627627
levels: [
628628
{
629629
id: "L1",
@@ -637,7 +637,7 @@ The first step
637637
};
638638
const result = parse({
639639
text: md,
640-
config,
640+
skeleton,
641641
commits: {
642642
L1S1Q: ["abcdef1", "123456789"],
643643
},
@@ -674,7 +674,7 @@ The first step
674674
Description.
675675
`;
676676

677-
const config = {
677+
const skeleton = {
678678
config: {
679679
testRunner: {
680680
command: "./node_modules/.bin/mocha",
@@ -704,7 +704,7 @@ Description.
704704
};
705705
const result = parse({
706706
text: md,
707-
config,
707+
skeleton,
708708
commits: {},
709709
});
710710
const expected = {
@@ -747,7 +747,7 @@ Description.
747747
Description.
748748
`;
749749

750-
const config = {
750+
const skeleton = {
751751
config: {
752752
testRunner: {
753753
command: "./node_modules/.bin/mocha",
@@ -774,7 +774,7 @@ Description.
774774
};
775775
const result = parse({
776776
text: md,
777-
config,
777+
skeleton,
778778
commits: {
779779
INIT: ["abcdef1", "123456789"],
780780
},

0 commit comments

Comments
 (0)