使用通配符时,validator会通过所有测试用例,即使测试应该失败

时间:2020-10-20 18:17:54

标签: node.js validation validator.js

我有一个订购对象数组,我正在尝试验证每个对象的属性。 my runkit code example here。有人可以告诉我为什么要通过测试,什么时候不能通过测试,以及如何解决?

对象主体是对象的数组:

  let body={order:[
  {CompanyId:12},
  {CompanyId:00}]}

验证规则:

const orderValidationRule = {
  "order.*.CompanyId": "required|digits_between:4,6", //4 to 6 digits long
  "order.*.Location": "required:size:2", 
  "order.*.CustomerId": "required:min:0"}

调用验证方法来检查对象:

const validator = (body, rules, customMessages, callback) => {
  const validation = new Validator(body, rules, customMessages);
  validation.passes(() => callback(null, true));
  validation.fails(() => callback(validation.errors, false));
};

  validator(body.order, orderValidationRule, {}, (err, status) => {
  if (!status) {
    throw `validation failed: ${JSON.stringify(err)}`;
  }
  else{
    console.log(`validation success: ${status} ${JSON.stringify(body.order)}`)
  }
});

1 个答案:

答案 0 :(得分:0)

我将订单验证规则的格式设置错误。我需要:

"order.*.CompanyId": "between:1000,9999999|numeric"
"order.*.Location": "required|size:2"
"order.*.CustomerId": "required|min:0|numeric"