node-form-validate
A validation module for nodejs and express. It aims to give a convenient way to validate incoming POST data (which is the most common) and be as flexible as possible. This module is in development - feel free to contribute if you feel like it :)
Installation
npm install form-validate
form-validate
with Express
Setting up In order to use the req.Validator
object and to access view helpers you have to register the form-validate middleware with express.
var express = validate = ; // ...Your express initialization logic... app;app;app; // ... var validationConfig = //You can configure certain aspects of the validation module;app; app;app; // ... Create http server
Configuration
You can configure the behaviour of the form-validate
module by passing options when initializing the middleware:
var options = //You can configure certain aspects of the validation module;app;
Options
Options | Type | Default | Description |
---|---|---|---|
i18n | object |
|
form-validate uses the i18n (https://github.com/mashpie/i18n-node) module to translate the error messages. You can configure the i18n module using this option. |
escapeHTML | boolean | true |
If this is true every value will be html escaped when using Validator.getValue() |
stripTags | boolean | true |
If this is true every value will be stripped from html tags when using Validator.getValue() |
Express Usage Example
Using form-validation
in an express context is pretty straight-forward:
app;
Validators
Validators are the core functionality of node-form-validate. Input data can be validated to see if they meet certain
criteria. If they dont match, an error message will be generated. They can be added by using req.Validator.validate()
Validator | Example Configuration |
---|---|
required |
required: true
|
length |
length: {
min: 5,
max: 50
}
|
alpha |
alpha: true |
alphaNumeric |
alphaNumeric: true |
numeric |
numeric: true numeric: { allowNegative: true } // also allow negative values
|
integer |
integer: true integer: { allowNegative: true } // also allow negative values
|
decimal |
decimal: true decimal: { allowNegative: true } // also allow negative values
|
between |
between: { min: 10, max: 15 }
|
email: true This method of testing is not recommended. You should allow all Email addresses and validate it by sending an email to the given address. |
Filters
Filters are applied before the input data is validated. Filters can parse the incoming data and prepare
it for validation or database input. They can be added by using req.Validator.filter()
Filter | Example Configuration |
---|---|
trim |
trim: true
|
toInt |
toInt: true
|
toFloat |
toFloat: true
|
toBoolean |
toBoolean: true
|
hash |
To get a list of all supported hash algorithms on your installation, see: http://nodejs.org/api/crypto.html#crypto_crypto_gethashes
|
escapeHTML |
Explicitly enable HTML escaping (enabled by default):
escapeHTML: true Disable HTML escaping: escapeHTML: false
|
stripTags |
Explicitly enable stripping of HTML tags (enabled by default):
stripTags: true Disable stripping of HTML tags: stripTags: false
|
Getting all Errors
To get validation errors simply call the asynchronous Validator.getErrors(fn)
method and provide a callback which gets called when all validations have been completed. The callback gets provided with an array containing all errors which resulted from the validation process.
Example:
app;
View Helpers
Validator.getErrors()
has to be called before the rendering took place.
Warning: In order to these view helpers to work The local variable Validator
is available in all express views.
/** * Returns the sanitized value of <code>fieldName</code> which ran through all * filters (if any) */Validator /** * Returns all errors for this field as an array */Validator /** * Returns true if this field has any error. You can use this to give the * field a certain class if an error occured */Validator
Jade Usage example
form(action='/login', method='post')
div(class=Validator.hasError('username') ? 'has-error' : '')
label(for='username')=Username
input#username(type='text', name='username', value=Validator.getValue('username'))
div(class=Validator.hasError('password') ? 'has-error' : '')
label(for='password')=Password
input#password(type='text', name='password', value=Validator.getValue('password'))
The MIT License
Copyright (c) 2013 André Eckardt (http://github.com/korve, http://mindpress.de)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.