sails-hook-valichain
v1.0.4
Published
Sails hook for validating requests using valichain.
Downloads
4
Maintainers
Readme
sails-hook-valichain
Adds valichain functionality to sails requests.
Installation
npm install sails-hook-valichain --save
Usage
Create reusable rules:
var rules = {
create : {
name : new Valichain()
.s$trim()
.v$matches(/^[a-z\. ]+$/i)
.msg("Name must have only letters, spaces and dots"),
address : new Valichain().default(null).s$trim().v$blacklist("'\""),
},
//...
};
NOTE: see valichain and its documentation for details about usage.
Use inside a controller method:
// returns the same object of Valichain.extract()
var params = req.valichain(rules.create);
if (!params) {
sails.log.debug(
"Validation failed. Results:",
req.valichain.result // result of Valichain.validate()
);
return res.badRequest();
}
console.log("validation succeeded: ", params);
var user = yield User.create({
name: params.name, // validate and sanitized value
address: params.address // validate and sanitized value
});
//...
NOTE: see Valichain.extract() and Valichain.validate() for details about result structures.
License
MIT