【问题标题】:How to Validate an Array of Objects and Sub-Objects using Express Validator Wildcards如何使用 Express Validator 通配符验证对象和子对象数组
【发布时间】:2020-11-13 10:37:46
【问题描述】:

我正在尝试验证通过 JSON 请求中的对象数组传递的数据。我使用了文档中指定的 express-validator 通配符,但无论对象的属性是否为空,都会返回错误消息。我已经在网上检查了各种解决方案,试图调试这个问题,但都无济于事。如果有人能指出我做错了什么并提出正确的解决方案,我将不胜感激,谢谢。

验证

body("*.product", "Invalid or Empty Product Details").trim().notEmpty(),
body("*.price", "Invalid or Empty Product Price").trim().notEmpty(),
body("*.customer", "Invalid or Empty Customer Details")
   .trim()
   .notEmpty(),
body("*.seller", "Invalid or Empty Seller Details").trim().notEmpty(),
body("*.haulageOptions", "Invalid or Empty Haulage Options").trim().notEmpty()

JSON 请求

{
    "order": [
        {
            "product": {
                "commodity": "",
                "quantity": ""
            },
            "price": {
                "orderPrice": "",
                "deliveryPrice": "",
                "totalPrice": ""
            },
            "customer": {
                "name": "",
                "email": "",
                "location": {
                    "state": "",
                    "localGovernment": "",
                    "town": "",
                    "address": ""
                }
            },
            "seller": {
                "name": "",
                "phone": [
                    ""
                ],
                "email": ""
            },
            "haulageOptions": {
                "firstOrder": "",
                "secondOrder": "",
                "thirdOrder": ""
            }
        }
    ]
}

【问题讨论】:

    标签: express validation object express-validator


    【解决方案1】:

    我想通了

    验证

        check("order.*.product.commodity", "Empty Product Commodity").trim().notEmpty(),
        check("order.*.product.quantity", "Empty Product Quantity").trim().notEmpty(),
        check("order.*.price.orderPrice", "Empty Product Order Price").trim().notEmpty(),
        check("order.*.price.deliveryPrice", "Empty Product Delivery Price").trim().notEmpty(),
        check("order.*.price.totalPrice", "Empty Product Total Price").trim().notEmpty(),
        check("order.*.customer.name", "Empty Customer Name").trim().notEmpty(),
        check("order.*.customer.email", "Empty Customer Email").trim().notEmpty(),
        check("order.*.customer.location.state", "Empty Customer Location (State)").trim().notEmpty(),
        check("order.*.customer.location.localGovernment", "Empty Customer Location (Local Government)").trim().notEmpty(),
        check("order.*.customer.location.town", "Empty Customer Location (Town)").trim().notEmpty(),
        check("order.*.customer.location.address", "Empty Customer Location (Address)").trim().notEmpty(),
        check("order.*.seller.name", "Empty Seller Name").trim().notEmpty(),
        check("order.*.seller.phone", "Empty Seller Phone Number").trim().notEmpty(),
        check("order.*.seller.email", "Empty Seller Email Address").trim().notEmpty(),
        check("order.*.haulageOptions.firstOrder", "Empty Haulage Options").trim().notEmpty(),
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-29
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 2020-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多