Package Exports
- express-joi-validations
- express-joi-validations/dist/index.js
- express-joi-validations/dist/index.mjs
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (express-joi-validations) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
express-joi-validations
Validate Express request (headers, params, query, body) using Joi
Installation
npm i express-joi-validationsSuggested usage with express-async-errors
Usage
import validate from 'express-joi-validations';
router.put('/posts/:id', validate({ headers: userToken, params: postId, body: postBody }), postsController.update);validateBody
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);validateParams
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);validateQuery
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);validateHeaders
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);Chain
import { validateHeaders, validateParams, validateBody } from 'express-joi-validations';
router.put('/posts/:id', validateHeaders(userToken), validateParams(postId), validateBody(postBody), postsController.update);Joi options
To every function, is possible to add a second parameter to specify custom Joi Options:
router.post('/posts', validateBody(postBody, { allowUnknown: true }), postsController.create);