Helper for check all params node.js - sails - express

痴心易碎 提交于 2019-12-13 01:08:02

问题


I want to check my params in my controllers. For the moment, I'm doing like that:

create: function (req, res) {
    if(req.params.label && req.params.password){
     // do stuff
     }
}

But I want to do this more quickly. I'm wondering if there is no tools which already exist.

I want to do things like that:

create: function (req, res) {
    checkParams({label: {empty: false}, password: {empty: false}}, function(err){
        // Do stuff
     });
}

Do you know something which could help me?

Thank you.


回答1:


Okay, I'll answer to myself.

I found this module, really useful. https://github.com/chriso/node-validator

And I wrote, by my own (and with a little help from here =D) a little helper for manage validations:

http://pastebin.com/Bw0qdbu1

Thanks for the help ;) Two samples are available in the file, I'm using sails.js. But it can be used with anything, you need the validator package, obviously.

npm install validator --save

Don't forget to import validator at the start of file.

var validator = require('../services/validator');


来源:https://stackoverflow.com/questions/19077325/helper-for-check-all-params-node-js-sails-express

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!