« get me outta code hell

validators: validateThing - 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>2024-03-04 18:11:01 -0400
committer(quasar) nebula <qznebula@protonmail.com>2024-05-01 07:06:05 -0300
commit262673f149f2b4ae8bc87d0e4fc507b7335609f4 (patch)
tree12d571912f7d19bdb12288425c065cde32dc83d1 /src
parentb34f4b28667881cd1a5ff191d82d177bdeb5bba9 (diff)
validators: validateThing
Diffstat (limited to 'src')
-rw-r--r--src/data/validators.js21
1 files changed, 20 insertions, 1 deletions
diff --git a/src/data/validators.js b/src/data/validators.js
index 976e6217..c0cec8a9 100644
--- a/src/data/validators.js
+++ b/src/data/validators.js
@@ -745,12 +745,31 @@ export function validateReferenceList(type = '') {
   return validateArrayItems(validateReference(type));
 }
 
+export function validateThing({
+  referenceType: expectedReferenceType = '',
+} = {}) {
+  return (thing) => {
+    isThing(thing);
+
+    if (expectedReferenceType) {
+      const {[Symbol.for('Thing.referenceType')]: referenceType} =
+        thing.constructor;
+
+      if (referenceType !== expectedReferenceType) {
+        throw new TypeError(`Expected only ${expectedReferenceType}, got other type: ${referenceType}`);
+      }
+    }
+
+    return true;
+  };
+}
+
 const validateWikiData_cache = {};
 
 export function validateWikiData({
   referenceType = '',
   allowMixedTypes = false,
-}) {
+} = {}) {
   if (referenceType && allowMixedTypes) {
     throw new TypeError(`Don't specify both referenceType and allowMixedTypes`);
   }