diff options
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 |
commit | 262673f149f2b4ae8bc87d0e4fc507b7335609f4 (patch) | |
tree | 12d571912f7d19bdb12288425c065cde32dc83d1 /src | |
parent | b34f4b28667881cd1a5ff191d82d177bdeb5bba9 (diff) |
validators: validateThing
Diffstat (limited to 'src')
-rw-r--r-- | src/data/validators.js | 21 |
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`); } |