« get me outta code hell

validators: oneOf: reflect combined checks on errors - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-12-31 23:36:50 -0400
committer(quasar) nebula <qznebula@protonmail.com>2023-12-31 23:36:50 -0400
commit3f922377726bf3886ccb815f27c1061496e79729 (patch)
tree165744d6385a6270a24d8966b0e5af5f4c4f91e8
parent2c1cfcec5ec7d35b4a1aaecbc10535ab39a80972 (diff)
validators: oneOf: reflect combined checks on errors
-rw-r--r--src/data/things/validators.js19
1 files changed, 15 insertions, 4 deletions
diff --git a/src/data/things/validators.js b/src/data/things/validators.js
index f240c6ba..75358833 100644
--- a/src/data/things/validators.js
+++ b/src/data/things/validators.js
@@ -651,6 +651,10 @@ export function oneOf(...validators) {
   const validConstructors = new Set();
   const validTypes = new Set();
 
+  const constantValidators = [];
+  const constructorValidators = [];
+  const typeValidators = [];
+
   const leftoverValidators = [];
 
   for (const validator of validators) {
@@ -662,14 +666,18 @@ export function oneOf(...validators) {
         for (const value of creatorMeta.values) {
           validConstants.add(value);
         }
+
+        constantValidators.push(validator);
         break;
 
       case validateInstanceOf:
         validConstructors.add(creatorMeta.constructor);
+        constructorValidators.push(validator);
         break;
 
       case validateType:
         validTypes.add(creatorMeta.type);
+        typeValidators.push(validator);
         break;
 
       default:
@@ -725,7 +733,7 @@ export function oneOf(...validators) {
       const gotPart = `, got ${value}`;
 
       prefaceErrorInfo.push([
-        null,
+        constantValidators,
         offset++,
         new TypeError(
           `Expected one of ${constants.join(' ')}` + gotPart),
@@ -740,7 +748,7 @@ export function oneOf(...validators) {
       const gotPart = `, got ${gotType}`;
 
       prefaceErrorInfo.push([
-        null,
+        typeValidators,
         offset++,
         new TypeError(
           `Expected one of ${types.join(', ')}` + gotPart),
@@ -756,7 +764,7 @@ export function oneOf(...validators) {
       const gotPart = (gotName ? `, got ${gotName}` : ``);
 
       prefaceErrorInfo.push([
-        null,
+        constructorValidators,
         offset++,
         new TypeError(
           `Expected one of ${names.join(', ')}` + gotPart),
@@ -773,7 +781,10 @@ export function oneOf(...validators) {
           ? `(#${i + 1} "${validator.name}") ${error.message}`
           : `(#${i + 1}) ${error.message}`);
 
-      error.check = validator;
+      error.check =
+        (Array.isArray(validator) && validator.length === 1
+          ? validator[0]
+          : validator);
 
       errors.push(error);
     }