我有一个订购对象数组,我正在尝试验证每个对象的属性。 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)}`)
}
});
答案 0 :(得分:0)
我将订单验证规则的格式设置错误。我需要:
"order.*.CompanyId": "between:1000,9999999|numeric"
"order.*.Location": "required|size:2"
"order.*.CustomerId": "required|min:0|numeric"