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

2019-08-13 03:18发布

问题:

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');