« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/thing/validators.js23
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;