« get me outta code hell

validators: validateAllPropertyValues - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-12-30 10:21:16 -0400
committer(quasar) nebula <qznebula@protonmail.com>2023-12-30 16:26:27 -0400
commitdc29162482b70a5ed9fe3c889a8fc29d001c8e80 (patch)
treefe9d227afca1849fcba02f5f020f309c15b107a4 /src
parent8fad4a45eed30b5bc220fb875d12c69b42f9e93b (diff)
validators: validateAllPropertyValues
Diffstat (limited to 'src')
-rw-r--r--src/data/things/validators.js25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/data/things/validators.js b/src/data/things/validators.js
index 0cc20229..27416bb2 100644
--- a/src/data/things/validators.js
+++ b/src/data/things/validators.js
@@ -378,6 +378,31 @@ export function validateProperties(spec) {
   };
 }
 
+export function validateAllPropertyValues(validator) {
+  return (object) => {
+    isObject(object);
+
+    if (Array.isArray(object))
+      throw new TypeError(`Expected an object, got array`);
+
+    withAggregate({message: `Errors validating object properties`}, ({call}) => {
+      for (const key of Reflect.ownKeys(object)) {
+        call(() => {
+          const value = object[key];
+          try {
+            validator(value);
+          } catch (error) {
+            error.message = `(key: ${colors.green(key)}, value: ${inspect(value)}) ${error.message}`;
+            throw error;
+          }
+        });
+      }
+    });
+
+    return true;
+  };
+}
+
 export const isContribution = validateProperties({
   who: isArtistRef,
   what: optional(isStringNonEmpty),