From 262673f149f2b4ae8bc87d0e4fc507b7335609f4 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Mon, 4 Mar 2024 18:11:01 -0400 Subject: validators: validateThing --- src/data/validators.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'src') 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`); } -- cgit 1.3.0-6-gf8a5