Skip to content

Array validation with .error() #1940

@kimini507

Description

@kimini507

Context

  • node version: v10.13.0
  • joi version: 16.0.0-rc1
  • environment (node, browser): node
  • used with (hapi, standalone, ...): standalone
  • any other relevant information:

What are you trying to achieve or the steps to reproduce ?

const myValidator = Joi.string().trim().regex(/someregex/)
  .error(errors => {
    let error = errors[0];
    if (error.type === 'string.regex.base') {
      error.message = 'my new error message'
    }

    return errors;
  })

let { error, value } = Joi.validate(input, {
  field1: Joi.array().items(myValidator.min(2).max(64)),
  field2: myValidator
})

Which result you had ?

When there is an error in field2 (not array), I am getting what I expected.

But when there is an error inside the array (field1), error is null while value will look like:

{
  field1: [
    // instance of Report with properties such as isJoi, context, message, etc
    isJoi: {...}
  ],
  field2: 'original value'
}

What did you expect ?

let { error, value } = Joi.validate(input, {
  field1: Joi.array().items(myValidator.min(2).max(64)),
  field2: myValidator
})

I expect error to be the error message

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