Commit 9a5f8fe0 authored by David Sveningsson's avatar David Sveningsson

fix(error): better schema validation error

parent 165da729
/* eslint-disable no-console, no-process-exit, sonarjs/no-duplicate-string */
import { TokenDump } from "../engine";
import { SchemaValidationError } from "../error";
import { UserError } from "../error/user-error";
import { MetaValidationError } from "../meta/validation-error";
import { Report, Reporter, Result } from "../reporter";
import { eventFormatter } from "./json";
......@@ -9,6 +9,7 @@ const pkg = require("../../package.json");
import chalk from "chalk";
import minimist from "minimist";
import path from "path";
import { CLI } from "./cli";
enum Mode {
......@@ -101,8 +102,14 @@ function renameStdin(report: Report, filename: string): void {
}
}
function handleValidationError(err: MetaValidationError): void {
console.log(err.prettyError());
function handleValidationError(err: SchemaValidationError): void {
const filename = path.relative(process.cwd(), err.filename);
console.log(chalk.red(`A configuration error was found in "${filename}":`));
if (console.group) console.group();
{
console.log(err.prettyError());
}
if (console.group) console.groupEnd();
}
function handleUserError(err: UserError): void {
......@@ -297,7 +304,7 @@ try {
process.exit(0);
}
} catch (err) {
if (err instanceof MetaValidationError) {
if (err instanceof SchemaValidationError) {
handleValidationError(err);
} else if (err instanceof UserError) {
handleUserError(err);
......
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`MetaValidationError should pretty-print validation errors 1`] = `
exports[`SchemaValidationError should pretty-print validation errors 1`] = `
"TYPE should be boolean
1 | {
......
export { UserError } from "./user-error";
export { NestedError } from "./nested-error";
export { SchemaValidationError } from "./schema-validation-error";
import stripAnsi = require("strip-ansi");
import { MetaElement } from "./element";
import { MetaTable } from "./table";
import { MetaValidationError } from "./validation-error";
import { MetaElement } from "../meta/element";
import { MetaTable } from "../meta/table";
import { SchemaValidationError } from "./schema-validation-error";
it("MetaValidationError should pretty-print validation errors ", () => {
it("SchemaValidationError should pretty-print validation errors ", () => {
expect.assertions(1);
const table = new MetaTable();
try {
......@@ -13,7 +13,7 @@ it("MetaValidationError should pretty-print validation errors ", () => {
} as unknown) as MetaElement,
});
} catch (err) {
if (err instanceof MetaValidationError) {
if (err instanceof SchemaValidationError) {
const output = (err.prettyError() as unknown) as string;
/* cannot test prettyError() method with builtin helpers */
......
import Ajv from "ajv";
import betterAjvErrors from "better-ajv-errors";
import { UserError } from "../error/user-error";
import { MetaDataTable } from "./element";
export class MetaValidationError extends UserError {
private obj: MetaDataTable;
export class SchemaValidationError extends UserError {
public filename: string | null;
private obj: any;
private schema: any;
private errors: Ajv.ErrorObject[];
public constructor(
filename: string | null,
message: string,
obj: MetaDataTable,
obj: any,
schema: any,
errors: Ajv.ErrorObject[]
) {
super(message);
this.filename = filename;
this.obj = obj;
this.schema = schema;
this.errors = errors;
......
......@@ -3,7 +3,7 @@ import betterAjvErrors from "better-ajv-errors";
import deepmerge from "deepmerge";
import jsonMergePatch from "json-merge-patch";
import { HtmlElement } from "../dom";
import { UserError } from "../error/user-error";
import { SchemaValidationError, UserError } from "../error";
import { SchemaValidationPatch } from "../plugin";
import {
ElementTable,
......@@ -13,7 +13,6 @@ import {
MetaLookupableProperty,
PropertyExpression,
} from "./element";
import { MetaValidationError } from "./validation-error";
const dynamicKeys = [
"metadata",
......@@ -73,7 +72,10 @@ export class MetaTable {
/**
* Load metadata table from object.
*/
public loadFromObject(obj: MetaDataTable): void {
public loadFromObject(
obj: MetaDataTable,
filename: string | null = null
): void {
const ajv = new Ajv({ jsonPointers: true });
const validator = ajv.compile(this.schema);
const valid = validator(obj);
......@@ -82,7 +84,8 @@ export class MetaTable {
format: "js",
}) as any;
const message = output[0].error;
throw new MetaValidationError(
throw new SchemaValidationError(
filename,
`Element metadata is not valid: ${message}`,
obj,
this.schema,
......@@ -108,7 +111,7 @@ export class MetaTable {
err
);
}
this.loadFromObject(clone(json));
this.loadFromObject(clone(json), filename);
}
public getMetaFor(tagName: string): MetaElement {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment