I want to create a piece of express middleware looks something like this:
function validate (options) {
var defaultOptions = {...}
, validations = _.extend(defaultOptions, options);
return validate (req, res, next) {
/* Use some sort of validation framework where I can pass `validations` into*/
next(someErrors || null)
}
}
I've looked at both node-validator with the middleware option as well as tracery but neither of them looked like you could pass a "rule set" into them and have them run the rules against a provided input.
Does anyone have any suggestions on how to do this with either of those modules or with another one that I haven't found yet? Am I going to have to roll my own to do this?
UPDATE
This is indeed to validate form posts. I know there won't be a single piece of middleware that will cover all of the posts for the whole site; this will be used only for certain routes. I want reusable middleware because we are making APIs that have common routes and expect common form bodies that we want to validate in similar fashion with the option to tweak that on a per API basis.
Without knowing any more about the specific things you'd like to check for, I think tools based on JSON schema could serve you pretty well. JSON schema specifies many kinds of validation rules.
Examples of node modules:
I made this list based on a simple search for "json schema" on Nipster. I find Nipster is a great tool to get a quick overview of good modules for a particular task because it also includes number of forks and github stars of the project as a gauge for popularity, which in turn often says something about a module's quality and maturity. Of course not to be taken blindly, but as a start for further research.
I expect that actual not all modules for JSON schema have support for all validation rules, so I think you should start by inventorying what kind of rules you actually need (or would like to have available in the future), then narrow down your selection based on that.
There's an official test suite for JSON schema tools. You may want to look for modules that advertise compliance with this suite.