express.js - best POST json schema validator

11,901

Solution 1

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

Solution 2

Hi 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 doesnt 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).

Good luck!

Solution 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

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

Share:
11,901
Kosmetika
Author by

Kosmetika

#SOreadytohelp

Updated on June 13, 2022

Comments

  • Kosmetika
    Kosmetika almost 2 years

    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.

  • Kosmetika
    Kosmetika over 10 years
    thanks, it looks very nice!
  • Aman Virk
    Aman Virk almost 9 years
    Try indicative.adonisjs.com , has human friendly api to validate schema
  • Younes
    Younes over 8 years
    Up-vote for the rational behind the design of the solution: (1) using json-schema, a dedicated schema language that will help in standardizing how we design our API, and for (2) avoiding to hijack the control from the application (separation of concerns).
  • Harindaka
    Harindaka over 8 years
    Same here. Upvote for inversion of control :D. IMO this is the best solution.