Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 验证express验证器中的对象数组_Node.js_Express Validator - Fatal编程技术网

Node.js 验证express验证器中的对象数组

Node.js 验证express验证器中的对象数组,node.js,express-validator,Node.js,Express Validator,我正在使用express validator验证我的字段。但现在我有了2或3个对象的数组,其中包含“userId”和“Hours”字段,如下所示 [ { user_id:1, hours:8 }, { user_id:2, hours:7 } ] 现在我需要验证对象属性,如hours或user\u id是否为空。如果为空,则抛出错误 let arr = [ { user_id:1, hours:8 }, { u

我正在使用express validator验证我的字段。但现在我有了2或3个对象的数组,其中包含“userId”和“Hours”字段,如下所示

[
  {
    user_id:1,
    hours:8
  },
  {
    user_id:2,
    hours:7
  }
]
现在我需要验证对象属性,如hours或user\u id是否为空。如果为空,则抛出错误

let arr = [
  {
    user_id:1,
    hours:8
  },
  {
    user_id:2,
    hours:7
  }
]
您可以这样放置支票:

check("arr.*.user_id")  
  .not()  
  .isEmpty()

check("arr.*.hours")  
  .not()  
  .isEmpty()

您可以通过访问请求正文来实现这一点:

const{body}=require('express-validator')
正文('*.')
.notEmpty()

我可以使用如下通配符:

app.post('/users', [
    body().isArray(),
    body('*.user_id', 'user_idfield must be a number').isNumeric(),
    body('*.hours', 'annotations field must a number').exists().isNumeric(),
], (req, res) => {
  const errors = validationResult(req);
  if (!errors.isEmpty()) {
      return res.status(422).json({errors: errors.array()});
  }
 return res.status(200).json(req.body)

对于希望使用checkSchema验证对象数组的任何人,下面是一个示例,我将验证一个可以包含0项的对象数组。如果抛出函数进行自定义验证,您甚至可以进行自定义验证:

JSON:

检查模式:

const customValidator = (async (employees, { req }) => {
    if (!Array.isArray(employees)) {
        return true; // let the base isArray: true validation take over.
    }

    if(!something) {
        throw Error('employee error');
    }

    // validate at your will here. 

    return true;    
}
checkSchema({
    employees: {
        isArray: true,
        min: 0,
        custom: {
            options: customValidator
        },
    },
    "employees.*.roleId": {
        isInt: true
    },
    "employees.*.employeeId": {
        isInt: true
    }
})

在您进行验证时,我建议您查看一下
Joi
。这使得此类案例的验证非常容易。Joi docs=>这是什么?我只想使用express validator进行验证。
const customValidator = (async (employees, { req }) => {
    if (!Array.isArray(employees)) {
        return true; // let the base isArray: true validation take over.
    }

    if(!something) {
        throw Error('employee error');
    }

    // validate at your will here. 

    return true;    
}
checkSchema({
    employees: {
        isArray: true,
        min: 0,
        custom: {
            options: customValidator
        },
    },
    "employees.*.roleId": {
        isInt: true
    },
    "employees.*.employeeId": {
        isInt: true
    }
})