admin管理员组文章数量:1334361
I have the following Joi schema for a User:
const userRules = Joi.object({
name: Joi.string().pattern(new RegExp('^[A-Za-zÁÉÍÓÚáéíóúãõÃÕâêôÂÊÔ ]+$')).required(),
email: Joi.string().email().required(),
password: Joi.string().min(8).max(40).required()
});
But for authentication purposes, I only want to validate email
and password
, ignoring name
. Is it possible to do this without having to create a different schema?
I have the following Joi schema for a User:
const userRules = Joi.object({
name: Joi.string().pattern(new RegExp('^[A-Za-zÁÉÍÓÚáéíóúãõÃÕâêôÂÊÔ ]+$')).required(),
email: Joi.string().email().required(),
password: Joi.string().min(8).max(40).required()
});
But for authentication purposes, I only want to validate email
and password
, ignoring name
. Is it possible to do this without having to create a different schema?
2 Answers
Reset to default 7This will ignore name
:
const userRules = Joi.object({
email: Joi.string().email().required(),
password: Joi.string().min(8).max(40).required()
})
.options({allowUnknown: true});
You can use Joi.fork()
to create a derived schema from a base one.
const userRules = Joi.object({
name: Joi.string().pattern(new RegExp('^[A-Za-zÁÉÍÓÚáéíóúãõÃÕâêôÂÊÔ ]+$')).required(),
email: Joi.string().email().required(),
password: Joi.string().min(8).max(40).required()
});
const authSchema = userRules.fork(['name'], (schema) => schema.optional())
Now, in authSchema
, the name
attribute is optional and can be ignored.
本文标签: javascriptHow to ignore one of the fields from a Joi schemaStack Overflow
版权声明:本文标题:javascript - How to ignore one of the fields from a Joi schema? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742338164a2456049.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论