From eba89ed9c5909d5fd5ada625587ba98d340e3399 Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Wed, 1 Jul 2020 11:22:51 +0200 Subject: [PATCH] Improve subError rendering --- src/aggregrate-errors.js | 14 ++++++++++++-- yarn.lock | 7 +++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/aggregrate-errors.js b/src/aggregrate-errors.js index 0740dac..0f5011a 100644 --- a/src/aggregrate-errors.js +++ b/src/aggregrate-errors.js @@ -4,6 +4,7 @@ const indentString = require("indent-string"); const supportsColor = require("supports-color"); const matchVirtualProperty = require("@validatem/match-virtual-property"); const asExpression = require("as-expression"); +const syncpipe = require("syncpipe"); const AggregrateValidationError = require("./aggregrate-validation-error"); @@ -54,7 +55,7 @@ function renderErrorList(errors, subErrorLevels = 0) { ? `${lineCharacter} ${joinPathSegments(pathSegments)}: ${error.message}` : `${lineCharacter} ${error.message}`; - return indentString(message, subErrorLevels * 4); + return message; } else { return (pathSegments.length > 0) ? ` - At ${joinPathSegments(pathSegments)}: ${error.message}` @@ -64,8 +65,17 @@ function renderErrorList(errors, subErrorLevels = 0) { if (error.subErrors != null && error.subErrors.length > 0) { let renderedSubErrors = renderErrorList(error.subErrors, subErrorLevels + 1); + let isLastError = (i === errors.length - 1); - return mainLine + "\n" + renderedSubErrors; + if (subErrorLevels > 0 && !isLastError) { + return syncpipe(renderedSubErrors, [ + (_) => indentString(_, 3), + (_) => indentString(_, 1, { indent: "│" }), + (_) => mainLine + "\n" + _ + ]); + } else { + return mainLine + "\n" + indentString(renderedSubErrors, 4); + } } else { return mainLine; } diff --git a/yarn.lock b/yarn.lock index 4e3cbe0..e8b0161 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1148,6 +1148,13 @@ supports-color@^7.1.0: dependencies: has-flag "^4.0.0" +syncpipe@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/syncpipe/-/syncpipe-1.0.0.tgz#170340f813150bc8fcb8878b1b9c71ea0ccd3727" + integrity sha512-cdiAFTnFJRvUaNPDc2n9CqoFvtIL3+JUMJZrC3kA3FzpugHOqu0TvkgNwmnxPZ5/WjAzMcfMS3xm+AO7rg/j/w== + dependencies: + assure-array "^1.0.0" + table@^5.2.3: version "5.4.6" resolved "https://registry.yarnpkg.com/table/-/table-5.4.6.tgz#1292d19500ce3f86053b05f0e8e7e4a3bb21079e"