open-mint-validator
v0.0.6
Published
The form validation class
Downloads
6
Readme
open-mint-validator
The form validation for node apps.
Usage
Install the library with npm install open-mint-validator
var Validation = require('open-mint-validator');
module.exports = function (req, res) {
var validator = new Validation();
var fields = req.body;
var query = {
title: fields.title,
amount: fields.amount,
currency: 'EUR'
};
var response;
validator.setRules({
'title': {
'len': {
'args': [
3,
100
]
}
},
'amount': {
'float': true
},
'currency': {
'enum': {
'args': [
[
'EUR',
'USD',
'PLN'
]
]
}
}
});
if (validator.validate(query)) {
// validation passed.
// do something
} else {
// validation failed.
// do something
var errors = validator.getErrors();
var key;
for (key in errors) {
console.log(
errors[key]
);
}
}
};
Available Validators
- len args: [ min, max ] - check if the string's length falls in a range. Note: this function takes into account surrogate pairs.
- email args: [] - check if the string is an email.
- equals args: [{string}] - check if the string matches the comparison.
- numeric args: [] - check if the string contains only numbers.
- int args: [] - check if the string is an integer.
- float args: [] - check if the string is a float.
- creditCard args: [] - check if the string is a correct credit card number.
- enum args: [ [ {string}, {string}... ] ] - check if the string is in a array of allowed values.
- date args: [] - check if the string is a date.
- alpha args: [] - check if the string contains only letters (a-zA-Z).
- url args: [ {object} ] - check if the string is an URL.
options
is an object which defaults to{ protocols: ['http','https','ftp'], require_tld: true, require_protocol: false, allow_underscores: false }
.