Commit 23a50f33 authored by David Sveningsson's avatar David Sveningsson
Browse files

fix(error): present original json for configuration errors

parent 0d59b366
Pipeline #280851561 passed with stages
in 10 minutes and 27 seconds
{
"foo": [1, 2, 3],
"bar": "baz"
}
import stripAnsi = require("strip-ansi");
import fs from "fs";
import path from "path";
import Ajv, { SchemaObject } from "ajv";
import stripAnsi from "strip-ansi";
import { MetaElement } from "../meta/element";
import { MetaTable } from "../meta/table";
import { SchemaValidationError } from "./schema-validation-error";
......@@ -22,3 +25,83 @@ it("SchemaValidationError should pretty-print validation errors", () => {
}
}
});
describe("prettyError()", () => {
const ajv = new Ajv({ strict: true, strictTuples: true, strictTypes: true });
const schema: SchemaObject = {
type: "string",
};
const validator = ajv.compile(schema);
const filename = path.join(__dirname, "__fixtures__", "invalid.json");
const data = JSON.parse(fs.readFileSync(filename, "utf-8"));
validator(data);
const errors = validator.errors ?? [];
it("should contain original formatting", () => {
expect.assertions(1);
const error = new SchemaValidationError(filename, "Mock error", data, schema, errors);
expect(stripAnsi(error.prettyError())).toMatchInlineSnapshot(`
"TYPE should be string
> 1 | {
| ^
> 2 | \\"foo\\": [1, 2, 3],
| ^^^^^^^^^^^^^^^^^^^
> 3 | \\"bar\\": \\"baz\\"
| ^^^^^^^^^^^^^^^^^^^
> 4 | }
| ^^ 👈🏽 type should be string
5 |"
`);
});
it("should handle invalid file", () => {
expect.assertions(1);
const error = new SchemaValidationError("invalid-file", "Mock error", data, schema, errors);
expect(stripAnsi(error.prettyError())).toMatchInlineSnapshot(`
"TYPE should be string
> 1 | {
| ^
> 2 | \\"foo\\": [
| ^^^^^^^^^^
> 3 | 1,
| ^^^^^^^^^^
> 4 | 2,
| ^^^^^^^^^^
> 5 | 3
| ^^^^^^^^^^
> 6 | ],
| ^^^^^^^^^^
> 7 | \\"bar\\": \\"baz\\"
| ^^^^^^^^^^
> 8 | }
| ^^ 👈🏽 type should be string"
`);
});
it("should handle missing filename", () => {
expect.assertions(1);
const error = new SchemaValidationError(null, "Mock error", data, schema, errors);
expect(stripAnsi(error.prettyError())).toMatchInlineSnapshot(`
"TYPE should be string
> 1 | {
| ^
> 2 | \\"foo\\": [
| ^^^^^^^^^^
> 3 | 1,
| ^^^^^^^^^^
> 4 | 2,
| ^^^^^^^^^^
> 5 | 3
| ^^^^^^^^^^
> 6 | ],
| ^^^^^^^^^^
> 7 | \\"bar\\": \\"baz\\"
| ^^^^^^^^^^
> 8 | }
| ^^ 👈🏽 type should be string"
`);
});
});
import fs from "fs";
import betterAjvErrors from "@sidvind/better-ajv-errors";
import { ErrorObject, SchemaObject } from "ajv";
import { UserError } from "./user-error";
......@@ -33,9 +34,19 @@ export class SchemaValidationError extends UserError {
}
public prettyError(): string {
const json = this.getRawJSON();
return betterAjvErrors(this.schema, this.obj, this.errors, {
format: "cli",
indent: 2,
json,
});
}
private getRawJSON(): string | null {
if (this.filename && fs.existsSync(this.filename)) {
return fs.readFileSync(this.filename, "utf-8");
} else {
return null;
}
}
}
Supports Markdown
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