Skip to content

Validate object keys related to another value #1761

@jinliming2

Description

@jinliming2

Assume I have objects like this:

{
  a: 'key',
  b: 'value',
  data: [
    { key: 'keyA', value: 1 },
    { key: 'keyB', value: 2 }
  ]
}
{
  a: 'anotherKey',
  b: 'anotherValue',
  data: [
    { anotherKey: 'keyA', anotherValue: 1 },
    { anotherKey: 'keyB', anotherValue: 2 }
  ]
}

Keys of object in array data must specify by key a and b.
I think it would be:

Joi.object({
  a: Joi.string().required(),
  b: Joi.string().required(),
  data: Joi.array()
    .items(
      Joi.object()
        .pattern(Joi.ref('a'), Joi.string().required())
        .pattern(Joi.ref('b'), Joi.number().required())
    )
    .required(),
})

Seems like #1382, but different.

Metadata

Metadata

Assignees

Labels

bugBug or defect

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions