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 all commits
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
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
60 changes: 30 additions & 30 deletions src/utils/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ export default {
examples: ["coderoad"],
},
setup: {
type: "object",
$ref: "#/definitions/setup_action",
description:
"Setup commits or commands used for setting up the test runner on tutorial launch",
Expand Down Expand Up @@ -97,40 +96,41 @@ export default {
additionalProperties: false,
required: ["uri", "branch"],
},
},
dependencies: {
type: "array",
description: "A list of tutorial dependencies",
items: {

dependencies: {
type: "array",
description: "A list of tutorial dependencies",
items: {
type: "object",
properties: {
name: {
type: "string",
description:
"The command line process name of the dependency. It will be checked by running `name --version`",
examples: ["node", "python"],
},
version: {
type: "string",
description:
"The version requirement. See https://github.com/npm/node-semver for options",
examples: [">=10"],
},
},
required: ["name", "version"],
},
},
appVersions: {
type: "object",
description:
"A list of compatable coderoad versions. Currently only a VSCode extension.",
properties: {
name: {
type: "string",
description:
"The command line process name of the dependency. It will be checked by running `name --version`",
examples: ["node", "python"],
},
version: {
vscode: {
type: "string",
description:
"The version requirement. See https://github.com/npm/node-semver for options",
examples: [">=10"],
"The version range for coderoad-vscode that this tutorial is compatable with",
examples: [">=0.7.0"],
},
},
required: ["name", "version"],
},
},
appVersions: {
type: "object",
description:
"A list of compatable coderoad versions. Currently only a VSCode extension.",
properties: {
vscode: {
type: "string",
description:
"The version range for coderoad-vscode that this tutorial is compatable with",
examples: [">=0.7.0"],
},
},
},
additionalProperties: false,
Expand Down Expand Up @@ -202,7 +202,7 @@ export default {
},
},
},
required: ["title", "description", "content"],
required: ["title", "summary", "content"],
},
minItems: 1,
},
Expand Down
16 changes: 8 additions & 8 deletions src/utils/schema/meta.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export default {
$schema: "http://json-schema.org/draft-07/schema#",
$id: "http://coderoad.io/tutorial_version.schema.json",
title: "Tutorial Version",
$id: "https://coderoad.io/tutorial-schema.json",
title: "Tutorial Schema",
description:
"A CodeRoad tutorial version. This JSON data is converted into a tutorial with the CodeRoad editor extension",
"A CodeRoad tutorial schema data. This JSON data is converted into a tutorial with the CodeRoad editor extension",
definitions: {
semantic_version: {
type: "string",
Expand Down Expand Up @@ -39,7 +39,7 @@ export default {
"An array of files which will be opened by the editor when entering the level or step",
items: {
$ref: "#/definitions/file_path",
uniqueItems: true,
// uniqueItems: true,
},
},
command_array: {
Expand All @@ -57,7 +57,7 @@ export default {
"An array of git commits which will be loaded when the level/step or solution is loaded",
items: {
$ref: "#/definitions/sha1_hash",
uniqueItems: true,
// uniqueItems: true,
},
minItems: 1,
},
Expand All @@ -77,12 +77,12 @@ export default {
},
watchers: {
type: "array",
description:
"An array file paths that, when updated, will trigger the test runner to run",
items: {
$ref: "#/definitions/file_path",
uniqueItems: true,
// uniqueItems: true,
},
description:
"An array file paths that, when updated, will trigger the test runner to run",
},
filter: {
type: "string",
Expand Down
24 changes: 14 additions & 10 deletions src/utils/validate.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
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: any) {
export function validateSchema(json: any): boolean | PromiseLike<boolean> {
// 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 jsonSchema = new JsonSchema({
allErrors: true,
// verbose: true,
});

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

if (!valid) {
// log errors
console.log(jsonSchema.errorsText());
throw new Error("Invalid schema. See log for details");
if (process.env.NODE_ENV !== "test") {
jsonSchema.errors?.forEach((error: JsonSchema.ErrorObject) => {
console.warn(
`Validation error at ${error.dataPath} - ${error.message}`
);
});
}
}

return true;
return valid;
}
51 changes: 51 additions & 0 deletions tests/validate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import * as T from "../typings/tutorial";
import { validateSchema } from "../src/utils/validate";

describe("validate", () => {
it("should reject an empty tutorial", () => {
const json = { version: "here" };

const valid = validateSchema(json);
expect(valid).toBe(false);
});
it("should return true for a valid tutorial", () => {
const json: Partial<T.Tutorial> = {
version: "0.1.0",
summary: { title: "Title", description: "Description" },
config: {
testRunner: {
command: "aCommand",
args: {
filter: "filter",
tap: "tap",
},
directory: "coderoad",
setup: {
commits: ["abcdef1"],
commands: ["npm install"],
},
},
repo: {
uri: "https://github.com/some-repo.git",
branch: "someBranch",
},
dependencies: [{ name: "name", version: ">=1" }],
appVersions: {
vscode: ">=0.7.0",
},
},
levels: [
{
id: "L1",
title: "Level 1",
summary: "The first level",
content: "The first level",
steps: [],
},
],
};

const valid = validateSchema(json);
expect(valid).toBe(true);
});
});