【问题标题】:Node Express joi validation failed when I added .when()当我添加 .when() 时,Node Express joi 验证失败
【发布时间】: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`"
    }

【问题讨论】:

标签: javascript node.js express mongoose joi


【解决方案1】:

Joi.object().keys({ ... }) 不是函数,您应该只使用Joi.object({ ... })

欲了解更多信息,请查看JOI Documentation

【讨论】:

    猜你喜欢
    • 2020-05-15
    • 1970-01-01
    • 2018-11-03
    • 1970-01-01
    • 2019-05-05
    • 2019-08-03
    • 1970-01-01
    • 2015-03-12
    • 2018-06-25
    相关资源
    最近更新 更多