4

What is the best way to validate dictionary containing custom keys with Joi?

var objToValidate = {
  "a": { name: 1 }
  "type": { name: 3 },
  ... // many other properties
}

var schema = ?
SashaPinsk
  • 162
  • 10
  • 1
    Possible duplicate of [Joi object validation: How to validate values with unknown key names?](http://stackoverflow.com/questions/41741612/joi-object-validation-how-to-validate-values-with-unknown-key-names) – Ankh Mar 30 '17 at 08:26
  • @Ankh Yes, I used answer from there. `campaign: Joi.object().pattern(/^/, Joi.date().iso())` – SashaPinsk Sep 26 '17 at 10:17

1 Answers1

-1

you can use the keys validation:

const object = Joi.object().keys({
    a: Joi.number().min(1).max(10).integer(),
    b: 'some string'
});

object.validate({ a: 5 , b : "word"}, (err, value) => { });
Akram Saouri
  • 1,179
  • 8
  • 15
  • 1
    Probably, I wasn't clear enough. If we know all the keys in advance, the task is easy. But the question about validation of dictionary with custom keys (ie UUID). – SashaPinsk Jan 13 '17 at 10:23
  • 1
    @SashaPinsk check out this answer: http://stackoverflow.com/questions/41741612/joi-object-validation-how-to-validate-values-with-unknown-key-names it amounts to using `joi.object().pattern()` – Cuthbert Jan 20 '17 at 14:14