How to implement validation using express-validator for the nested object How to implement validation using express-validator for the nested object express express

How to implement validation using express-validator for the nested object


for array i.e

productDetails: [  {    commodityID: {      type: mongoose.Schema.Types.ObjectId,      required: true,      ref: "commodity",    },    perOnePrice: { type: String, required: true },    totalAmount: { type: Number, required: true },      },],

use

[ body('productDetails.*.commodityID').not().isEmpty() body('productDetails.*.perOnePrice').not().isEmpty() body('productDetails.*.totalAmount').not().isEmpty()]

For nested object, lets suppose:

productDetails: {    commodityID: {      type: mongoose.Schema.Types.ObjectId,      required: true,      ref: "commodity",    },    perOnePrice: { type: String, required: true },    totalAmount: { type: Number, required: true },        },

use

[ body('productDetails.commodityID').not().isEmpty() body('productDetails.perOnePrice').not().isEmpty() body('productDetails.totalAmount').not().isEmpty()]


Use Wildcard * for nested Objects Read Here

   [        check('name', " ").not().isEmpty()        check('productDetails.commdityID', " ").not().isEmpty()        check('productDetails.totalAmount', " ").not().isEmpty()    ]