1.1.0 • Published 1 year ago

@akrdevtech/lib-express-joi-validation-middleware v1.1.0

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

Express Joi Validation Middleware

Description

Implementation of Joi middleware for ExpressJS with TS.

  • TypeScript support.
  • Specify the order in which request inputs are validated.

Quick Links

Usage

Install

npm i @akrdevtech/lib-express-joi-validation-middleware

Peer Dependency

npm i express

Example Usage (TypeScript)

Validate body,query,cookies,headers&params at once using . Each of these may be optional as well.

import * as Joi from 'joi'
import * as express from 'express'
import { RequestValidator } from '@akrdevtech/lib-express-joi-validation-middleware';
const { validateAll } = new RequestValidator({ abortEarly: false }); // parameters of constructor is optional

const app = express()

const validateAllSchema: IValidateAllSchema = {
    body: Joi.object({
        someField: Joi.string().min(3).required(),
    }),
    query: Joi.object({
        someField: Joi.string().min(3).required(),
    }),
    cookies: Joi.object({
        someField: Joi.string().min(3).required(),
    }),
    headers: Joi.object({
        someField: Joi.string().min(3).required(),
    }),
    params: Joi.object({
        someField: Joi.string().min(3).required(),
    }),
}

app.get('/', [
  validateAll(validateAllSchema),
  (req, res) => { res.send(`Hello World!`) }
]);

// with joi validation options
app.get('/with-joi-validation-option', [
  validateAll(validateAllSchema,{ allowUnknown:true }),
  (req, res) => { res.send(`Hello World!`) }
]);

const port = 8000;
app.listen(port, () => {console.log(`⚡️ Service started : PORT → ${port}}`);

Example Usage (JavaScript)

const Joi = require('joi')
const app = require('express')()
const { RequestValidator } = require('@akrdevtech/lib-express-joi-validation-middleware');

const {
    validateAll,
    validateBody,
    validateCookies,
    validateHeaders,
    validateQuery,
    validateParams
} = RequestValidator;

const validateAllSchema: IValidateAllSchema = {
    body: Joi.object({
        someField: Joi.string().min(3).required(),
    }),
    query: Joi.object({
        someField: Joi.string().min(3).required(),
    }),
    cookies: Joi.object({
        someField: Joi.string().min(3).required(),
    }),
    headers: Joi.object({
        someField: Joi.string().min(3).required(),
    }),
    params: Joi.object({
        someField: Joi.string().min(3).required(),
    }),
}
const headerSchema = Joi.object({ someField: Joi.string().required() });
const bodySchema = Joi.object({ someField: Joi.string().required() });
const querySchema= Joi.object({ someField: Joi.string().required() });
const cookieSchema= Joi.object({ someField: Joi.string().required() });
const paramSchema= Joi.object({ someField: Joi.string().required() });

app.get('/', [
  validateAll(validateAllSchema),
  (req, res) => { res.send(`Hello World!`) }
]);

app.get('/separately', [
  validateQuery(querySchema),
  validateBody(bodySchema),
  validateCookies(cookieSchema),
  validateHeaders(headerSchema),
  validateParams(paramSchema),
  (req, res) => { res.send(`Hello World!`) }
]);

const port = 8000;
app.listen(port, () => {console.log(`⚡️ Service started : PORT → ${port}}`);

Behaviours

Validation Ordering

Validation can be performed in a specific order using standard express middleware behaviour. Pass the middleware in the desired order.

Here's an example where the order is headers, body, query:

const headerSchema = Joi.object({ someField: Joi.string().required() });
const bodySchema = Joi.object({ someField: Joi.string().required() });
const querySchema= Joi.object({ someField: Joi.string().required() });

route.get('/', [
  validateHeaders(headerSchema),
  validateBody(bodySchema),
  validateQuery(querySchema),
  routeHandler
]);

Validation Options

Validation options can be extented with Joi.ValidationOptions.

Here’s an example where the order is headers, body, query:

const bodySchema = Joi.object({ someField: Joi.string().required() });

const options = {
  abortEarly: false,
  allowUnknown: true,
}

route.get('/', [
  validateBody(bodySchema, options),
  routeHandler
]);
1.1.0

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago