get-validated
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 ValidationError
object
with a status|code of 412. And the messages
property is set to an object keyed
by parameter name of the current errors.
Basic Usage
Initial set up for a our little example express app.
var express = router = express get_validated = validations;
Once we have our express app all set then we create our validations object.
validations =
Now that we have our validations
object we can use it in a few different
ways.
- Apply it to the route and then use
req.getValidated
within individual route handlers.
router; router;
- Use it as route specific middleware to validate parameters used by
that route. Note: If you use this method then all errors are sent
to
next(err)
to be handled by your app's current error handler.
router;
Advanced Usage
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.
router;
options
container
- This is your integration point. Anything you attach here will be sent along to your validation functions as thecontainer
object.validateAction
- Don't like my validation handling function? Then write your own. Debugging is your responsibility.renderer
-get-validate
comes with a very simple message renderer that recognizes two tags (':name' and ':value'), provide your own function if you like.
todo
- route specific middleware
- more usage examples showing how to use the joi and composed-validations