json-schema-middleware
middleware to validate json schema
Last updated 4 years ago by lrlna .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install json-schema-middleware 
SYNC missed versions from official npm registry.

json-schema-middleware

stability npm version build status downloads js-standard-style

Middleware to validate your json schemas.

usage

One of the most common things for your code to consume in a node framework is probably going to be JSON. The problem is that it doesn't always come back in the nice format you might need. We gotchu tho: json-schema-middleware takes in a JSON schema and validates the request body against it. Here is how you would use it with merry:

var jsonSchemaMiddleware = require('json-schema-middleware')
var merry = require('merry')

var mw = merry.middleware
var mySchema = `
  {
    "required": true,
    "type": "object",
    "properties": {
      "hello": {
        "required": true,
        "type": "string"
      }
    }
  }
`

var app = merry()
app.router([
  ['/foo', mw([jsonSchemaMiddleware(mySchema), myCoolEndpoint])]
])

function myCoolEndpoint (req, res, ctx, done) {
  console.log('hot code bod', ctx.body)
  done(null, 'success!')
}

API

middleware.schema(string)

Takes a JSON string to validate the response against. It will parse and validate the res against the schema, and attach it to ctx.body as part of middleware. If an error occurs, json-schema-middleware will return a boom type error.

related content

LICENSE

MIT

Current Tags

  • 1.0.3                                ...           latest (4 years ago)

4 Versions

  • 1.0.3                                ...           4 years ago
  • 1.0.2                                ...           4 years ago
  • 1.0.1                                ...           4 years ago
  • 1.0.0                                ...           4 years ago
Maintainers (1)
Downloads
Today 0
This Week 1
This Month 1
Last Day 0
Last Week 0
Last Month 0
Dependencies (5)
Dev Dependencies (8)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |