| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
#feathers-validator
WIP. Validator for Feathers services, or any service.
require('feathers-validator') will export a class. Simply instantiate it with your form data and validation rules. The form data and validation rules should be objects. The errors() method returns a string array containing any validation errors encountered.
Example:
var feathers = require('feathers');
var myService = {
create: function(data, params, callback) {
var Validator = require('feathers-validator');
var validator = new Validator(data, {
username: 'required|max:255',
password: 'required|min:6',
email: 'required|email',
add_to_mailing_list: 'required|boolean'
});
var errors = validator.errors();
if (errors.length == 0) {
//Request is valid! Do stuff safely, without
//breaking your app!
//...
callback(null, {error: 'success'});
} else {
//Validation errors occurred.
//...
callback(null, {error: 'failure', errors: errors});
}
}
}
var app = feathers().configure(feathers.rest());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use('/', myService);
module.exports = app;##Rules
Can take any of the following forms: 1. A string, with rules separated by pipes (|). Rule parameters are denoted by a colon (:) and separated by commas (,). Example:
'required|min:6|rule:foo,bar'An object, where every rule is an object. Rule parameters take the form of arrays. Example:
{
required: true,
min: 6,
rule: ['foo', 'bar']
}##Available Validation Rules
Draws inspiration from Laravel's validation rules.
##Contributing to feathers-validator
This package is really easy to extend. All validation is performed by functions. feathers-validator determines which one to use by parsing input, then running a generator function.
If you want to add a new one, include a generator function in index.js. The generator must return a function(key, value). This function in turn must return either: {valid: true} OR {valid: false, error: 'Your error message here'} Otherwise, the validator will crash.
Then, submit a pull request. It will be reviewed, and if it's good, I'll add it to feathers-validator.
| Back | FazBrowse Home | New Git URL |