From ca3a698e06bd44346c96f512aebd7fae84d8500b Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Wed, 19 Jan 2022 23:22:12 -0400 Subject: show item info in errs caught by validateListItems --- src/thing/validators.js | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'src/thing') 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; -- cgit 1.3.0-6-gf8a5