Javascript 如何使用ExpressValidator为嵌套对象实现验证
我创建了一个模型,如下所示: 在模型“name”、“commodityID”、“totalAmount”中是必需的,但是请注意,commodityID和totalAmount是内部对象“productDetails”的一部分,现在我使用expressvalidator进行服务器端验证,如下所示 此验证适用于有意义的“名称”字段,但不适用于内部对象的“totalAmount”和“commodityID”字段, 这是我给邮递员拍的照片 你们能告诉我解决这个问题的正确方法吗 对嵌套对象使用通配符*Javascript 如何使用ExpressValidator为嵌套对象实现验证,javascript,arrays,node.js,express,express-validator,Javascript,Arrays,Node.js,Express,Express Validator,我创建了一个模型,如下所示: 在模型“name”、“commodityID”、“totalAmount”中是必需的,但是请注意,commodityID和totalAmount是内部对象“productDetails”的一部分,现在我使用expressvalidator进行服务器端验证,如下所示 此验证适用于有意义的“名称”字段,但不适用于内部对象的“totalAmount”和“commodityID”字段, 这是我给邮递员拍的照片 你们能告诉我解决这个问题的正确方法吗 对嵌套对象使用通配符
用于阵列即
productDetails: [
{
commodityID: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: "commodity",
},
perOnePrice: { type: String, required: true },
totalAmount: { type: Number, required: true },
},
],
使用
[
body('productDetails.*.commodityID).not().isEmpty()
body('productDetails.*.perOnePrice).not().isEmpty()
body('productDetails.*.totalAmount).not().isEmpty()
]
[
body('productDetails.commodityID).not().isEmpty()
body('productDetails.perOnePrice).not().isEmpty()
body('productDetails.totalAmount).not().isEmpty()
]
对于嵌套对象,让我们假设:
productDetails: {
commodityID: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: "commodity",
},
perOnePrice: { type: String, required: true },
totalAmount: { type: Number, required: true },
},
使用
[
body('productDetails.*.commodityID).not().isEmpty()
body('productDetails.*.perOnePrice).not().isEmpty()
body('productDetails.*.totalAmount).not().isEmpty()
]
[
body('productDetails.commodityID).not().isEmpty()
body('productDetails.perOnePrice).not().isEmpty()
body('productDetails.totalAmount).not().isEmpty()
]