Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
express-json-validator-middleware
Advanced tools
An Express middleware to validate requests against JSON Schemas
express.js middleware for JSON schema validation.
WIP, suggestions / bug reports are welcome!
Based heavily on https://github.com/trainiac/express-jsonschema.
$ npm install express-json-validator-middleware
var express = require('express');
var app = express();
var validate = require('express-json-validator-middleware').validate;
var bodyParser = require('body-parser');
// Create a json scehma
var StreetSchema = {
type: 'object',
required: ['number, name, type'],
properties: {
number: {
type: 'number'
},
name: {
type: 'string'
},
type: {
type: 'string',
enum: ['Street', 'Avenue', 'Boulevard']
}
}
}
// This route validates req.body against the StreetSchema
app.post('/street/', validate({body: StreetSchema}), function(req, res) {
// At this point req.body has been validated and you can
// begin to execute your application code
});
/*
Setup a general error handler for JsonSchemaValidationError errors.
As mentioned before, how one handles an invalid request depends on their application.
You can easily create some express error middleware
(http://expressjs.com/guide/error-handling.html) to customize how your
application behaves. When the express-jsonschema.validate middleware finds invalid data it
passes an instance of JsonSchemaValidationError to the next middleware.
Sometimes your route may depend on the body
and query
both having a specific format. In this example I use body
and query
but you can choose to validate any request
properties you'd like.
var TokenSchema = {
type: 'object',
required: ['token']
properties: {
token: {
type: 'string',
format: 'alphanumeric',
minLength: 10,
maxLength: 10
}
}
}
app.post('/street/', validate({body: StreetSchema, query: TokenSchema}), function(req, res) {
// application code
});
A valid request would now also require a url like /street/?token=F42G5N5BGC
.
In express-jsonschema, you could define a required property in two ways. Ajv only supports the first latter
// WRONG
{
type: 'object',
properties: {
foo: {
type: 'string',
required: true
}
}
}
// CORRECT
{
type: 'object',
properties: {
foo: {
type: 'string'
},
required: ['foo']
}
}
Huge thanks to
FAQs
An Express middleware to validate requests against JSON Schemas
The npm package express-json-validator-middleware receives a total of 15,995 weekly downloads. As such, express-json-validator-middleware popularity was classified as popular.
We found that express-json-validator-middleware demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.