Skip to content

Specify required matches for .pattern() #1457

@barbaracassani

Description

@barbaracassani

Context

  • node version: v8.9.3
  • joi version: 10.6.0
  • environment : node
  • used with : hapi

I am trying to validate a dynamic key that needs to be required:

validate: {
   payload: Joi.object()
         .pattern(/^id_[\s\S]*$/i, Joi.string().required())
         .keys({
             state: Joi.string().optional(),
             presentation: Joi.string().optional(),
  // etc

I would expect -- or at least this is what I am trying to express -- that at least one key starting with id would be required. Unfortunately, whilst other forms of validation work alongside pattern (i.e. Joi.number()), required is ignored and therefore the validation will always pass even if a key with that pattern is missing from the payload.

Metadata

Metadata

Assignees

Labels

featureNew functionality or improvement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions