validatorjs is passing all test cases when using wildcard, even when test should fail

410 Views Asked by At

I have an array of order objects, and I'm trying to validate properties on each object. my runkit code example here. Can someone tell me why this is passing tests, when it should fail them, and how to fix it?

object body is an array of objects:

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

validation rule:

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

calling a validation method to check the objects:

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

There are 1 best solutions below

0
On BEST ANSWER

I was formatting the order validation rules wrong. I needed:

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