diff options
author | (quasar) nebula <qznebula@protonmail.com> | 2023-11-04 21:01:23 -0300 |
---|---|---|
committer | (quasar) nebula <qznebula@protonmail.com> | 2023-11-04 21:01:23 -0300 |
commit | 9f4c3b913fa6b12a236cedf76abe120f0321f53e (patch) | |
tree | ce93ffd30c6f5239ef8965b7ff68883c4a0b2a38 /src | |
parent | c4eba52cd5023e3b503937b47e2bc6f77527d5c3 (diff) |
data: validateWikiData: early exit for mixed items
Diffstat (limited to 'src')
-rw-r--r-- | src/data/things/validators.js | 34 |
1 files changed, 27 insertions, 7 deletions
diff --git a/src/data/things/validators.js b/src/data/things/validators.js index ee301f15..ea4303fc 100644 --- a/src/data/things/validators.js +++ b/src/data/things/validators.js @@ -433,18 +433,38 @@ export function validateWikiData({ OK = true; return true; } - const allRefTypes = - new Set(array.map(object => - object.constructor[Symbol.for('Thing.referenceType')])); + const allRefTypes = new Set(); - if (allRefTypes.has(undefined)) { - if (allRefTypes.size === 1) { - throw new TypeError(`Expected array of wiki data objects, got array of other objects`); + let foundThing = false; + let foundOtherObject = false; + + for (const object of array) { + const {[Symbol.for('Thing.referenceType')]: referenceType} = object.constructor; + + if (referenceType === undefined) { + foundOtherObject = true; + + // Early-exit if a Thing has been found - nothing more can be learned. + if (foundThing) { + throw new TypeError(`Expected array of wiki data objects, got mixed items`); + } } else { - throw new TypeError(`Expected array of wiki data objects, got mixed items`); + foundThing = true; + + // Early-exit if a non-Thing object has been found - nothing more can + // be learned. + if (foundOtherObject) { + throw new TypeError(`Expected array of wiki data objects, got mixed items`); + } + + allRefTypes.add(referenceType); } } + if (foundOtherObject && !foundThing) { + throw new TypeError(`Expected array of wiki data objects, got array of other objects`); + } + if (allRefTypes.size > 1) { if (allowMixedTypes) { OK = true; return true; |