Commit 6ecf0501 authored by David Sveningsson's avatar David Sveningsson
Browse files

fix(meta): throw schema validation error when element metadata does not validate

fixes #81
parent 43fe81ec
Pipeline #139792925 passed with stages
in 9 minutes and 54 seconds
......@@ -35,7 +35,7 @@ jest.mock(
);
import { Config } from "../config";
import { UserError } from "../error/user-error";
import { UserError, SchemaValidationError } from "../error";
import { Parser } from "../parser";
import { MetaDataTable } from "./element";
import { MetaData, MetaTable } from ".";
......@@ -52,7 +52,7 @@ describe("MetaTable", () => {
validate.errors = [];
});
it("should throw error if data does not validate", () => {
it("should throw SchemaValidationError if object does not validate", () => {
expect.assertions(2);
validate.errors = [
{
......@@ -68,13 +68,35 @@ describe("MetaTable", () => {
table.loadFromObject({
foo: mockEntry({ invalid: true }),
});
expect(fn).toThrow(UserError);
expect(fn).toThrow(SchemaValidationError);
expect(fn).toThrow(
"Element metadata is not valid: /foo Property invalid is not expected to be here"
);
});
it("should throw user-error if file is not properly formatted json", () => {
it("should throw SchemaValidationError if file does not validate", () => {
expect.assertions(2);
const filename = path.resolve(
__dirname,
"../../test-files/meta/invalid-schema.json"
);
const table = new MetaTable();
validate.errors = [
{
keyword: "additionalProperties",
dataPath: "/foo",
schemaPath: "#/patternProperties/%5E.*%24/additionalProperties",
params: { additionalProperty: "invalid" },
message: "should NOT have additional properties",
},
];
expect(() => table.loadFromFile(filename)).toThrow(SchemaValidationError);
expect(() => table.loadFromFile(filename)).toThrow(
"Element metadata is not valid: /foo Property invalid is not expected to be here"
);
});
it("should throw UserError if file is not properly formatted json", () => {
expect.assertions(2);
const table = new MetaTable();
expect(() => table.loadFromFile("invalid-file.json")).toThrow(UserError);
......
......@@ -106,6 +106,9 @@ export class MetaTable {
/**
* Load metadata table from object.
*
* @param obj - Object with metadata to load
* @param filename - Optional filename used when presenting validation error
*/
public loadFromObject(
obj: MetaDataTable,
......@@ -134,6 +137,8 @@ export class MetaTable {
/**
* Load metadata table from filename
*
* @param filename - Filename to load
*/
public loadFromFile(filename: string): void {
try {
......@@ -146,6 +151,9 @@ export class MetaTable {
const data = require(filename); // eslint-disable-line import/no-dynamic-require
this.loadFromObject(data, filename);
} catch (err) {
if (err instanceof SchemaValidationError) {
throw err;
}
throw new UserError(
`Failed to load element metadata from "${filename}"`,
err
......
{
"foo": {
"invalid": true
}
}
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