如何验证请求正文仅包含某些字段?

时间:2018-12-04 20:13:21

标签: node.js express express-validator

如何使用快速验证器来确保正文或查询仅包含某些字段?例如:

 const { body, query } = require('express-validator/check')

 body.containsOnly("field1", "field2", "field3")

 query.containsOnly("queryParam1", "queryParam2")

1 个答案:

答案 0 :(得分:0)

let fields = ['field1', 'field2'];

function validateBody(body, fields) {
    for(key of fields) { 
        if(!body.hasOwnProperty(key)) 
            return false;
    }
    return true
}

您可以使用类似的东西