Skip to content

Commit 8cd8a88

Browse files
committed
refactor schema validation
Signed-off-by: shmck <[email protected]>
1 parent feec144 commit 8cd8a88

File tree

5 files changed

+15
-36
lines changed

5 files changed

+15
-36
lines changed

src/build.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as util from "util";
55
import { parse } from "./utils/parse";
66
import { getArg } from "./utils/args";
77
import { getCommits, CommitLogObject } from "./utils/commits";
8+
import tutorialSchema from "./schema/tutorial";
89
import { validateSchema } from "./utils/validateSchema";
910
import * as T from "../typings/tutorial";
1011

@@ -112,7 +113,7 @@ async function build(args: string[]) {
112113

113114
// validate tutorial based on json schema
114115
try {
115-
const valid = validateSchema(tutorial);
116+
const valid = validateSchema(tutorialSchema, tutorial);
116117
if (!valid) {
117118
console.error("Tutorial validation failed. See above to see what to fix");
118119
return;

src/utils/validateSchema.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import schema from "../schema/tutorial";
2-
31
// https://www.npmjs.com/package/ajv
42
// @ts-ignore ajv typings not working
53
import JsonSchema from "ajv";
64

7-
export function validateSchema(json: any): boolean | PromiseLike<boolean> {
5+
export function validateSchema(
6+
schema: any,
7+
json: any
8+
): boolean | PromiseLike<boolean> {
89
// validate using https://json-schema.org/
910
const jsonSchema = new JsonSchema({
1011
allErrors: true,

src/utils/validateSkeleton.ts

-29
This file was deleted.

tests/skeleton.test.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { validateSkeleton } from "../src/utils/validateSkeleton";
1+
import { validateSchema } from "../src/utils/validateSchema";
2+
import skeletonSchema from "../src/schema/skeleton";
3+
4+
const validateSkeleton = (json: any) => validateSchema(skeletonSchema, json);
25

36
const validJson = {
47
version: "0.1.0",

tests/validate.test.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import * as T from "../typings/tutorial";
2+
import tutorialSchema from "../src/schema/tutorial";
23
import { validateSchema } from "../src/utils/validateSchema";
34

5+
const validateTutorial = (json: any) => validateSchema(tutorialSchema, json);
6+
47
describe("validate tutorial", () => {
58
it("should reject an empty tutorial", () => {
69
const json = { version: "here" };
710

8-
const valid = validateSchema(json);
11+
const valid = validateTutorial(json);
912
expect(valid).toBe(false);
1013
});
1114
it("should return true for a valid tutorial", () => {
@@ -45,7 +48,7 @@ describe("validate tutorial", () => {
4548
],
4649
};
4750

48-
const valid = validateSchema(json);
51+
const valid = validateTutorial(json);
4952
expect(valid).toBe(true);
5053
});
5154
});

0 commit comments

Comments
 (0)