Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array validation with .error() #1940

Closed
kimini507 opened this issue Jun 27, 2019 · 1 comment
Closed

Array validation with .error() #1940

kimini507 opened this issue Jun 27, 2019 · 1 comment
Assignees
Labels
bug Bug or defect
Milestone

Comments

@kimini507
Copy link

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

@hueniverse
Copy link
Contributor

Note that error.type is not error.code.

@hueniverse hueniverse self-assigned this Jun 28, 2019
@hueniverse hueniverse added the bug Bug or defect label Jun 28, 2019
@hueniverse hueniverse added this to the 16.0.0 milestone Jun 28, 2019
@hueniverse hueniverse added the v16 label Aug 11, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Feb 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Bug or defect
Projects
None yet
Development

No branches or pull requests

2 participants