Project Name | Stars | Downloads | Repos Using This | Packages Using This | Most Recent Commit | Total Releases | Latest Release | Open Issues | License | Language |
---|---|---|---|---|---|---|---|---|---|---|
Body Parser | 5,244 | 862,600 | 23,952 | 3 months ago | 69 | April 03, 2022 | 25 | mit | JavaScript | |
Node.js body parsing middleware | ||||||||||
Simorgh | 1,126 | 12 hours ago | 88 | other | JavaScript | |||||
The BBC's Open Source Single Page Application. Contributions welcome! Used on some of our biggest websites, e.g. | ||||||||||
Apicache | 1,063 | 437 | 57 | a year ago | 79 | October 25, 2021 | 52 | mit | JavaScript | |
Simple API-caching middleware for Express/Node. | ||||||||||
Dyson | 836 | 43 | 13 | 6 months ago | 66 | June 08, 2021 | 8 | JavaScript | ||
Node server for dynamic, fake JSON. | ||||||||||
Node Spliderapi | 801 | 9 months ago | 13 | mit | JavaScript | |||||
基于node+express爬虫 API接口项目,包括全国高校信息、成语诗歌、星座运势、历史的今天、音乐数据接口、图片壁纸、搞笑视频、热点新闻资讯 详情接口数据 | ||||||||||
Stock Market India | 574 | 6 months ago | 28 | mit | JavaScript | |||||
API for Indian Stock Market's NSE and BSE. | ||||||||||
Express Zod Api | 356 | 1 | 15 hours ago | 147 | January 08, 2023 | 3 | mit | TypeScript | ||
A Typescript library to help you get an API server up and running with I/O schema validation and custom middlewares in minutes. | ||||||||||
Linkedin Profile Scraper | 331 | 4 days ago | 22 | July 03, 2020 | 22 | mit | TypeScript | |||
🕵️♂️ LinkedIn profile scraper returning structured profile data in JSON. Works in 2020. | ||||||||||
Athena Express | 164 | 3 | a month ago | 52 | March 22, 2022 | 29 | mit | JavaScript | ||
Athena-Express can simplify executing SQL queries in Amazon Athena AND fetching cleaned-up JSON results in the same synchronous or asynchronous request - well suited for web applications. | ||||||||||
Express Json Validator Middleware | 136 | 28 | 5 | a year ago | 27 | May 20, 2022 | 9 | mit | JavaScript | |
Express middleware for validating requests against JSON schema |
Express middleware for validating requests against JSON schemas.
npm install express-json-validator-middleware
import { Validator } from "express-json-validator-middleware";
/**
* Define a JSON schema.
*/
const addressSchema = {
type: "object",
required: ["street"],
properties: {
street: {
type: "string",
}
},
};
/**
* Initialize a `Validator` instance, optionally passing in
* an Ajv options object.
*
* @see https://github.com/ajv-validator/ajv/tree/v6#options
*/
const { validate } = new Validator();
/**
* The `validate` method accepts an object which maps request
* properties to the JSON schema you want them to be validated
* against e.g.
*
* { requestPropertyToValidate: jsonSchemaObject }
*
* Validate `request.body` against `addressSchema`.
*/
app.post("/address", validate({ body: addressSchema }), (request, response) => {
/**
* Route handler logic to run when `request.body` has been validated.
*/
response.send({});
});
Coming from express-jsonschema
? Read the migration notes.
On encountering invalid data, the validator will call next()
with a
ValidationError
object. It is recommended to setup a general error handler
for your app where you handle ValidationError
errors.
Example - error thrown for the body
request property:
ValidationError {
name: "JsonSchemaValidationError",
validationErrors: {
body: [AjvError]
}
}
More information on Ajv errors.
import express from "express";
import { Validator, ValidationError } from "express-json-validator-middleware";
const app = express();
app.use(express.json());
const addressSchema = {
type: "object",
required: ["number", "street", "type"],
properties: {
number: {
type: "number",
},
street: {
type: "string",
},
type: {
type: "string",
enum: ["Street", "Avenue", "Boulevard"],
},
},
};
const { validate } = new Validator();
/**
* Validate `request.body` against `addressSchema`.
*/
app.post("/address", validate({ body: addressSchema }), (request, response) => {
/**
* Route handler logic to run when `request.body` has been validated.
*/
response.send({});
});
/**
* Error handler middleware for validation errors.
*/
app.use((error, request, response, next) => {
// Check the error is a validation error
if (error instanceof ValidationError) {
// Handle the error
response.status(400).send(error.validationErrors);
next();
} else {
// Pass error on if not a validation error
next(error);
}
});
app.listen(3000);
Sometimes your route may depend on the body
and query
both having a specific
format. In this example we use body
and query
but you can choose to validate
any request
properties you like. This example builds on the
Example Express application.
const tokenSchema = {
type: "object",
required: ["token"],
properties: {
token: {
type: "string",
minLength: 36,
maxLength: 36
},
},
};
app.post(
"/address",
validate({ body: addressSchema, query: tokenSchema }),
(request, response) => {
/**
* Route handler logic to run when `request.body` and
* `request.query` have both been validated.
*/
response.send({});
}
);
A valid request must now include a token URL query. Example valid URL:
/street/?uuid=af3996d0-0e8b-4165-ae97-fdc0823be417
Instead of passing in a schema object you can also pass in a function that will return a schema. It is useful if you need to generate or alter the schema based on the request object.
Example: Loading schema from a database (this example builds on the Example Express application):
function getSchemaFromDb() {
/**
* In a real application this would be making a database query.
*/
return Promise.resolve(addressSchema);
}
/**
* Middleware to set schema on the `request` object.
*/
async function loadSchema(request, response, next) {
try {
request.schema = await getSchemaFromDb();
next();
} catch (error) {
next(error);
}
}
/**
* Get schema set by the `loadSchema` middleware.
*/
function getSchema(request) {
return request.schema;
}
app.post(
"/address",
loadSchema,
validate({ body: getSchema }),
(request, response) => {
/**
* Route handler logic to run when `request.body` has been validated.
*/
response.send({});
}
);
The Ajv instance can be accessed via validator.ajv
.
import { Validator, ValidationError } from "express-json-validator-middleware";
const validator = new Validator();
// Ajv instance
validator.ajv;
Ajv must be configured before you call Validator.validate()
to add middleware
(e.g. if you need to define custom keywords.
The major version 1.x
of this module uses [email protected]
, read their changelog and
migration guide here.
Major version 2.x
uses [email protected]
in order to support draft-07 of JSON Schema.
You have to manually configure Ajv to support draft-06 schemas
(see https://github.com/ajv-validator/ajv/tree/v6#using-version-6).
Tests are written using Mocha & Chai.
npm install
npm test