« get me outta code hell

validators: validateProperties: clean up definition - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-01-03 19:17:48 -0400
committer(quasar) nebula <qznebula@protonmail.com>2024-01-03 19:28:53 -0400
commit1864ecf0c1375b8115e4bc2bad8b1dc41150444c (patch)
tree8fe1f0ef1218560f5c5743c3f6e8acbc45001b27 /src/data
parent18dd81903cebb8cbd97c6b6e530bd8e517477da1 (diff)
validators: validateProperties: clean up definition
Diffstat (limited to 'src/data')
-rw-r--r--src/data/things/validators.js36
1 files changed, 18 insertions, 18 deletions
diff --git a/src/data/things/validators.js b/src/data/things/validators.js
index 5cbe33de..2058fb91 100644
--- a/src/data/things/validators.js
+++ b/src/data/things/validators.js
@@ -354,6 +354,11 @@ export function isCommentary(commentaryText) {
 const isArtistRef = validateReference('artist');
 
 export function validateProperties(spec) {
+  const {
+    [validateProperties.validateOtherKeys]: validateOtherKeys = null,
+    [validateProperties.allowOtherKeys]: allowOtherKeys = false,
+  } = spec;
+
   const specEntries = Object.entries(spec);
   const specKeys = Object.keys(spec);
 
@@ -364,7 +369,16 @@ export function validateProperties(spec) {
       throw new TypeError(`Expected an object, got array`);
 
     withAggregate({message: `Errors validating object properties`}, ({push}) => {
-      for (const [specKey, specValidator] of specEntries) {
+      const testEntries = specEntries.slice();
+
+      const unknownKeys = Object.keys(object).filter((key) => !specKeys.includes(key));
+      if (validateOtherKeys) {
+        for (const key of unknownKeys) {
+          testEntries.push([key, validateOtherKeys]);
+        }
+      }
+
+      for (const [specKey, specValidator] of testEntries) {
         const value = object[specKey];
         try {
           specValidator(value);
@@ -374,23 +388,9 @@ export function validateProperties(spec) {
         }
       }
 
-      const unknownKeys = Object.keys(object).filter((key) => !specKeys.includes(key));
-      if (!empty(unknownKeys)) {
-        if (spec[validateProperties.validateOtherKeys]) {
-          const specValidator = spec[validateProperties.validateOtherKeys];
-          for (const key of unknownKeys) {
-            const value = object[key];
-            try {
-              specValidator(value);
-            } catch (error) {
-              error.message = `(key: ${colors.green(key)}, value: ${inspect(value)}) ${error.message}`;
-              push(error);
-            }
-          }
-        } else if (!spec[validateProperties.allowOtherKeys]) {
-          push(new Error(
-            `Unknown keys present (${unknownKeys.length}): [${unknownKeys.join(', ')}]`));
-        }
+      if (!validateOtherKeys && !allowOtherKeys && !empty(unknownKeys)) {
+        push(new Error(
+          `Unknown keys present (${unknownKeys.length}): [${unknownKeys.join(', ')}]`));
       }
     });