-
Notifications
You must be signed in to change notification settings - Fork 20
/
express-joi-validation.js
121 lines (110 loc) · 2.95 KB
/
express-joi-validation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
'use strict'
// These represent the incoming data containers that we might need to validate
const containers = {
query: {
storageProperty: 'originalQuery',
joi: {
convert: true,
allowUnknown: false,
abortEarly: false
}
},
// For use with body-parser
body: {
storageProperty: 'originalBody',
joi: {
convert: true,
allowUnknown: false,
abortEarly: false
}
},
headers: {
storageProperty: 'originalHeaders',
joi: {
convert: true,
allowUnknown: true,
stripUnknown: false,
abortEarly: false
}
},
// URL params e.g "/users/:userId"
params: {
storageProperty: 'originalParams',
joi: {
convert: true,
allowUnknown: false,
abortEarly: false
}
},
// For use with express-formidable or similar POST body parser for forms
fields: {
storageProperty: 'originalFields',
joi: {
convert: true,
allowUnknown: false,
abortEarly: false
}
}
}
function buildErrorString(err, container) {
let ret = `Error validating ${container}.`
let details = err.error.details
for (let i = 0; i < details.length; i++) {
ret += ` ${details[i].message}.`
}
return ret
}
module.exports.createValidator = 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 function expressJoiValidator(req, res, next) {
const ret = schema.validate(req[type], computedOpts)
if (!ret.error) {
req[container.storageProperty] = req[type]
req[type] = ret.value
next()
} else 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'
return (req, res, next) => {
const resJson = res.json.bind(res)
res.json = validateJson
next()
function validateJson(json) {
const ret = schema.validate(json, opts.joi)
const { error, value } = ret
if (!error) {
// return res.json ret to retain express compatibility
return resJson(value)
} else if (opts.passError || cfg.passError) {
ret.type = type
next(ret)
} else {
res
.status(opts.statusCode || cfg.statusCode || 500)
.end(buildErrorString(ret, `${type} json`))
}
}
}
}
}