'express.js - best POST json schema validator [closed]

I'm searching for a module to validate POST json requests in my Express.js application.

What json schema module do you use in your node.js apps?

I assume node-validator (https://github.com/chriso/node-validator) is not an option here because it works only with strings.



Solution 1:[1]

I made this - if you are still interested: https://npmjs.org/package/isvalid

Solution 2:[2]

I recently wrote express-jsonschema. The main differences with the other validators out there are:

  1. You use the standard json schemas for validation. If you have written other server side languages you probably have used them before.
  2. It doesn't control how your application responds to invalid data. It does give you an opportunity to hook in and respond however you want. In my experience this is something that is nice to control.

express-schema-validator, express-validate, and Paperwork are all great. They each have their own unique syntax for declaring schemas and also control how your app responds to invalid data (i.e. status code and data structure).

Solution 3:[3]

I made Paperwork, which is a very simple solution for JSON validation. You can do things like:

app.post('/my/route', paperwork({
  username: /[a-z0-9]+/,
  password: String,
  age: Number,
  interests: [String],
  jobs: [{
    company: String,
    role: String
  },
}, function (req, res) {
  // ...
});

It will validate:

{
  username: 'brucewayne',
  password: 'iambatman',
  age: 36,
  interests: ['Climbing', 'CQC', 'Cosplay'],
  jobs: [{
    company: 'Wayne Inc.',
    role: 'CEO'
  }]
}

Or will silently responds a 400 error with information about what's wrong. Check the doc to do more advanced usages.

Solution 4:[4]

There are a lot of options for those who choose JSON Schema validation rules. Good libs comparison here: https://github.com/ebdrup/json-schema-benchmark

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Mark Rotteveel
Solution 3 Laurent Perrin
Solution 4 Alex Povar