-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
66 lines (55 loc) · 2.02 KB
/
index.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
'use strict'
const Ajv = require('ajv')
const Errors = require('throwable-http-errors')
/**
* Returns a middleware with compiled ajv validators
* @param {Object} routeSchema An object with ajv schemas {body: Schema1, query: Schema2, prams: Schema3}
*/
const validateRoute = routeSchema => {
let ajv = null
const validators = {}
// Compiling query schema beforehand
if (Object.prototype.hasOwnProperty.call(routeSchema, 'query')) {
ajv = new Ajv()
validators.query = ajv.compile(routeSchema.query)
}
// Compiling params schema beforehand
if (Object.prototype.hasOwnProperty.call(routeSchema, 'params')) {
// We coerce types on params because they cann only be strings
// since they are part of an url
ajv = new Ajv({ coerceTypes: true })
validators.params = ajv.compile(routeSchema.params)
}
// Compiling body schema beforehand
if (Object.prototype.hasOwnProperty.call(routeSchema, 'body')) {
ajv = new Ajv()
validators.body = ajv.compile(routeSchema.body)
}
// The actual middleware that gets loaded by express
// has already-compiled validators
return (req, res, next) => {
let validation = null
if (Object.prototype.hasOwnProperty.call(validators, 'params')) {
validation = ajv.validate(routeSchema.params, req.params)
if (!validation) {
return next(new Errors.BadRequest(`Request url parameters validation failed: ${ajv.errorsText()}`))
}
}
if (Object.prototype.hasOwnProperty.call(validators, 'query')) {
validation = ajv.validate(routeSchema.query, req.query)
if (!validation) {
return next(new Errors.BadRequest(`Request query validation failed: ${ajv.errorsText()}`))
}
}
if (Object.prototype.hasOwnProperty.call(validators, 'body')) {
validation = ajv.validate(routeSchema.body, req.body)
if (!validation) {
return next(new Errors.BadRequest(`Request body validation failed: ${ajv.errorsText()}`))
}
}
return next()
}
}
module.exports = {
validateRoute: validateRoute
}