arb / celebrate

A joi validation middleware for Express.
MIT License
1.33k stars 66 forks source link

Show all errors #123

Closed juliuscc closed 5 years ago

juliuscc commented 5 years ago

My joi schema looks like this:

{
  body: {
    state: joi.object().keys({
      token_url: joi.string().required(),
      grant_type: joi.string().required(),
      user_id: joi.string().required(),
      stream_id: joi.string().required(),
      node_id: joi.string().required()
    }).required(),
    code: joi.string().required()
  }
}

Here is an example value that is not working as expected:

{ state: { token_url: 'http://test.com/oauth2/token' }, code: 'authorization_code' }

The issue I am having with celebrate is:

I am migrating from express-validation with joi@14 to celebrate with @hapi/joi@15.1.0. Everything except for one thing is going smoothly.

I try to output an error in the format:

[
  { field: 'state', message: '"grant_type" is required' },
  { field: 'state', message: '"user_id" is required' },
  { field: 'state', message: '"stream_id" is required' },
  { field: 'state', message: '"node_id" is required' }
]

I attach a custom error handler to the router and parse the incoming error. In express-validation you get an error array with all necessary information. With celebrate I only get one single error. It looks like this:

{
  "joi": {
    "isJoi": true,
    "name": "ValidationError",
    "details": [
      {
        "message": "\"state\" is required",
        "path": ["state"],
        "type": "any.required",
        "context": { "key": "state", "label": "state" }
      }
    ],
    "_object": { "code": "authorization_code" }
  },
  "meta": { "source": "body" }
}

Is it possible to get all errors or do you only get one?

Marsup commented 5 years ago

That's likely a duplicate of #67.

juliuscc commented 5 years ago

Yes thank you!