Skip to content

Validate yaml #28

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 9 commits into from
Jun 7, 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
Next Next commit
add skeleton validation
Signed-off-by: shmck <[email protected]>
  • Loading branch information
ShMcK committed Jun 7, 2020
commit bc4dd2cb8bd039146bb46cde8cd6f4f88983f1f1
28 changes: 20 additions & 8 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as util from "util";
import { parse } from "./utils/parse";
import { getArg } from "./utils/args";
import { getCommits, CommitLogObject } from "./utils/commits";
import skeletonSchema from "./schema/skeleton";
import tutorialSchema from "./schema/tutorial";
import { validateSchema } from "./utils/validateSchema";
import * as T from "../typings/tutorial";
Expand Down Expand Up @@ -71,25 +72,36 @@ async function build(args: string[]) {
return;
}

// parse yaml config
let config;
// parse yaml skeleton config
let skeleton;
try {
config = yamlParser.load(_yaml);
// TODO: validate yaml
if (!config || !config.length) {
skeleton = yamlParser.load(_yaml);
if (!skeleton || !skeleton.length) {
throw new Error("Invalid yaml file contents");
}
} catch (e) {
console.error("Error parsing yaml");
console.error(e.message);
}

// validate skeleton based on skeleton json schema
try {
const valid = validateSchema(skeletonSchema, skeleton);
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);
}

// load git commits to use in parse step
let commits: CommitLogObject;
try {
commits = await getCommits({
localDir: localPath,
codeBranch: config.config.repo.branch,
codeBranch: skeleton.config.repo.branch,
});
} catch (e) {
console.error("Error loading commits:");
Expand All @@ -102,7 +114,7 @@ async function build(args: string[]) {
try {
tutorial = await parse({
text: _markdown,
config,
skeleton,
commits,
});
} catch (e) {
Expand All @@ -111,7 +123,7 @@ async function build(args: string[]) {
return;
}

// validate tutorial based on json schema
// validate tutorial based on tutorial json schema
try {
const valid = validateSchema(tutorialSchema, tutorial);
if (!valid) {
Expand Down
10 changes: 5 additions & 5 deletions src/utils/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,17 @@ export function parseMdContent(md: string): TutorialFrame | never {

type ParseParams = {
text: string;
config: Partial<T.Tutorial | any>;
skeleton: Partial<T.Tutorial | any>;
commits: CommitLogObject;
};

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

const parsed: Partial<T.Tutorial> = {
version: params.config.version,
version: params.skeleton.version,
summary: mdContent.summary,
config: params.config.config || {},
config: params.skeleton.config || {},
levels: [],
};

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

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

Expand Down
56 changes: 28 additions & 28 deletions tests/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ Short description to be shown as a tutorial's subtitle.

`;

const config = { version: "0.1.0" };
const skeleton = { version: "0.1.0" };
const result = parse({
text: md,
config,
skeleton,
commits: {},
});
const expected = {
Expand All @@ -37,13 +37,13 @@ Description.
Some text
`;

const config = {
const skeleton = {
levels: [{ id: "L1" }],
};

const result = parse({
text: md,
config,
skeleton,
commits: {},
});
const expected = {
Expand Down Expand Up @@ -73,7 +73,7 @@ Description.
Some text
`;

const config = {
const skeleton = {
levels: [
{
id: "L1",
Expand All @@ -85,7 +85,7 @@ Some text
};
const result = parse({
text: md,
config,
skeleton,
commits: {},
});
const expected = {
Expand Down Expand Up @@ -115,10 +115,10 @@ Description.
Some text that becomes the summary
`;

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

const config = { levels: [{ id: "L1" }] };
const skeleton = { levels: [{ id: "L1" }] };
const result = parse({
text: md,
config,
skeleton,
commits: {},
});
const expected = {
Expand Down Expand Up @@ -180,10 +180,10 @@ Second line
Third line
`;

const config = { levels: [{ id: "L1" }] };
const skeleton = { levels: [{ id: "L1" }] };
const result = parse({
text: md,
config,
skeleton,
commits: {},
});
const expected = {
Expand Down Expand Up @@ -215,7 +215,7 @@ First line

The first step
`;
const config = {
const skeleton = {
levels: [
{
id: "L1",
Expand All @@ -229,7 +229,7 @@ The first step
};
const result = parse({
text: md,
config,
skeleton,
commits: {
L1S1Q: ["abcdefg1"],
},
Expand Down Expand Up @@ -271,7 +271,7 @@ First line

The first step
`;
const config = {
const skeleton = {
levels: [
{
id: "L1",
Expand All @@ -285,7 +285,7 @@ The first step
};
const result = parse({
text: md,
config,
skeleton,
commits: {
L1S1Q: ["abcdefg1", "123456789"],
},
Expand Down Expand Up @@ -327,7 +327,7 @@ First line

The first step
`;
const config = {
const skeleton = {
levels: [
{
id: "L1",
Expand All @@ -336,7 +336,7 @@ The first step
};
const result = parse({
text: md,
config,
skeleton,
commits: {
L1: ["abcdefg1"],
},
Expand Down Expand Up @@ -372,7 +372,7 @@ First line

The first step
`;
const config = {
const skeleton = {
levels: [
{
id: "L1",
Expand All @@ -397,7 +397,7 @@ The first step
};
const result = parse({
text: md,
config,
skeleton,
commits: {
L1S1Q: ["abcdefg1", "123456789"],
L1S1A: ["1gfedcba", "987654321"],
Expand Down Expand Up @@ -462,7 +462,7 @@ Second level content.

The third step
`;
const config = {
const skeleton = {
levels: [
{
id: "L1",
Expand Down Expand Up @@ -522,7 +522,7 @@ The third step
};
const result = parse({
text: md,
config,
skeleton,
commits: {
L1S1Q: ["abcdef1", "123456789"],
L1S1A: ["1fedcba", "987654321"],
Expand Down Expand Up @@ -623,7 +623,7 @@ First level content.
The first step

`;
const config = {
const skeleton = {
levels: [
{
id: "L1",
Expand All @@ -637,7 +637,7 @@ The first step
};
const result = parse({
text: md,
config,
skeleton,
commits: {
L1S1Q: ["abcdef1", "123456789"],
},
Expand Down Expand Up @@ -674,7 +674,7 @@ The first step
Description.
`;

const config = {
const skeleton = {
config: {
testRunner: {
command: "./node_modules/.bin/mocha",
Expand Down Expand Up @@ -704,7 +704,7 @@ Description.
};
const result = parse({
text: md,
config,
skeleton,
commits: {},
});
const expected = {
Expand Down Expand Up @@ -747,7 +747,7 @@ Description.
Description.
`;

const config = {
const skeleton = {
config: {
testRunner: {
command: "./node_modules/.bin/mocha",
Expand All @@ -774,7 +774,7 @@ Description.
};
const result = parse({
text: md,
config,
skeleton,
commits: {
INIT: ["abcdef1", "123456789"],
},
Expand Down