Skip to content

alternatives doesn't work with error #1119

@machineghost

Description

@machineghost

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugBug or defect

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions