【问题标题】:How can I validate a nested POST body payload with the checkSchema middleware in express-validator?如何使用 express-validator 中的 checkSchema 中间件验证嵌套的 POST 正文有效负载?
【发布时间】:2020-01-13 06:47:32
【问题描述】:

给定以下示例 POST 正文:

User = {
    email: string; //required, email
    password: string; //required, min length 8

    profile: {
        firstName: string;  //required
        lastName: string;   //required
        gender: Gender;     //required, enum (M/F)
    }
}

如何定义一个 express ValidationSchema 来验证嵌套的 Profile 对象及其在 User 中的道具

userValidatorSchema: ValidationSchema = {
    email: {
        isEmail: {}
    },
    password: {
        exists: {
            options: {checkNull: true},
            errorMessage: 'Null value'
        },
        isEmpty: {
            negated: true,
            errorMessage: 'Empty string'
        }        
    }
    //Profile validation??

Express-validator docs 让它看起来像是 express-validator 无法实现的。如果是这样,什么是更通用的 express 验证器?

【问题讨论】:

    标签: express express-validator


    【解决方案1】:

    您是否尝试过这种方法:

    // Profile validation
    profile: {
      optional: false
    },
    'profile.firstName': {
      notEmpty: true,
      optional: false
    }
    // ...others
    

    【讨论】:

    猜你喜欢
    • 2023-01-12
    • 1970-01-01
    • 2020-06-29
    • 2014-08-05
    • 2017-02-03
    • 2019-04-21
    • 2013-07-11
    • 1970-01-01
    • 2020-04-13
    相关资源
    最近更新 更多