Skip to content
Snippets Groups Projects
Commit d492a6b7 authored by David Sveningsson's avatar David Sveningsson
Browse files

Merge branch 'bugfix/issue-59-required-attr-empty' into 'master'

allow `requiredAttributes` and others to be empty array

Closes #59

See merge request !335
parents 672872a8 244d0384
No related branches found
No related tags found
Loading
Pipeline #102747134 passed
......@@ -21,17 +21,17 @@
"foreign": { "type": "boolean" },
"void": { "type": "boolean" },
"transparent": { "type": "boolean" },
"implicitClosed": { "type": "array", "contains": { "type": "string" } },
"implicitClosed": { "type": "array", "items": { "type": "string" } },
"scriptSupporting": { "type": "boolean" },
"form": { "type": "boolean" },
"deprecatedAttributes": {
"type": "array",
"contains": { "type": "string" }
"items": { "type": "string" }
},
"requiredAttributes": {
"type": "array",
"contains": { "type": "string" }
"items": { "type": "string" }
},
"attributes": { "$ref": "#/definitions/PermittedAttribute" },
......
/* 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 { Report, Reporter, Result } from "../reporter";
import { eventFormatter } from "./json";
......@@ -8,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 {
......@@ -100,6 +102,45 @@ function renameStdin(report: Report, filename: string): void {
}
}
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 {
console.error(chalk.red("Caught exception:"));
if (console.group) console.group();
{
console.error(err);
}
if (console.group) console.groupEnd();
}
function handleUnknownError(err: Error): void {
console.error(chalk.red("Caught exception:"));
if (console.group) console.group();
{
console.error(err);
}
if (console.group) console.groupEnd();
const bugUrl = `${pkg.bugs.url}?issuable_template=Bug`;
console.error(chalk.red(`This is a bug in ${pkg.name}-${pkg.version}.`));
console.error(
chalk.red(
[
`Please file a bug at ${bugUrl}`,
`and include this message in full and if possible the content of the`,
`file being parsed (or a reduced testcase).`,
].join("\n")
)
);
}
const argv: minimist.ParsedArgs = minimist(process.argv.slice(2), {
string: [
"c",
......@@ -263,20 +304,12 @@ try {
process.exit(0);
}
} catch (err) {
console.error(chalk.red("Caught exception:"));
if (console.group) console.group();
{
console.error(err);
}
if (console.group) console.groupEnd();
if (!(err instanceof UserError)) {
const bugUrl = `${pkg.bugs.url}?issuable_template=Bug`;
console.error(chalk.red(`This is a bug in ${pkg.name}-${pkg.version}.`));
console.error(
chalk.red(
`Please file a bug at ${bugUrl}\nand include this message in full and if possible the content of the\nfile being parsed (or a reduced testcase).`
)
);
if (err instanceof SchemaValidationError) {
handleValidationError(err);
} else if (err instanceof UserError) {
handleUserError(err);
} else {
handleUnknownError(err);
}
process.exit(1);
}
// 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 {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment