README
@valbo/validation-middlewares
Express middlewares to validate JSON request bodies.
Install
npm install @valbo/validation-middlewares
Usage
This package exports functions to validate data against JSON Schemas using Ajv.
validateSchema<SchemaId extends string>(ajv: Ajv, schemaId: SchemaId, data: unknown): void
Validates some data against a JSON Schema loaded in an Ajv instance. The function is generic, so you can specify which schema ids are valid. It will throw a 400 BadRequestError if the data is invalid.
createParseJsonBodyMiddleware(options?: OptionsJson): RequestHandler
A wrapper around the body-parser json() function. It returns a middleware that checks if the request has a Content-Type: application/json header and then parses the request body as a JSON.
The middleware throws a 415 Unsupported Media Type error if the header is missing, and a 400 Bad Request Error if it cannot parse the body.
configureValidateMiddleware<SchemaId extends string>(ajv: Ajv): (schemaId: Schema) => RequestHandler
It receives an Ajv instance configured with all the required JSON schemas and returns a function that receives the id of the JSON schema to use for validation. That returned function then returns a middleware that validates the request body against the provided schema.
The middleware throws a 400 Bad Request Error if the validation fails.
Example
import express from 'express';
import Ajv from 'ajv';
import { createParseJsonBodyMiddleware, configureValidateMiddleware, CreateValidateMiddleware } from '@valbo/validation-middlewares';
const parseBody = createParseBody({ limit: '1Mb' });
const findUserSchema = {
type: 'object',
properties: { username: { type: 'string' } },
required: ['username'],
additionalProperties: false,
};
const ajv = new Ajv();
ajv.addSchema(schema, 'findUserRequest')
type SchemaIds = 'findUserRequest';
const validateMiddleware: CreateValidateMiddleware<SchemaIds> = configureValidateMiddleware<SchemasIds>(ajv);
const app = express()
.use(parseBody)
.use('/users/find', validateMiddleware('findUserRequest'));