From 1864ecf0c1375b8115e4bc2bad8b1dc41150444c Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Wed, 3 Jan 2024 19:17:48 -0400 Subject: validators: validateProperties: clean up definition --- src/data/things/validators.js | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) (limited to 'src/data/things/validators.js') diff --git a/src/data/things/validators.js b/src/data/things/validators.js index 5cbe33de..2058fb91 100644 --- a/src/data/things/validators.js +++ b/src/data/things/validators.js @@ -354,6 +354,11 @@ export function isCommentary(commentaryText) { const isArtistRef = validateReference('artist'); export function validateProperties(spec) { + const { + [validateProperties.validateOtherKeys]: validateOtherKeys = null, + [validateProperties.allowOtherKeys]: allowOtherKeys = false, + } = spec; + const specEntries = Object.entries(spec); const specKeys = Object.keys(spec); @@ -364,7 +369,16 @@ export function validateProperties(spec) { throw new TypeError(`Expected an object, got array`); withAggregate({message: `Errors validating object properties`}, ({push}) => { - for (const [specKey, specValidator] of specEntries) { + const testEntries = specEntries.slice(); + + const unknownKeys = Object.keys(object).filter((key) => !specKeys.includes(key)); + if (validateOtherKeys) { + for (const key of unknownKeys) { + testEntries.push([key, validateOtherKeys]); + } + } + + for (const [specKey, specValidator] of testEntries) { const value = object[specKey]; try { specValidator(value); @@ -374,23 +388,9 @@ export function validateProperties(spec) { } } - const unknownKeys = Object.keys(object).filter((key) => !specKeys.includes(key)); - if (!empty(unknownKeys)) { - if (spec[validateProperties.validateOtherKeys]) { - const specValidator = spec[validateProperties.validateOtherKeys]; - for (const key of unknownKeys) { - const value = object[key]; - try { - specValidator(value); - } catch (error) { - error.message = `(key: ${colors.green(key)}, value: ${inspect(value)}) ${error.message}`; - push(error); - } - } - } else if (!spec[validateProperties.allowOtherKeys]) { - push(new Error( - `Unknown keys present (${unknownKeys.length}): [${unknownKeys.join(', ')}]`)); - } + if (!validateOtherKeys && !allowOtherKeys && !empty(unknownKeys)) { + push(new Error( + `Unknown keys present (${unknownKeys.length}): [${unknownKeys.join(', ')}]`)); } }); -- cgit 1.3.0-6-gf8a5