admin管理员组

文章数量:1180477

I'm fairly new to using Joi to validate request payloads in hapi. My question is the following. I have this defined route:

{
    method: 'POST',
    path: '/foo/bar',
    config: {
      description: 'foo.bar',
      handler: handlers.foo,
      auth:false,
      tags: ['api'],
      validate: {
        payload: {
          email : Joi.string().required(),
          password : Joi.string().required(),
        }
      }
    }
}

Email and password are my required properties. However, i would like to allow other properties without having to specify them all. for example:

{
  email: [email protected],
  password: fooPass,
  name: myName,
  surname: mySurname
}

Is there a way to do it with Joi?

I'm fairly new to using Joi to validate request payloads in hapi. My question is the following. I have this defined route:

{
    method: 'POST',
    path: '/foo/bar',
    config: {
      description: 'foo.bar',
      handler: handlers.foo,
      auth:false,
      tags: ['api'],
      validate: {
        payload: {
          email : Joi.string().required(),
          password : Joi.string().required(),
        }
      }
    }
}

Email and password are my required properties. However, i would like to allow other properties without having to specify them all. for example:

{
  email: [email protected],
  password: fooPass,
  name: myName,
  surname: mySurname
}

Is there a way to do it with Joi?

Share Improve this question edited Nov 4, 2015 at 2:30 Gergo Erdosi 42k21 gold badges121 silver badges95 bronze badges asked Nov 3, 2015 at 23:21 João Minhós RodriguesJoão Minhós Rodrigues 1731 gold badge2 silver badges10 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 31

You can set allowUnknown to true in options:

validate: {
  payload: {
    email : Joi.string().required(),
    password : Joi.string().required(),
  },
  options: {
    allowUnknown: true
  }
}

The options parameter is passed to Joi on validation.

For current version of Joi (v15.1.0), while doing

Joi.validate(value, schema, options)

set allowUnknown: true

into the options object.

Reference:

https://github.com/hapijs/joi/blob/v15.1.0/API.md#validatevalue-schema-options-callback

本文标签: javascriptAllow optional parameters in Joi without specifying themStack Overflow