|
|
|
@ -1,5 +1,7 @@
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
const indentString = require("indent-string");
|
|
|
|
|
const supportsColor = require("supports-color");
|
|
|
|
|
const matchVirtualProperty = require("@validatem/match-virtual-property");
|
|
|
|
|
|
|
|
|
|
const AggregrateValidationError = require("./aggregrate-validation-error");
|
|
|
|
@ -7,38 +9,69 @@ const AggregrateValidationError = require("./aggregrate-validation-error");
|
|
|
|
|
// TODO: Omit the "At (root)" for path-less errors, to avoid confusion when singular values are being compared?
|
|
|
|
|
// TODO: Move out the path generating logic into a separate module, to better support custom error formatting code
|
|
|
|
|
|
|
|
|
|
module.exports = function aggregrateAndThrowErrors(errors) {
|
|
|
|
|
let rephrasedErrors = errors.map((error) => {
|
|
|
|
|
let stringifiedPathSegments = error.path.map((segment) => {
|
|
|
|
|
function joinPathSegments(segments) {
|
|
|
|
|
return (segments.length > 0)
|
|
|
|
|
? segments.join(" -> ")
|
|
|
|
|
: "(root)";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NOTE: We do some manual ANSI escape code stuff here for now, because using `chalk` would significantly inflate the bundle size of the core.
|
|
|
|
|
// TODO: Find a better solution for this.
|
|
|
|
|
let openHighlight, openDim, closeColor;
|
|
|
|
|
|
|
|
|
|
if (supportsColor.stderr) {
|
|
|
|
|
openHighlight = `\u001b[36m`; // cyan
|
|
|
|
|
openDim = `\u001b[90m`; // gray
|
|
|
|
|
closeColor = `\u001b[39m`;
|
|
|
|
|
} else {
|
|
|
|
|
openHighlight = "";
|
|
|
|
|
openDim = "";
|
|
|
|
|
closeColor = "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function renderErrorList(errors, basePath = []) {
|
|
|
|
|
let rephrasedErrors = errors.map((error, i) => {
|
|
|
|
|
let pathSegments = error.path.map((segment) => {
|
|
|
|
|
if (segment == null) {
|
|
|
|
|
throw new Error(`Unexpected empty path segment encountered; this is a bug, please report it!`);
|
|
|
|
|
} else if (typeof segment === "string") {
|
|
|
|
|
return segment;
|
|
|
|
|
} else if (typeof segment === "number") {
|
|
|
|
|
return String(segment);
|
|
|
|
|
} else if (typeof segment === "string" || typeof segment === "number") {
|
|
|
|
|
return openHighlight + String(segment) + closeColor;
|
|
|
|
|
} else if (matchVirtualProperty(segment)) {
|
|
|
|
|
return `(${segment.name})`;
|
|
|
|
|
return openDim + `(${segment.name})` + closeColor;
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(`Unexpected path segment encountered: ${segment}; this is a bug, please report it!`);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/* TODO: Make immutable */
|
|
|
|
|
let path = (stringifiedPathSegments.length > 0)
|
|
|
|
|
? stringifiedPathSegments.join(" -> ")
|
|
|
|
|
: "(root)";
|
|
|
|
|
let lineCharacter = (i < errors.length - 1)
|
|
|
|
|
? "├─"
|
|
|
|
|
: "└─";
|
|
|
|
|
|
|
|
|
|
error.message = `At ${path}: ${error.message}`;
|
|
|
|
|
return error;
|
|
|
|
|
let mainLine = (basePath.length > 0)
|
|
|
|
|
// ? `... -> ${joinPathSegments(pathSegments)}: ${error.message}`
|
|
|
|
|
? ` ${lineCharacter} ${joinPathSegments(pathSegments)}: ${error.message}`
|
|
|
|
|
: ` - At ${joinPathSegments(pathSegments)}: ${error.message}`;
|
|
|
|
|
|
|
|
|
|
if (error.subErrors != null && error.subErrors.length > 0) {
|
|
|
|
|
let renderedSubErrors = renderErrorList(error.subErrors, error.path);
|
|
|
|
|
|
|
|
|
|
return mainLine + "\n" + indentString(renderedSubErrors, 2);
|
|
|
|
|
} else {
|
|
|
|
|
return mainLine;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let detailLines = rephrasedErrors.map((error) => {
|
|
|
|
|
return ` - ${error.message}`;
|
|
|
|
|
return rephrasedErrors.map((error) => {
|
|
|
|
|
return `${error}`;
|
|
|
|
|
}).join("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = function aggregrateAndThrowErrors(errors) {
|
|
|
|
|
let detailLines = renderErrorList(errors);
|
|
|
|
|
|
|
|
|
|
if (errors.length > 0) {
|
|
|
|
|
return new AggregrateValidationError(`One or more validation errors occurred:\n${detailLines}`, {
|
|
|
|
|
errors: rephrasedErrors
|
|
|
|
|
errors: errors
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|