diff options
author | (quasar) nebula <qznebula@protonmail.com> | 2022-01-19 23:22:12 -0400 |
---|---|---|
committer | (quasar) nebula <qznebula@protonmail.com> | 2022-01-19 23:22:12 -0400 |
commit | ca3a698e06bd44346c96f512aebd7fae84d8500b (patch) | |
tree | 902be57a7168fd876ce519e60d7b90052155e036 /src | |
parent | cb481dd53f3793c3c12c8e1a41875f024c9cbe11 (diff) |
show item info in errs caught by validateListItems
Diffstat (limited to 'src')
-rw-r--r-- | src/thing/validators.js | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/src/thing/validators.js b/src/thing/validators.js index 05736914..2bdb2995 100644 --- a/src/thing/validators.js +++ b/src/thing/validators.js @@ -1,5 +1,13 @@ import { withAggregate } from '../util/sugar.js'; +import { color, ENABLE_COLOR } from '../util/cli.js'; + +import { inspect as nodeInspect } from 'util'; + +function inspect(value) { + return nodeInspect(value, {colors: ENABLE_COLOR}); +} + // Basic types (primitives) function a(noun) { @@ -97,12 +105,25 @@ export function isArray(value) { return true; } +function validateArrayItemsHelper(itemValidator) { + return (item, index) => { + try { + itemValidator(item); + } catch (error) { + error.message = `(index: ${color.green(index)}, item: ${inspect(item)}) ${error.message}`; + throw error; + } + }; +} + export function validateArrayItems(itemValidator) { + const fn = validateArrayItemsHelper(itemValidator); + return array => { isArray(array); withAggregate({message: 'Errors validating array items'}, ({ wrap }) => { - array.forEach(wrap(itemValidator)); + array.forEach(wrap(fn)); }); return true; |