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
setup initial validate schema tests
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
  • Loading branch information
ShMcK committed Jun 7, 2020
commit feec144fe3f256b94b391f607d5ed28640a8bdf9
4 changes: 0 additions & 4 deletions src/schema/meta.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
export default {
$schema: "http://json-schema.org/draft-07/schema#",
$id: "https://coderoad.io/tutorial-schema.json",
title: "Tutorial Schema",
description:
"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 @@ -48,7 +45,6 @@ export default {
"An array of command line commands that will be called when the user enters the level or step. Currently commands are limited for security purposes",
items: {
type: "string",
enum: ["npm install"],
},
},
commit_array: {
Expand Down
185 changes: 185 additions & 0 deletions src/schema/skeleton.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
import meta from "./meta";

export default {
title: "Skeleton Schema",
description:
"A CodeRoad tutorial config schema. This data is paired up with the markdown to create a tutorial",
...meta,
type: "object",
properties: {
version: {
$ref: "#/definitions/semantic_version",
description: "The tutorial version. Must be unique for the tutorial.",
examples: ["0.1.0", "1.0.0"],
},

// config
config: {
type: "object",
properties: {
testRunner: {
type: "object",
description: "The test runner configuration",
properties: {
command: {
type: "string",
description: "Command line to start the test runner",
examples: ["./node_modules/.bin/mocha"],
},
args: {
type: "object",
description:
"A configuration of command line args for your test runner",
properties: {
filter: {
type: "string",
description:
"the command line arg for filtering tests with a regex pattern",
examples: ["--grep"],
},
tap: {
type: "string",
description:
"The command line arg for configuring a TAP reporter. See https://github.com/sindresorhus/awesome-tap for examples.",
examples: ["--reporter=mocha-tap-reporter"],
},
},
additionalProperties: false,
required: ["tap"],
},
directory: {
type: "string",
description: "An optional folder for the test runner",
examples: ["coderoad"],
},
setup: {
$ref: "#/definitions/setup_action",
description:
"Setup commits or commands used for setting up the test runner on tutorial launch",
},
},
required: ["command", "args"],
},
repo: {
type: "object",
description: "The repo holding the git commits for the tutorial",
properties: {
uri: {
type: "string",
description: "The uri source of the tutorial",
format: "uri",
examples: ["https://github.com/name/tutorial-name.git"],
},
branch: {
description:
"The branch of the repo where the tutorial config file exists",
type: "string",
examples: ["master"],
},
},
additionalProperties: false,
required: ["uri", "branch"],
},

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: {
vscode: {
type: "string",
description:
"The version range for coderoad-vscode that this tutorial is compatable with",
examples: [">=0.7.0"],
},
},
},
},
additionalProperties: false,
required: ["testRunner", "repo"],
},

// levels
levels: {
type: "array",
description:
'Levels are the stages a user goes through in the tutorial. A level may contain a group of tasks called "steps" that must be completed to proceed',
items: {
type: "object",
properties: {
id: {
type: "string",
description: "A level id",
examples: ["L1", "L11"],
},
setup: {
$ref: "#/definitions/setup_action",
description:
"An optional point for loading commits, running commands or opening files",
},
steps: {
type: "array",
items: {
type: "object",
properties: {
id: {
type: "string",
description: "A level id",
examples: ["L1S1", "L11S12"],
},
setup: {
allOf: [
{
$ref: "#/definitions/setup_action",
description:
"A point for loading commits. It can also run commands and/or open files",
},
],
},
solution: {
allOf: [
{
$ref: "#/definitions/setup_action",
description:
"The solution commits that can be loaded if the user gets stuck. It can also run commands and/or open files",
},
{
required: [],
},
],
},
},
required: ["id", "setup"],
},
},
},
required: ["id"],
},
minItems: 1,
},
},
additionalProperties: false,
required: ["version", "config", "levels"],
};
3 changes: 3 additions & 0 deletions src/schema/index.ts → src/schema/tutorial.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import meta from "./meta";

export default {
title: "Tutorial Schema",
description:
"A CodeRoad tutorial schema data. This JSON data is converted into a tutorial with the CodeRoad editor extension",
...meta,
type: "object",
properties: {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/validateSchema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import schema from "../schema";
import schema from "../schema/tutorial";

// https://www.npmjs.com/package/ajv
// @ts-ignore ajv typings not working
Expand Down
29 changes: 29 additions & 0 deletions src/utils/validateSkeleton.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import schema from "../schema/skeleton";

// https://www.npmjs.com/package/ajv
// @ts-ignore ajv typings not working
import JsonSchema from "ajv";

export function validateSkeleton(json: any): Boolean | PromiseLike<Boolean> {
// validate using https://json-schema.org/
const jsonSchema = new JsonSchema({
allErrors: true,
// verbose: true,
});

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

if (!valid) {
// log errors
/* istanbul ignore next */
if (process.env.NODE_ENV !== "test") {
jsonSchema.errors?.forEach((error: JsonSchema.ErrorObject) => {
console.warn(
`Validation error at ${error.dataPath} - ${error.message}`
);
});
}
}

return valid;
}
3 changes: 0 additions & 3 deletions src/utils/validateYaml.ts

This file was deleted.

99 changes: 99 additions & 0 deletions tests/skeleton.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { validateSkeleton } from "../src/utils/validateSkeleton";

const validJson = {
version: "0.1.0",
config: {
testRunner: {
directory: "coderoad",
setup: {
commands: [],
},
args: {
filter: "--grep",
tap: "--reporter=mocha-tap-reporter",
},
command: "./node_modules/.bin/mocha",
},
repo: {
uri: "http://github.com/somePath/toRepo.git",
branch: "codeBranch",
},
dependencies: [],
appVersions: {
vscode: ">=0.7.0",
},
},
levels: [
{
steps: [
{
id: "L1S1",
setup: {
files: ["package.json"],
},
solution: {
files: ["package.json"],
},
},
{
id: "L1S2",
setup: {
commands: ["npm install"],
},
solution: {
commands: ["npm install"],
},
},
{
id: "L1S3",
setup: {
files: ["package.json"],
watchers: ["package.json", "node_modules/some-package"],
},
solution: {
files: ["package.json"],
},
},
{
id: "L1S4",
setup: {
commands: [],
filter: "^Example 2",
subtasks: true,
},
},
],
id: "L1",
},
],
};

describe("validate skeleton", () => {
it("should fail an empty skeleton file", () => {
const json = {};

const valid = validateSkeleton(json);
expect(valid).toBe(false);
});
it("should parse a valid skeleton file", () => {
const json = { ...validJson };

const valid = validateSkeleton(json);
expect(valid).toBe(true);
});
it.todo("should fail if version is invalid");
it.todo("should fail if version is missing");
it.todo("should fail if config is missing");
it.todo("should fail if config testRunner is missing");
it.todo("should fail if config testRunner command is missing");
it.todo("should fail if config testRunner args tap is missing");
it.todo("should fail if repo is missing");
it.todo("should fail if repo uri is missing");
it.todo("should fail if repo uri is invalid");
it.todo("should fail if repo branch is missing");
it.todo("should fial if level is missing id");
it.todo("should fail if level setup is invalid");
it.todo("should fail if step is missing id");
it.todo("should fail if step setup is invalid");
it.todo("should fail if solution setup is invalid");
});
2 changes: 1 addition & 1 deletion tests/validate.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as T from "../typings/tutorial";
import { validateSchema } from "../src/utils/validateSchema";

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

Expand Down
18 changes: 0 additions & 18 deletions tests/yaml.test.ts

This file was deleted.