From dc29162482b70a5ed9fe3c889a8fc29d001c8e80 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 30 Dec 2023 10:21:16 -0400 Subject: validators: validateAllPropertyValues --- src/data/things/validators.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'src/data') diff --git a/src/data/things/validators.js b/src/data/things/validators.js index 0cc20229..27416bb2 100644 --- a/src/data/things/validators.js +++ b/src/data/things/validators.js @@ -378,6 +378,31 @@ export function validateProperties(spec) { }; } +export function validateAllPropertyValues(validator) { + return (object) => { + isObject(object); + + if (Array.isArray(object)) + throw new TypeError(`Expected an object, got array`); + + withAggregate({message: `Errors validating object properties`}, ({call}) => { + for (const key of Reflect.ownKeys(object)) { + call(() => { + const value = object[key]; + try { + validator(value); + } catch (error) { + error.message = `(key: ${colors.green(key)}, value: ${inspect(value)}) ${error.message}`; + throw error; + } + }); + } + }); + + return true; + }; +} + export const isContribution = validateProperties({ who: isArtistRef, what: optional(isStringNonEmpty), -- cgit 1.3.0-6-gf8a5