How to perform jsonschemavalidation through http?

71 views Asked by At

I have defined a restservice and trying to do a jsonschema validation with paperwork:

var _ = require('underscore');
var validate = require('isvalid-express');
var paperwork=require('paperwork');

module.exports = function (app) {

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


};

This is the request I post this in postman to myroute:

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

However it throws an error:

TypeError: undefined is not a function
    at module.exports (c:\heroku\newher\node_codechallenge\node_modules\paperwork\paperwork.js:129:5)
    at module.exports (c:\heroku\newher\node_codechallenge\app\testroutes.js:12:26)
    at Object.<anonymous> (c:\heroku\newher\node_codechallenge\test.js:16:31)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain [as _onTimeout] (module.js:497:10)
    at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)

What is the best way of doing a json schemavalidation for http in node.js?

1

There are 1 answers

0
esp On

You may consider using JSON-schema standard rather than non-standard schema definition that paperwork uses.

There are many JavaScript JSON Schema validators, here is the benchmark: https://github.com/ebdrup/json-schema-benchmark