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 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
validate schema with tests
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
  • Loading branch information
ShMcK committed May 31, 2020
commit f29235a5b40392726065a8b67c986c3f66c8fe6a
12 changes: 6 additions & 6 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 @@ -79,7 +79,7 @@ export default {
type: "array",
items: {
$ref: "#/definitions/file_path",
uniqueItems: true,
// uniqueItems: true,
},
description:
"An array file paths that, when updated, will trigger the test runner to run",
Expand Down
25 changes: 15 additions & 10 deletions src/utils/validate.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
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") {
console.error("Validation failed. See below for details");
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);
});
});