diff options
author | (quasar) nebula <qznebula@protonmail.com> | 2023-12-30 10:21:16 -0400 |
---|---|---|
committer | (quasar) nebula <qznebula@protonmail.com> | 2023-12-30 16:26:27 -0400 |
commit | dc29162482b70a5ed9fe3c889a8fc29d001c8e80 (patch) | |
tree | fe9d227afca1849fcba02f5f020f309c15b107a4 | |
parent | 8fad4a45eed30b5bc220fb875d12c69b42f9e93b (diff) |
validators: validateAllPropertyValues
-rw-r--r-- | src/data/things/validators.js | 25 |
1 files changed, 25 insertions, 0 deletions
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), |