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 badges2 Answers
Reset to default 31You 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
版权声明:本文标题:javascript - Allow optional parameters in Joi without specifying them - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738133105a2065325.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论