Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joi - validate object keys and values?

Tags:

joi

how could I use Joi to validate a substitutions field has zero or more key /value pairs ? and that each key is a string and that each value is a string, number or bool ?

"substitutions": {
    "somekey": "someval",
    "somekey": "someval"
  }
like image 902
1977 Avatar asked Mar 14 '17 23:03

1977


1 Answers

You can use Joi.object().pattern():

{
    substitutions: Joi.object().pattern(/.*/, [Joi.string(), Joi.number(), Joi.boolean()])
}

This would work with payloads like:

{
    substitutions: {
        blah   : 'string',
        test123: 123,
        example: true,
    }
}
like image 150
blade Avatar answered Oct 16 '22 11:10

blade