@iondrive/validation-middleware
v0.0.6
Published
A JSON Schema validation middleware
Downloads
1
Readme
@iondrive/validation-middleware
A JSON Schema validation middleware module for Node.js/io.js.
Install
npm install @iondrive/validation-middleware
Usage
const express = require('express');
const bodyParser = require('body-parser');
const validate = require('@iondrive/validation-middleware');
const app = express();
app.use(bodyParser.json());
const greetingSchema = {
type: 'object',
properties: {
name: {
type: 'string'
}
},
required: ['name']
};
app.get('/greeting', validate.body(greetingSchema), function (req, res) {
res.send('Hello there ' + req.body.name + '!');
});
app.use(function (err, req, res, next) {
if (err instanceof validate.ValidationError) {
res.status(400).send({
errors: err.errors
});
}
});
app.listen(3000);