Skip to content

Commit f29235a

Browse files
committed
validate schema with tests
Signed-off-by: shmck <[email protected]>
1 parent cc46fed commit f29235a

File tree

3 files changed

+72
-16
lines changed

3 files changed

+72
-16
lines changed

Diff for: src/utils/schema/meta.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
export default {
22
$schema: "http://json-schema.org/draft-07/schema#",
3-
$id: "http://coderoad.io/tutorial_version.schema.json",
4-
title: "Tutorial Version",
3+
$id: "https://coderoad.io/tutorial-schema.json",
4+
title: "Tutorial Schema",
55
description:
6-
"A CodeRoad tutorial version. This JSON data is converted into a tutorial with the CodeRoad editor extension",
6+
"A CodeRoad tutorial schema data. This JSON data is converted into a tutorial with the CodeRoad editor extension",
77
definitions: {
88
semantic_version: {
99
type: "string",
@@ -39,7 +39,7 @@ export default {
3939
"An array of files which will be opened by the editor when entering the level or step",
4040
items: {
4141
$ref: "#/definitions/file_path",
42-
uniqueItems: true,
42+
// uniqueItems: true,
4343
},
4444
},
4545
command_array: {
@@ -57,7 +57,7 @@ export default {
5757
"An array of git commits which will be loaded when the level/step or solution is loaded",
5858
items: {
5959
$ref: "#/definitions/sha1_hash",
60-
uniqueItems: true,
60+
// uniqueItems: true,
6161
},
6262
minItems: 1,
6363
},
@@ -79,7 +79,7 @@ export default {
7979
type: "array",
8080
items: {
8181
$ref: "#/definitions/file_path",
82-
uniqueItems: true,
82+
// uniqueItems: true,
8383
},
8484
description:
8585
"An array file paths that, when updated, will trigger the test runner to run",

Diff for: src/utils/validate.ts

+15-10
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
1-
import * as T from "../../typings/tutorial";
21
import schema from "./schema";
32

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

8-
export function validateSchema(json: any) {
7+
export function validateSchema(json: any): boolean | PromiseLike<boolean> {
98
// validate using https://json-schema.org/
10-
const jsonSchema = new JsonSchema({ allErrors: true, verbose: true });
11-
// support draft-07 of json schema
12-
jsonSchema.addMetaSchema(require("ajv/lib/refs/json-schema-draft-07.json"));
9+
const jsonSchema = new JsonSchema({
10+
allErrors: true,
11+
// verbose: true,
12+
});
1313

14-
const validator = jsonSchema.compile(schema);
15-
const valid = validator(json);
14+
const valid = jsonSchema.validate(schema, json);
1615

1716
if (!valid) {
1817
// log errors
19-
console.log(jsonSchema.errorsText());
20-
throw new Error("Invalid schema. See log for details");
18+
if (process.env.NODE_ENV !== "test") {
19+
console.error("Validation failed. See below for details");
20+
jsonSchema.errors?.forEach((error: JsonSchema.ErrorObject) => {
21+
console.warn(
22+
`Validation error at ${error.dataPath} - ${error.message}`
23+
);
24+
});
25+
}
2126
}
2227

23-
return true;
28+
return valid;
2429
}

Diff for: tests/validate.test.ts

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import * as T from "../typings/tutorial";
2+
import { validateSchema } from "../src/utils/validate";
3+
4+
describe("validate", () => {
5+
it("should reject an empty tutorial", () => {
6+
const json = { version: "here" };
7+
8+
const valid = validateSchema(json);
9+
expect(valid).toBe(false);
10+
});
11+
it("should return true for a valid tutorial", () => {
12+
const json: Partial<T.Tutorial> = {
13+
version: "0.1.0",
14+
summary: { title: "Title", description: "Description" },
15+
config: {
16+
testRunner: {
17+
command: "aCommand",
18+
args: {
19+
filter: "filter",
20+
tap: "tap",
21+
},
22+
directory: "coderoad",
23+
setup: {
24+
commits: ["abcdef1"],
25+
commands: ["npm install"],
26+
},
27+
},
28+
repo: {
29+
uri: "https://github.com/some-repo.git",
30+
branch: "someBranch",
31+
},
32+
dependencies: [{ name: "name", version: ">=1" }],
33+
appVersions: {
34+
vscode: ">=0.7.0",
35+
},
36+
},
37+
levels: [
38+
{
39+
id: "L1",
40+
title: "Level 1",
41+
summary: "The first level",
42+
content: "The first level",
43+
steps: [],
44+
},
45+
],
46+
};
47+
48+
const valid = validateSchema(json);
49+
expect(valid).toBe(true);
50+
});
51+
});

0 commit comments

Comments
 (0)