
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
express-joi-validations
Advanced tools
Express middleware to validate request (headers, params, query, body) using Joi
Express middleware to validate request (headers, params, query, body) using Joi
npm i express-joi-validations
import validate from 'express-joi-validations';
router.put('/posts/:id', validate({ headers: userToken, params: postId, body: postBody }), (request, response) => {
// Validation errors will be in request.validationErrors
// Validation values will be in request.validationValues
});
import express from 'express';
import { expressJoiValidations } from 'express-joi-validations';
const app = express();
app.use(expressJoiValidations({ validationConfigs }));
throwErrors (default: false): Define if errors should be thrown (suggested usage with express-async-errors)
overwriteRequest (default: false): Define if original request should be overwritten with validated data.
import { Joi, validateBody } from 'express-joi-validations';
// POST /posts
// body: { title: "Lorem ipsum", content: "Lorem ipsum dolor sit amet" }
const postBody = Joi.object({
title: Joi.string().required().trim(),
content: Joi.string().required().trim().min(10),
});
router.post('/posts', validateBody(postBody), postsController.create);
import { Joi, validateParams } from 'express-joi-validations';
// GET /posts/507f1f77bcf86cd799439011
const postId = Joi.object({
id: Joi.string().hex().length(24),
});
router.get('/posts/:id', validateParams(postId), postsController.detail);
import { Joi, validateQuery } from 'express-joi-validations';
// GET /posts?page=2
const postQuery = Joi.object({
page: Joi.optional().number().integer().min(1).default(1),
});
router.get('/posts', validateQuery(postQuery), postsController.list);
import { Joi, validateHeaders } from 'express-joi-validations';
// DELETE /posts/507f1f77bcf86cd799439011
// headers: { Authorization: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" }
const userToken = Joi.object({
authorization: Joi.string().regex(/^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_.+/=]*$/),
});
router.delete('/posts/:id', validateHeaders(userToken), postsController.remove);
import { validateHeaders, validateParams, validateBody } from 'express-joi-validations';
router.put('/posts/:id', validateHeaders(userToken), validateParams(postId), validateBody(postBody), postsController.update);
To every function, is possible to add a second parameter to specify custom Joi Options:
router.post('/posts', validateBody(postBody, { allowUnknown: true }), postsController.create);
FAQs
Express middleware to validate request (headers, params, query, body) using Joi
The npm package express-joi-validations receives a total of 1,081,751 weekly downloads. As such, express-joi-validations popularity was classified as popular.
We found that express-joi-validations demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer 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.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.