-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
172 lines (144 loc) · 5.14 KB
/
middleware.ts
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { RequestHandler as Middleware } from 'express';
import { errorResponse, BmrToCalculate, RequiredKeys, OptionalKeys } from './src/types';
import { Validatable, validate } from './src/services/validators';
import { activityValues } from './src/services/values/activityValues';
import { sexValues } from './src/services/values/sexValues';
import { inImperialValues } from './src/services/values/inImperialValues';
export const validateParamsBmr: Middleware = (req, res, next) => {
const allowedParams = ["height", "weight", "age", "sex", "inImperial"];
const validity = validateParams(allowedParams, req.query, req.originalUrl);
if(validity !== true){
res.status(validity.errorCode);
res.send(validity);
return;
}
const heightValidate: Validatable = {
valueName: allowedParams[0],
value: +req.query.height,
require: true,
min: 0,
max: 250
}
const weightValidate: Validatable = {
valueName: allowedParams[1],
value: +req.query.weight,
require: true,
min: 0,
max: 400
}
const ageValidate: Validatable = {
valueName: allowedParams[2],
value: +req.query.age,
require: true,
min: 0,
max: 130
}
const sexValidate: Validatable = {
valueName: allowedParams[3],
value: <string>req.query.sex,
require: true,
allowedValues: Object.keys(sexValues)
}
const inImperial: Validatable = {
valueName: allowedParams[4],
value: <string>req.query.inImperial,
require: true,
allowedValues: Object.keys(inImperialValues)
}
if(!validate(heightValidate)[0] || !validate(weightValidate)[0] || !validate(ageValidate)[0] || !validate(sexValidate)[0] || !validate(inImperial)[0]) {
const message = validate(heightValidate)[1] + validate(weightValidate)[1] + validate(ageValidate)[1] + validate(sexValidate)[1] + validate(inImperial)[1]
const errorMessage: errorResponse = {
errorCode: 400,
path: req.originalUrl,
message: message
}
res.status(400).send(errorMessage);
return;
}
next();
}
export const validateParamsTmr: Middleware = (req, res, next) => {
const allowedParams = ["height", "weight", "age", "sex", "activity", "inImperial"];
const validity = validateParams(allowedParams, req.query, req.originalUrl);
if(validity !== true){
res.status(validity.errorCode);
res.send(validity);
return;
}
const heightValidate: Validatable = {
valueName: allowedParams[0],
value: +req.query.height,
require: true,
min: 0,
max: 250
}
const weightValidate: Validatable = {
valueName: allowedParams[1],
value: +req.query.weight,
require: true,
min: 0,
max: 400
}
const ageValidate: Validatable = {
valueName: allowedParams[2],
value: +req.query.age,
require: true,
min: 0,
max: 130
}
const sexValidate: Validatable = {
valueName: allowedParams[3],
value: <string>req.query.sex,
require: true,
allowedValues: Object.keys(sexValues)
}
const activityValidate: Validatable = {
valueName: allowedParams[4],
value: <string>req.query.activity,
require: true,
allowedValues: Object.keys(activityValues)
}
const inImperial: Validatable = {
valueName: allowedParams[4],
value: <string>req.query.inImperial,
require: true,
allowedValues: Object.keys(inImperialValues)
}
if(!validate(heightValidate)[0] || !validate(weightValidate)[0] || !validate(ageValidate)[0] || !validate(sexValidate)[0] || !validate(activityValidate)[0] || !validate(inImperial)[0]) {
const message = validate(heightValidate)[1] + validate(weightValidate)[1] + validate(ageValidate)[1] + validate(sexValidate)[1] + validate(activityValidate)[1] + validate(inImperial)[1];
const errorMessage: errorResponse = {
errorCode: 400,
path: req.originalUrl,
message: message
}
res.status(400).send(errorMessage);
return;
}
next();
}
export const validateNoParams: Middleware = (req, res, next) => {
const allowedParams = [];
const validity = validateParams(allowedParams, req.query, req.originalUrl, "Bad request. No params should be provided");
if(validity !== true){
res.status(validity.errorCode);
res.send(validity);
return;
}
next();
}
function validateParams(allowedParams: string[], paramsInRequest: Object, originalUrl: string, customErrorMessage?: string): true|errorResponse{
const errorMessage: errorResponse = {
errorCode: 400,
path: originalUrl,
message: customErrorMessage ? customErrorMessage : "Bad request"
}
if(Object.keys(paramsInRequest).length != allowedParams.length){
return errorMessage;
}
for(const param in allowedParams){
if(!(allowedParams[param] in paramsInRequest)) {
return errorMessage;
}
}
return true;
}