Is there a preferred form handling and validation library for Express?
I'm really looking for a similar level of abstraction as is found in Django forms - i.e. validation and error reporting in the template.
If the same validation could be used on the client side, that would be great.
Has anyone used, or written, anything good?
According to the official website, Express Validator is a set of Express. js middleware that wraps validator. js , a library that provides validator and sanitizer functions. Simply said, Express Validator is an Express middleware library that you can incorporate in your apps for server-side data validation.
Basic Validation − First of all, the form must be checked to make sure all the mandatory fields are filled in. It would require just a loop through each field in the form and check for data. Data Format Validation − Secondly, the data that is entered must be checked for correct form and value.
One method of preventing SQL injection is to sanitize inputs. Input sanitization is a cybersecurity measure of checking, cleaning, and filtering data inputs before using them. validator. js is a library of string validators and sanitizers that can be used server-side with Node.
It looks like there's a module for this located at https://github.com/caolan/forms. I've never used it, but it seems fairly full featured.
This also looks viable and is still being developed: https://github.com/ctavan/express-validator
Here's an example of validating a form submission (login post request):
exports.login.post = function(req, res){
req.assert('username', 'Enter username').notEmpty();
req.assert('password', 'Enter password').notEmpty();
res.locals.err = req.validationErrors(true);
if ( res.locals.err ) {
if ( req.xhr ) {
res.send(401, { err: res.locals.err });
} else {
res.render('login', { err: res.locals.err });
}
return;
}
//authenticate user, data is valid
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With