1

I need to validate all the queries params of http requests, knex or bookshelf have a function to do such a thing?

Here is my code example:

var validateModelQuery = function(Model , query) {
for(var att in query) {
    if(!Model.has(att)) //does not exist
        return false;
}
return true;
};

/

router.get('/customers', function(req, res, next) {
if (!validateModelQuery(Customer , req.query)) {
    res.status(400);
Customer.where(req.query).fetchAll()
......

What do you guys suggest to validate request params or body, because write all the fields manually at every route is really a bad coding.

  • Hi João, I guess neither bookshelf nor knex provide such a function. If you know the underlying database, you could query some system table (and probably cache the results). For example MySql: http://stackoverflow.com/questions/4165195/mysql-query-to-get-column-names#4165253; then translate column name to camel case if that's applicable to your Bookshelf model attribute names. – bgerth Aug 16 '16 at 14:59

1 Answers1

0

knex provide hasColumn function which can be used to check if a column is already exists.

Mohamed Ben HEnda
  • 2,686
  • 1
  • 30
  • 44