Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
## CHANGELOG
Date format is DD/MM/YYYY

## 5.1.1 (23/11/2023)
* Creates a validator that generates middlewares using Joi validateAsync feature.

## 5.0.1 (22/12/2021)
* Fix typescript compilation and bump joi and join extract type versions.

## 5.0.0 (13/10/2020)
* Drop Node.js 8 support.
* Update to use Joi v17.x.
Expand Down
6 changes: 6 additions & 0 deletions express-joi-validation.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import { ParsedQs } from 'qs'
*/
export function createValidator(cfg?: ExpressJoiConfig): ExpressJoiInstance

/**
* Creates an instance of this module that can be used to generate middleware
* @param cfg
*/
export function createValidatorAsync(cfg?: ExpressJoiConfig): ExpressJoiInstance

/**
* These are the named properties on an express.Request that this module can
* validate, e.g "body" or "query"
Expand Down
80 changes: 80 additions & 0 deletions express-joi-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,83 @@ module.exports.createValidator = function generateJoiMiddlewareInstance(cfg) {
}
}
}

module.exports.createValidatorAsync = function generateJoiMiddlewareInstance(
cfg
) {
cfg = cfg || {} // default to an empty config
// We'll return this instance of the middleware
const instance = {
response
}

Object.keys(containers).forEach(type => {
// e.g the "body" or "query" from above
const container = containers[type]

instance[type] = function(schema, opts) {
opts = opts || {} // like config, default to empty object
const computedOpts = { ...container.joi, ...cfg.joi, ...opts.joi }

return async function expressJoiValidator(req, res, next) {
try {
const value = await schema.validateAsync(req[type], computedOpts)

req[container.storageProperty] = req[type]
req[type] = value

next()
} catch (error) {
let ret = { type, error }

if (opts.passError || cfg.passError) {
ret.type = type

next(ret)
} else {
res
.status(opts.statusCode || cfg.statusCode || 400)
.end(buildErrorString(ret, `request ${type}`))
}
}
}
}
})

return instance

function response(schema, opts = {}) {
const type = 'response'

/**
* @param {import("express").Request} req
* @param {import("express").Response} res
* @param {import("express").NextFunction} next
*/
return (req, res, next) => {
const resJson = res.json.bind(res)
res.json = validateJson
next()

async function validateJson(json) {
try {
const value = await schema.validateAsync(json, opts.joi)

return resJson(value)
} catch (error) {
let ret = { type, error }

if (opts.passError || cfg.passError) {
ret.type = type

next(ret)
} else {
res
.status(opts.statusCode || cfg.statusCode || 500)
.end(buildErrorString(ret, `${type} json`))
}
}
}
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "express-joi-validation",
"version": "5.0.1",
"version": "5.1.0",
"description": "validate express application inputs and parameters using joi",
"main": "express-joi-validation.js",
"scripts": {
Expand Down