get-validated0.1.0 • Public • Published
A simple, flexible validation middleware to make validation less painful
What is it?
get-validated is not a validation framework. Rather, it is a simple express middleware that allows you to integrate many validation frameworks into your express app in a consistent manner.
It comes with the validator framework built in.
All validation errors are combined and put into a single
with a status|code of 412. And the
messages property is set to an object keyed
by parameter name of the current errors.
Initial set up for a our little example express app.
var express =router = expressget_validated =validations;
Once we have our express app all set then we create our validations object.
Now that we have our
validations object we can use it in a few different
- Apply it to the route and then use
req.getValidatedwithin individual route handlers.
- Use it as route specific middleware to validate parameters used by
that route. Note: If you use this method then all errors are sent
next(err)to be handled by your app's current error handler.
Now one of the fun parts of this module is that you can integrate pretty much any validations framework that you want with it. Here is a list of some:
I'm going to use Composed Validations here to show how you integrate a validation libray, at least one way to do it.
In Composed Validations examples they show an address validator, so let's work with that.
var cv = ;var addressValidator = cv
So now that we have our address validator we can integrate it directly into our validations object like so
var options =container:'cv': cv // we don't have to integrate but just for fun we will.'addressValidator': addressValidator;var validations =;
After we have our validations object we can use it just like any other one.
container- This is your integration point. Anything you attach here will be sent along to your validation functions as the
validateAction- Don't like my validation handling function? Then write your own. Debugging is your responsibility.
get-validatecomes with a very simple message renderer that recognizes two tags (':name' and ':value'), provide your own function if you like.
- route specific middleware
- more usage examples showing how to use the joi and composed-validations