/
openapi.request.validator.ts
335 lines (293 loc) 路 9.51 KB
/
openapi.request.validator.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import * as Ajv from 'ajv';
import { validationError } from '../errors';
import ono from 'ono';
import * as draftSchema from 'ajv/lib/refs/json-schema-draft-04.json';
const TYPE_JSON = 'application/json';
const maxInt32 = 2 ** 31 - 1;
const minInt32 = (-2) ** 31;
const maxInt64 = 2 ** 63 - 1;
const minInt64 = (-2) ** 63;
const maxFloat = (2 - 2 ** -23) * 2 ** 127;
const minFloat = 2 ** -126;
const alwaysTrue = () => true;
const base64regExp = /^[A-Za-z0-9+/]*(=|==)?$/;
const formats = {
int32: {
validate: i => Number.isInteger(i) && i <= maxInt32 && i >= minInt32,
type: 'number',
},
int64: {
validate: i => Number.isInteger(i) && i <= maxInt64 && i >= minInt64,
type: 'number',
},
float: {
validate: i => typeof i === 'number' && (i <= maxFloat && i >= minFloat),
type: 'number',
},
double: {
validate: i => typeof i === 'number',
type: 'number',
},
byte: b => b.length % 4 === 0 && base64regExp.test(b),
binary: alwaysTrue,
password: alwaysTrue,
};
export class RequestValidator {
private _middlewareCache;
private _apiDocs;
private ajv;
constructor(apiDocs, options = {}) {
this._middlewareCache = {};
this._apiDocs = apiDocs;
this.ajv = this.initAjv(options);
}
initAjv(options) {
const ajv = new Ajv({
...options,
formats: { ...formats, ...options.formats },
schemaId: 'auto',
allErrors: true,
meta: draftSchema,
});
ajv.removeKeyword('propertyNames');
ajv.removeKeyword('contains');
ajv.removeKeyword('const');
/**
* Remove readOnly property in requestBody when validate.
* If you want validate response, then need secondary Ajv without modifying this keyword
* You can probably change this rule so that can't delete readOnly property in response
*/
ajv.removeKeyword('readOnly');
ajv.addKeyword('readOnly', {
modifying: true,
compile: sch => {
if (sch) {
return (data, path, obj, propName) => {
delete obj[propName];
return true;
};
}
return () => true;
},
});
if (this._apiDocs.components.schemas) {
Object.entries(this._apiDocs.components.schemas).forEach(
([id, schema]: any[]) => {
ajv.addSchema(schema, `#/components/schemas/${id}`);
},
);
}
if (this._apiDocs.components.requestBodies) {
Object.entries(this._apiDocs.components.requestBodies).forEach(
([id, schema]: any[]) => {
// TODO add support for content all content types
ajv.addSchema(
schema.content[TYPE_JSON].schema,
`#/components/requestBodies/${id}`,
);
},
);
}
return ajv;
}
validate(req, res, next) {
if (!req.openapi) {
// this path was not found in open api and
// this path is not defined under an openapi base path
// skip it
return next();
}
const path = req.openapi.expressRoute;
if (!path) {
const message = 'not found';
const err = validationError(404, req.path, message);
throw ono(err, message);
}
const pathSchema = req.openapi.schema;
if (!pathSchema) {
// add openapi metadata to make this case more clear
// its not obvious that missig schema means methodNotAllowed
const message = `${req.method} method not allowed`;
const err = validationError(405, req.path, message);
throw ono(err, message);
}
// cache middleware by combining method, path, and contentType
const contentType = this.extractContentType(req);
const key = `${req.method}-${req.path}-${contentType}`;
if (!this._middlewareCache[key]) {
this._middlewareCache[key] = this.buildMiddleware(
path,
pathSchema,
contentType,
);
}
return this._middlewareCache[key](req, res, next);
}
private extractContentType(req) {
let contentType = req.headers['content-type'] || 'not_provided';
let end = contentType.indexOf(';');
end = end === -1 ? contentType.length : end;
if (contentType) {
return contentType.substring(0, end);
}
return contentType;
}
private buildMiddleware(path, pathSchema, contentType) {
const parameters = this.parametersToSchema(path, pathSchema.parameters);
const requestBody = pathSchema.requestBody;
let body = this.requestBodyToSchema(path, contentType, requestBody);
let requiredAdds = requestBody && requestBody.required ? ['body'] : [];
const schema = {
// $schema: "http://json-schema.org/draft-04/schema#",
required: ['query', 'headers', 'params'].concat(requiredAdds),
properties: {
body,
...parameters.schema,
},
};
const validator = this.ajv.compile(schema);
return (req, res, next) => {
const shouldUpdatePathParams =
Object.keys(req.openapi.pathParams).length > 0;
if (shouldUpdatePathParams) {
req.params = req.openapi.pathParams || req.params;
}
req.schema = schema;
/**
* support json in request params, query, headers and cookies
* like this filter={"type":"t-shirt","color":"blue"}
*
* https://swagger.io/docs/specification/describing-parameters/#schema-vs-content
*/
parameters.parseJson.forEach(item => {
if (req[item.reqField] && req[item.reqField][item.name]) {
req[item.reqField][item.name] = JSON.parse(
req[item.reqField][item.name],
);
}
});
/**
* array deserialization
* filter=foo,bar,baz
* filter=foo|bar|baz
* filter=foo%20bar%20baz
*/
parameters.parseArray.forEach(item => {
if (req[item.reqField] && req[item.reqField][item.name]) {
req[item.reqField][item.name] = req[item.reqField][item.name].split(
item.delimiter,
);
}
});
const reqToValidate = {
...req,
cookies: req.cookies
? { ...req.cookies, ...req.signedCookies }
: undefined,
};
const valid = validator(reqToValidate);
// save errors, Ajv overwrites errors on each validation call (race condition?)
// TODO look into Ajv async errors plugins
const errors = [...(validator.errors || [])];
if (valid) {
next();
} else {
if (errors.length > 0) {
const error = {
status: 400,
errors: errors.map(e => ({
path:
(e.params && e.params.missingProperty) ||
e.dataPath ||
e.schemaPath,
message: e.message,
errorCode: `${e.keyword}.openapi.validation`,
})),
};
const message = this.ajv.errorsText(errors, { dataVar: 'request' });
throw ono(error, message);
}
}
};
}
private requestBodyToSchema(path, contentType, requestBody: any = {}) {
if (requestBody.content) {
const content = requestBody.content[contentType];
if (!content) {
const message = `unsupported media type ${contentType}`;
const err = validationError(415, path, message);
throw ono(err, message);
}
return content.schema || {};
}
return {};
}
private parametersToSchema(path, parameters = []) {
const schema = { query: {}, headers: {}, params: {}, cookies: {} };
const reqFields = {
query: 'query',
header: 'headers',
path: 'params',
cookie: 'cookies',
};
const arrayDelimiter = {
form: ',',
spaceDelimited: ' ',
pipeDelimited: '|',
};
const parseJson = [];
const parseArray = [];
parameters.forEach(parameter => {
if (parameter.hasOwnProperty('$ref')) {
const id = parameter.$ref.replace(/^.+\//i, '');
parameter = this._apiDocs.components.parameters[id];
}
const $in = parameter.in;
const name =
$in === 'header' ? parameter.name.toLowerCase() : parameter.name;
const reqField = reqFields[$in];
if (!reqField) {
const message = `Parameter 'in' has incorrect value '${$in}' for [${parameter.name}]`;
const err = validationError(400, path, message);
throw ono(err, message);
}
let parameterSchema = parameter.schema;
if (parameter.content && parameter.content[TYPE_JSON]) {
parameterSchema = parameter.content[TYPE_JSON].schema;
parseJson.push({ name, reqField });
}
if (!parameterSchema) {
const message = `Not available parameter 'schema' or 'content' for [${parameter.name}]`;
const err = validationError(400, path, message);
throw ono(err, message);
}
if (
parameter.schema &&
parameter.schema.type === 'array' &&
!parameter.explode
) {
const delimiter = arrayDelimiter[parameter.style];
if (!delimiter) {
const message = `Parameter 'style' has incorrect value '${parameter.style}' for [${parameter.name}]`;
const err = validationError(400, path, message);
throw ono(err, message);
}
parseArray.push({ name, reqField, delimiter });
}
if (!schema[reqField].properties) {
schema[reqField] = {
type: 'object',
properties: {},
};
}
schema[reqField].properties[name] = parameterSchema;
if (parameter.required) {
if (!schema[reqField].required) {
schema[reqField].required = [];
}
schema[reqField].required.push(name);
}
});
return { schema, parseJson, parseArray };
}
}