From 6bef000091102cb7180f8bc6733291906dd1f7ff Mon Sep 17 00:00:00 2001 From: Septianata Rizky Pratama Date: Thu, 23 Nov 2023 20:22:13 +0700 Subject: [PATCH 1/3] chore: bump to 5.1.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3b3fe89..ef90795 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "express-joi-validation", - "version": "5.0.1", + "version": "5.1.1", "description": "validate express application inputs and parameters using joi", "main": "express-joi-validation.js", "scripts": { From 01ccb2cf9efd99ade5f71c3cd1bd9366a8c1a5ec Mon Sep 17 00:00:00 2001 From: Septianata Rizky Pratama Date: Thu, 23 Nov 2023 20:24:38 +0700 Subject: [PATCH 2/3] feat: creates a validator that generates middlewares using async --- CHANGELOG.md | 6 +++ express-joi-validation.d.ts | 6 +++ express-joi-validation.js | 80 +++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c81bf9..8d6c9a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/express-joi-validation.d.ts b/express-joi-validation.d.ts index 423f9e1..1a56fef 100644 --- a/express-joi-validation.d.ts +++ b/express-joi-validation.d.ts @@ -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" diff --git a/express-joi-validation.js b/express-joi-validation.js index 5219afe..4e8eaf5 100644 --- a/express-joi-validation.js +++ b/express-joi-validation.js @@ -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`)) + } + } + } + } + } +} From 38447d9fc00439a821cd690bb899d691f4d34742 Mon Sep 17 00:00:00 2001 From: Septianata Rizky Pratama Date: Fri, 24 Nov 2023 23:41:09 +0700 Subject: [PATCH 3/3] fix: correcting version bump to 5.1.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ef90795..36095ef 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "express-joi-validation", - "version": "5.1.1", + "version": "5.1.0", "description": "validate express application inputs and parameters using joi", "main": "express-joi-validation.js", "scripts": {