README
express-formwork
express-formwork is a completely asynchronous form validator and sanitizer for express based on validator
Installation
$ npm install express-formwork
Validator
The validator is and express middleware responsible for the validation and sanitization of the fields.
var validator = require('express-formwork').validator;
//...
app.use(validator( field1,
field2,
field2 ));
//...
Field
The field is a configuration for the validation and sanitization of a single value.
var field = require('express-formwork').field;
var field1 = field(name, location);
- name (string) the name of the field
- location (string | array) where to take the value from
Location
The location is where the value will be taken from.
Possible values:
- body the value will be searched in the body of the request (the value will be available in req.formwork.body.name.value)
- query the value will be searched in the query of the request (the value will be available in req.formwork.query.name.value)
- params the value will be searched in the parameters of the request (the value will be available in req.formwork.params.name.value)
- [ ] an array of the previous values. The value will be seached in those locations following the provided order (the value will be available in req.formwork.any.name.value)
examples
var field = require('express-formwork').field;
var field1 = field(name1, 'body');
var field2 = field(name2, 'query');
var field3 = field(name3, 'params');
var field4 = field(name4, ['body']); // same as field.body(name1)
var field5 = field(name5, ['query']); // same as field.query(name2)
var field6 = field(name6, ['params']); // same as field.params(name3)
var field7 = field(name7, ['body', 'query', 'params']); // same as field.any(name4)
var field8 = field(name8, ['query', 'body']);
Operations
On each field you can execution various operations.
var field1 = field(name1, location1).op1().op2().op3().op4();
optional()
If the current value is undefined/null/'' it will stop the operations chain
required(error)
If the current value is undefined/null/'' it will add the given error to the field and stop the operations chain
validate(callback)
The callback will validate the value of the field. If the validation is unsuccessful it will attach the error to the field and stop the operations chain
prototype
function (value, callback, formwork, req, res) {
// everything ok continue with the operation chain
return callback();
// validation failed stop the current field and attach an error to it
return callback(undefined, 'invalid value');
// forward an error from asynchronous module, stop the entire validator and forword the error to express-formwork
return callback(err);
}
Parameters:
- value (mandatory) the current value of the field to validate
- callback (mandatory) the callback that must be invoked to continue with the the next operation (see example)
- formwork (optional) reference to the current formwork object
- req (optional) current express request
- res (optional) current express response
The predefined validators are:
- contains(seed) | check if the string contains the seed.
- equals(comparison) | check if the string matches the comparison.
- isAfter([date]) | check if the string is a date that's after the specified date (defaults to now).
- isAlpha([locale]) | check if the string contains only letters (a-zA-Z).
Locale is one of
['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fr-FR', 'fa-IR', 'he', 'hu-HU', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']
) and defaults toen-US
. Locale list isvalidator.isAlphaLocales
. - isAlphanumeric([locale]) | check if the string contains only letters and numbers.
Locale is one of
['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fr-FR', 'fa-IR', 'he', 'hu-HU', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']
) and defaults toen-US
. Locale list isvalidator.isAlphanumericLocales
. - isAscii() | check if the string contains ASCII chars only.
- isBase32() | check if a string is base32 encoded.
- isBase64() | check if a string is base64 encoded.
- isBefore([date]) | check if the string is a date that's before the specified date.
- isBIC() | check if a string is a BIC (Bank Identification Code) or SWIFT code.
- isBoolean() | check if a string is a boolean.
- isByteLength([options]) | check if the string's length (in UTF-8 bytes) falls in a range.
options
is an object which defaults to{min:0, max: undefined}
. - isCreditCard() | check if the string is a credit card.
- isCurrency([options]) | check if the string is a valid currency amount.
options
is an object which defaults to{symbol: '