Skip to content

Commit

Permalink
Merge 1305882 into 9a18c94
Browse files Browse the repository at this point in the history
  • Loading branch information
scucchiero committed Jul 22, 2020
2 parents 9a18c94 + 1305882 commit 756820c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
4 changes: 3 additions & 1 deletion express-joi-validation.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as Joi from '@hapi/joi'
import * as express from 'express'
import { IncomingHttpHeaders } from 'http'
import { ParsedQs } from 'qs'

/**
* Creates an instance of this module that can be used to generate middleware
Expand Down Expand Up @@ -43,7 +44,7 @@ export type ValidatedRequestSchema = Record<ContainerTypes, any>
export interface ValidatedRequest<T extends ValidatedRequestSchema>
extends express.Request {
body: T[ContainerTypes.Body]
query: T[ContainerTypes.Query]
query: T[ContainerTypes.Query] & ParsedQs
headers: T[ContainerTypes.Headers]
params: T[ContainerTypes.Params]
}
Expand Down Expand Up @@ -77,6 +78,7 @@ export interface ValidatedRequestWithRawInputsAndFields<
export interface ExpressJoiConfig {
statusCode?: number
passError?: boolean
joi?: object
}

/**
Expand Down
4 changes: 2 additions & 2 deletions express-joi-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ module.exports.createValidator = function generateJoiMiddlewareInstance(cfg) {

instance[type] = function(schema, opts) {
opts = opts || {} // like config, default to empty object

const computedOpts = { ...container.joi, ...cfg.joi, ...opts.joi }
return function expressJoiValidator(req, res, next) {
const ret = schema.validate(req[type], opts.joi || container.joi)
const ret = schema.validate(req[type], computedOpts)

if (!ret.error) {
req[container.storageProperty] = req[type]
Expand Down
43 changes: 43 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ describe('express joi', function() {
}
)

app.post(
'/global-joi-config',
require('body-parser').json(),
middleware,
(req, res) => {
expect(req.body).to.exist
expect(req.originalBody).to.exist

expect(req.originalBody.known).to.exist
expect(req.originalBody.known).to.exist

expect(req.originalBody.unknown).to.exist
expect(req.originalBody.unknown).to.exist

res.end('ok')
}
)

app.post(
'/fields-check',
require('express-formidable')(),
Expand Down Expand Up @@ -270,4 +288,29 @@ describe('express joi', function() {
})
})
})

describe('#joiGlobalOptionMerging.', function() {
it('should return a 200 since our body is valid', function(done) {
const mod = require('./express-joi-validation.js').createValidator({
passError: true,
joi: {
allowUnknown: true
}
})
const schema = Joi.object({
known: Joi.boolean().required()
})

const mw = mod.body(schema)

getRequester(mw)
.post('/global-joi-config')
.send({
known: true,
unknown: true
})
.expect(200)
.end(done)
})
})
})
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "express-joi-validation",
"version": "4.0.3",
"version": "4.0.4-beta.0",
"description": "validate express application inputs and parameters using joi",
"main": "express-joi-validation.js",
"scripts": {
Expand Down Expand Up @@ -34,6 +34,7 @@
"@types/express-formidable": "~1.0.4",
"@types/hapi__joi": "~16.0.3",
"@types/node": "~10.14.18",
"@types/qs": "~6.9.3",
"body-parser": "~1.18.3",
"chai": "~3.5.0",
"clear-require": "~2.0.0",
Expand All @@ -50,6 +51,7 @@
"nyc": "~14.1.1",
"prettier": "~1.14.3",
"proxyquire": "~1.7.11",
"qs": "~6.9.4",
"sinon": "~1.17.7",
"supertest": "~3.0.0",
"typescript": "~3.5.2"
Expand Down

0 comments on commit 756820c

Please sign in to comment.