gpt4 book ai didi

express - 如何使用 express-validator 中的 checkSchema 中间件验证嵌套的 POST 正文有效负载?

转载 作者:行者123 更新时间:2023-12-02 02:40:41 26 4
gpt4 key购买 nike

给定以下示例 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 中的对象及其 Prop
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 验证器?

最佳答案

您是否尝试过这种方法:

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

关于express - 如何使用 express-validator 中的 checkSchema 中间件验证嵌套的 POST 正文有效负载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59711680/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com