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
refactor schema validation
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
  • Loading branch information
ShMcK committed Jun 7, 2020
commit 8cd8a8818ab0df4e2d938b13f76186be730180f1
3 changes: 2 additions & 1 deletion src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as util from "util";
import { parse } from "./utils/parse";
import { getArg } from "./utils/args";
import { getCommits, CommitLogObject } from "./utils/commits";
import tutorialSchema from "./schema/tutorial";
import { validateSchema } from "./utils/validateSchema";
import * as T from "../typings/tutorial";

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

// validate tutorial based on json schema
try {
const valid = validateSchema(tutorial);
const valid = validateSchema(tutorialSchema, tutorial);
if (!valid) {
console.error("Tutorial validation failed. See above to see what to fix");
return;
Expand Down
7 changes: 4 additions & 3 deletions src/utils/validateSchema.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import schema from "../schema/tutorial";

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

export function validateSchema(json: any): boolean | PromiseLike<boolean> {
export function validateSchema(
schema: any,
json: any
): boolean | PromiseLike<boolean> {
// validate using https://json-schema.org/
const jsonSchema = new JsonSchema({
allErrors: true,
Expand Down
29 changes: 0 additions & 29 deletions src/utils/validateSkeleton.ts

This file was deleted.

5 changes: 4 additions & 1 deletion tests/skeleton.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { validateSkeleton } from "../src/utils/validateSkeleton";
import { validateSchema } from "../src/utils/validateSchema";
import skeletonSchema from "../src/schema/skeleton";

const validateSkeleton = (json: any) => validateSchema(skeletonSchema, json);

const validJson = {
version: "0.1.0",
Expand Down
7 changes: 5 additions & 2 deletions tests/validate.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import * as T from "../typings/tutorial";
import tutorialSchema from "../src/schema/tutorial";
import { validateSchema } from "../src/utils/validateSchema";

const validateTutorial = (json: any) => validateSchema(tutorialSchema, json);

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

const valid = validateSchema(json);
const valid = validateTutorial(json);
expect(valid).toBe(false);
});
it("should return true for a valid tutorial", () => {
Expand Down Expand Up @@ -45,7 +48,7 @@ describe("validate tutorial", () => {
],
};

const valid = validateSchema(json);
const valid = validateTutorial(json);
expect(valid).toBe(true);
});
});