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

alternatives doesn't work with error #1119

Closed
machineghost opened this issue Mar 7, 2017 · 1 comment
Closed

alternatives doesn't work with error #1119

machineghost opened this issue Mar 7, 2017 · 1 comment
Labels
bug Bug or defect
Milestone

Comments

@machineghost
Copy link

machineghost commented Mar 7, 2017

Context

  • node version: 6.9.4
  • joi version: 10.0.6
  • environment (node, browser): node
  • used with (hapi, standalone, ...): standalone
  • any other relevant information: no

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

Pretty straightforward, take a thing or an array of that thing, with a sane failure message:

Joi.alternatives()
  .try(notificationsArray, notification)
  .error(new Error('Either a valid notification or non-empty notification array is required')),

The actual definition of notification and notificationArray aren't important, but they are something to the effect of:

const notification = Joi.object({
    a: Joi.string(),
    b: Joi.number(),
    c: Joi.string().optional(),
  });
const notificationsArray = Joi.array().items(notification);

Then just validate with a garbage value like ''.

What was your result?

"error": [
    {
        "message": "\"value\" must be an array",
        "path": "value",
        "type": "array.base",
        "context": {
            "key": "value"
        }
    },
    {
        "message": "\"notifications\" is not allowed",
        "path": "notifications",
        "type": "object.allowUnknown",
        "context": {
            "child": "notifications",
            "key": "notifications"
        }
    }
]

What did you expect ?

"error": "Either a valid notification or non-empty notification array is required"

This is especially relevant for alternatives because they produce such ... unfriendly error output that you really need to override it, if you want your API consumers to have any idea what they did wrong.

@Marsup Marsup added the bug Bug or defect label Mar 7, 2017
@Marsup Marsup self-assigned this Apr 2, 2017
@Marsup Marsup closed this as completed in f8e5435 Apr 2, 2017
@Marsup Marsup added this to the 10.4.1 milestone Apr 2, 2017
Marsup added a commit that referenced this issue Apr 2, 2017
@machineghost
Copy link
Author

Thanks!

@lock lock bot locked as resolved and limited conversation to collaborators Jan 10, 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

Successfully merging a pull request may close this issue.

2 participants