Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express Req.Body Validation

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.

like image 810
arb Avatar asked May 23 '13 15:05

arb


People also ask

What does req body do in express?

The req. body object allows you to access data in a string or JSON object from the client side. You generally use the req. body object to receive data through POST and PUT requests in the Express server.

What is checkFalsy in express validator?

What is checkFalsy in Express-validator? You can customize this behavior by passing an object with the following options: nullable: if true, fields with null values will be considered optional. checkFalsy: if true, fields with falsy values (eg "", 0, false, null) will also be considered optional.10-Jul-2014.

How do I get an express Body request?

Express has a built-in express. json() function that returns an Express middleware function that parses JSON HTTP request bodies into JavaScript objects. The json() middleware adds a body property to the Express request req . To access the parsed request body, use req.

What is bail express validator?

According to the express-validator documentation: . bail() is useful to prevent a custom validator that touches a database or external API from running when you know it will fail. Can be used multiple times IN THE SAME validation chain if needed. In your case it would be like: [ check('id') .


1 Answers

Using JSON schema

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:

  • JaySchema
  • Amanda
  • json-gate
  • Tiny Validator
  • js-schema

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.

Not using JSON schema

  • Joi https://github.com/spumko/joi
  • screener https://github.com/RushPL/node-screener
like image 140
Myrne Stol Avatar answered Oct 06 '22 00:10

Myrne Stol