Express Validator-如何处理条件验证

时间:2018-10-01 23:13:51

标签: node.js express express-validator validator.js

我有一个表格,如果某人的送货地址与他们的帐单地址不同,他们将选择一个单选按钮,说“否”。

我需要能够检查它的值,如果为“否”,请检查展开的表格。如果是“是”,我将跳到下面的下一个验证。 “是”部分正常工作,但“否”部分始终返回无效状态。是否可以通过该库在这样的条件中嵌套验证?

这是该表格的屏幕截图:

Test Form Image

这是我在这种情况下的验证码:

    router.post('/testData2/', [
      body('billingSameAsShipping', "Please Fill All Fields Of Your Billing Address").custom((value) => {
        if (value === 'no') {
          [
            body('billingFirstName', "Please Enter Your First Name").isLength({
              min: 1
            }),
            body('billingLastName', "Please Enter Your Last Name").isLength({
              min: 1
            }),
            body('billingAddress1', "Please Enter Your Billing Address").isLength({
              min: 1
            }),
            body('billingZip', "Please Enter Your Billing ZipCode").isLength({
              min: 1
            }),
            body('billingCity', "Please Enter Your Billing City").isLength({
              min: 1
            }),
            body('billingState', "Please Enter Your Billing State").isLength({
              min: 1
            })
          ]
        } else {

          return Promise.resolve();
        }

      }),
    body('creditCardNumber', 'Please Enter A Vaild Credit Card Number').isCreditCard(),
    body('expmonth', 'Exp Month Empty').isLength({
      min: 1
    }),
    body('expyear', 'Exp Year Empty').isLength({
      min: 1
    }),
    body('CVV', 'CVV Empty').isLength({
      min: 3
    })
  ],
  (req, res) => {...

这是我们要检查的请求对象

{
  billingSameAsShipping: 'no',
  billingFirstName: 'First',
  billingLastName: 'Last',
  billingAddress1: '450 Test Ave',
  billingZip: '12345',
  billingCity: 'San Diego',
  billingState: 'CA',
}

1 个答案:

答案 0 :(得分:0)

我知道了,Express Validator内置了条件检查

我使用oneOf([])检查单选按钮的值是否为“是” 检查帐单邮寄地址字段是否已填写。

router.post('/testData2/', [
    oneOf([
      [
        body('billingFirstName', "Please Enter Your First Name").isLength({
          min: 1
        }),
        body('billingLastName', "Please Enter Your Last Name").isLength({
          min: 1
        }),
        body('billingAddress1', "Please Enter Your Billing Address").isLength({
          min: 1
        }),
        body('billingZip', "Please Enter Your Billing ZipCode").isLength({
          min: 1
        }),
        body('billingCity', "Please Enter Your Billing City").isLength({
          min: 1
        }),
        body('billingState', "Please Enter Your Billing State").isLength({
          min: 1
        })
      ],
      body('billingSameAsShipping').equals('yes')
    ], "Please Fill Out All Fields Of Your Billing Address"),
    body('creditCardNumber', 'Please Enter A Vaild Credit Card Number').isCreditCard(),
    body('expmonth', 'Exp Month Empty').isLength({
      min: 1
    }),
    body('expyear', 'Exp Year Empty').isLength({
      min: 1
    }),
    body('CVV', 'CVV Empty').isLength({
      min: 3
    })
  ],
  (req, res) => {...

这似乎现在可以正常工作。

有人对此有其他解决方案,还是我上面的解决方案有问题?