« get me outta code hell

validators: validateReference (etc): multiple reference types - 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-11-14 20:04:52 -0400
committer(quasar) nebula <qznebula@protonmail.com>2024-11-15 20:40:14 -0400
commit4f6e0bc351831abedca9dcb9667987b2907e0a61 (patch)
tree3d27e6be9ea421ca2ff07a33ee198851e8c4b5f5 /src/data
parent2d5f323bb4da3cdedc5814491f841e4154478f69 (diff)
validators: validateReference (etc): multiple reference types
Diffstat (limited to 'src/data')
-rw-r--r--src/data/validators.js21
1 files changed, 15 insertions, 6 deletions
diff --git a/src/data/validators.js b/src/data/validators.js
index f886c4f2..2c99be0c 100644
--- a/src/data/validators.js
+++ b/src/data/validators.js
@@ -790,7 +790,7 @@ export function isURL(string) {
   return true;
 }
 
-export function validateReference(type = 'track') {
+export function validateReference(type) {
   return (ref) => {
     isStringNonEmpty(ref);
 
@@ -803,8 +803,17 @@ export function validateReference(type = 'track') {
     const {groups: {typePart, directoryPart}} = match;
 
     if (typePart) {
-      if (typePart !== type)
-        throw new TypeError(`Expected ref to begin with "${type}:", got "${typePart}:"`);
+      if (Array.isArray(type)) {
+        if (!typePart.includes(type)) {
+          throw new TypeError(
+            `Expected ref to begin with one of ` +
+            type.map(type => `"${type}:"`).join(', ') +
+            `, got "${typePart}:"`);
+        }
+      } else if (typePart !== type) {
+        throw new TypeError(
+          `Expected ref to begin with "${type}:", got "${typePart}:"`);
+      }
 
       isDirectory(directoryPart);
     }
@@ -815,18 +824,18 @@ export function validateReference(type = 'track') {
   };
 }
 
-export function validateReferenceList(type = '') {
+export function validateReferenceList(type) {
   return validateArrayItems(validateReference(type));
 }
 
-export function validateAnnotatedReference(type = '') {
+export function validateAnnotatedReference(type) {
   return validateProperties({
     reference: validateReference(type),
     annotation: optional(isContentString),
   });
 }
 
-export function validateAnnotatedReferenceList(type = '') {
+export function validateAnnotatedReferenceList(type) {
   return validateArrayItems(validateAnnotatedReference(type));
 }