joi icon indicating copy to clipboard operation
joi copied to clipboard

Question on relationship between child/parent properties.

Open mradamh opened this issue 11 years ago • 2 comments

I'm probably missing something very obvious, but I'm trying to validate a structure that looks roughly like this:

{
    ...
    type: Joi.string().valid(['FOO', 'BAR', 'BAZ', 'QUX']).required(),
    source: Joi.object().keys({
        uri: Joi.string(),
        search: Joi.string(),
        username: Joi.string(),
        password: Joi.string()
    })
    .and('username', 'password')
    .when('type', { is: 'QUX', then: Joi.forbidden(), otherwise: Joi.required() })
    ...
}

There are relationships that I haven't been able to figure out how to describe between the source object and type:

  1. If type is FOO, then uri is required and search is forbidden
  2. If type is BAR or BAZ, then search is required and uri is forbidden.

Pointing me in the right direction on above would be greatly appreciated. I've been turning in circles and can't get it to work quite right.

mradamh avatar Aug 10 '15 15:08 mradamh

See the docs about refs for the parent/child explanation.

Until I can get the when to behave better, I think you're better off with multiple alternatives, that's not very DRY but that'll get the work done until I can find time to figure out better ways.

Marsup avatar Aug 10 '15 22:08 Marsup

thanks. i was able to get it to work with multiple alternatives.

mradamh avatar Aug 12 '15 15:08 mradamh