Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
ShMcK merged 4 commits intomasterfromfeature/validate
May 31, 2020
Merged
Show file tree
Hide file tree
Changes from1 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
PrevPrevious commit
NextNext commit
validate schema with tests
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
  • Loading branch information
@ShMcK
ShMcK committedMay 31, 2020
commitf29235a5b40392726065a8b67c986c3f66c8fe6a
12 changes: 6 additions & 6 deletionssrc/utils/schema/meta.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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: "TutorialVersion",
$id: "https://coderoad.io/tutorial-schema.json",
title: "TutorialSchema",
description:
"A CodeRoad tutorialversion. This JSON data is converted into a tutorial with the CodeRoad editor extension",
"A CodeRoad tutorialschema data. This JSON data is converted into a tutorial with the CodeRoad editor extension",
definitions: {
semantic_version: {
type: "string",
Expand DownExpand 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 deletionssrc/utils/validate.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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}`
);
});
}
}

returntrue;
returnvalid;
}
51 changes: 51 additions & 0 deletionstests/validate.test.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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);
});
});

[8]ページ先頭

©2009-2025 Movatter.jp