joi

Joi object validation: How to validate values with unknown key names?

混江龙づ霸主 提交于 2019-11-27 06:40:58
问题 I have an object with key names I cannot possibly know - they are created by user. However I do know what values they (keys) are going to store, and they (values) are going to be ISO strings. How do I validate those values? And, optionally , how do I validate uknown object's keys, i.e.: key: Joi.string().min(2).max(25) What I have already tried was based on Joi API docs : Another benefits of using Joi.object([schema]) instead of a plain JS object is >that you can set any options on the object

Is there a way to validate dynamic key names in a Joi schema?

陌路散爱 提交于 2019-11-26 21:53:35
问题 Is there a way I can validate a value like this with Joi so that I can verify it is an object with zero or more keys (of any name) and that each have values of either a string, number or boolean? { dynamicallyNamedKey1: 'some value', dynamicallyNamedKey2: 4 } 回答1: You're going to want to use Joi 's object().pattern() method. It's specifically for validating objects with unknown keys. To match against one or more datatypes on a single key you'll need alternatives().try() (or simply pass an