【发布时间】:2020-07-20 23:58:40
【问题描述】:
我正在使用 Joi 和 Joigoose 在 Mongoose 模型中设置我的架构,一切正常,直到我添加条件“.when(...)”,如下所示
const ClientJoiSchema = Joi.object().keys({
clientType: Joi.string().valid("physical", "moral").default("physical"),
firstName: Joi.when('clientType', { is: "physical", then: Joi.string().required() }),
lastName: Joi.when('clientType', { is: "physical", then: Joi.string().required() }),
...
)}
当我在 Postman 上尝试时,我得到了这个:
"message": {
"errors": {
"firstName": {
"properties": {
"message": "Validator failed for path `firstName` with value `Daniel`",
"type": "user defined",
"path": "firstName",
"value": "Daniel"
},
"kind": "user defined",
"path": "firstName",
"value": "Daniel"
},
"lastName": {
"properties": {
"message": "Validator failed for path `lastName` with value `Noah`",
"type": "user defined",
"path": "lastName",
"value": "Noah"
},
"kind": "user defined",
"path": "lastName",
"value": "Noah"
}
},
"_message": "Clients validation failed",
"message": "Clients validation failed: firstName: Validator failed for path `firstName` with value `Daniel`, lastName: Validator failed for path `lastName` with value `Noah`"
}
【问题讨论】:
-
请阅读Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers? - 总结是这不是解决志愿者的理想方式,并且可能会适得其反。请不要将此添加到您的问题中。
-
我会考虑的,谢谢通知!
标签: javascript node.js express mongoose joi