|
1 | 1 | 'use strict'; |
2 | | - |
| 2 | +const autoBind = require('auto-bind'); |
3 | 3 | const SchemaEndpointResolver = require('./utils/schemaEndpointResolver'); |
4 | | - |
5 | 4 | const InputValidationError = require('./inputValidationError'), |
6 | 5 | apiSchemaBuilder = require('api-schema-builder'); |
7 | 6 | const allowedFrameworks = ['express', 'koa', 'fastify']; |
8 | 7 |
|
9 | | -let schemas = {}; |
10 | | -let middlewareOptions; |
11 | | -let framework; |
12 | | -let schemaEndpointResolver; |
13 | | -let validationMiddleware; |
14 | | - |
15 | | -function init(swaggerPath, options) { |
16 | | - _baseInit(swaggerPath, options); |
17 | | - // build schema for requests only |
18 | | - const schemaBuilderOptions = Object.assign({}, options, { buildRequests: true, buildResponses: false }); |
19 | | - schemas = apiSchemaBuilder.buildSchemaSync(swaggerPath, schemaBuilderOptions); |
20 | | -} |
| 8 | +class Middleware { |
| 9 | + constructor(swaggerPath, options) { |
| 10 | + this.schemas = {}; |
| 11 | + this.middlewareOptions = undefined; |
| 12 | + this.framework = undefined; |
| 13 | + this.schemaEndpointResolver = undefined; |
| 14 | + this.validationMiddleware = undefined; |
| 15 | + this.InputValidationError = InputValidationError; |
| 16 | + if (swaggerPath){ |
| 17 | + this.init(swaggerPath, options); |
| 18 | + } |
| 19 | + autoBind(this); |
| 20 | + } |
21 | 21 |
|
22 | | -async function initAsync(swaggerPath, options){ |
23 | | - _baseInit(swaggerPath, options); |
24 | | - // build schema for requests only |
25 | | - const schemaBuilderOptions = Object.assign({}, options, { buildRequests: true, buildResponses: false }); |
26 | | - schemas = await apiSchemaBuilder.buildSchema(swaggerPath, schemaBuilderOptions); |
27 | | -} |
| 22 | + async initAsync(swaggerPath, options) { |
| 23 | + this._baseInit(options); |
| 24 | + // build schema for requests only |
| 25 | + const schemaBuilderOptions = Object.assign({}, options, { buildRequests: true, buildResponses: false }); |
| 26 | + this.schemas = await apiSchemaBuilder.buildSchema(swaggerPath, schemaBuilderOptions); |
| 27 | + } |
28 | 28 |
|
29 | | -function validate(...args) { |
30 | | - return validationMiddleware(...args); |
31 | | -} |
| 29 | + init (swaggerPath, options) { |
| 30 | + this._baseInit(options); |
| 31 | + // build schema for requests only |
| 32 | + const schemaBuilderOptions = Object.assign({}, options, { buildRequests: true, buildResponses: false }); |
| 33 | + this.schemas = apiSchemaBuilder.buildSchemaSync(swaggerPath, schemaBuilderOptions); |
| 34 | + } |
32 | 35 |
|
33 | | -function _baseInit(swaggerPath, options) { |
34 | | - middlewareOptions = options || {}; |
35 | | - const frameworkToLoad = allowedFrameworks.find((frameworkName) => { |
36 | | - return middlewareOptions.framework === frameworkName; |
37 | | - }); |
| 36 | + getNewMiddleware(swaggerPath, options) { |
| 37 | + return new Middleware(swaggerPath, options); |
| 38 | + } |
38 | 39 |
|
39 | | - framework = frameworkToLoad ? require(`./frameworks/${frameworkToLoad}`) : require('./frameworks/express'); |
40 | | - validationMiddleware = framework.getValidator(_validateRequest); |
41 | | - schemaEndpointResolver = new SchemaEndpointResolver(); |
42 | | -} |
| 40 | + validate(...args) { |
| 41 | + return this.validationMiddleware(...args); |
| 42 | + } |
43 | 43 |
|
44 | | -function _getContentType(headers) { |
45 | | - // This is to filter out things like charset |
46 | | - const contentType = headers['content-type']; |
47 | | - return contentType && contentType.split(';')[0].trim(); |
48 | | -} |
| 44 | + _baseInit (options) { |
| 45 | + this.middlewareOptions = options || {}; |
| 46 | + const frameworkToLoad = allowedFrameworks.find((frameworkName) => { |
| 47 | + return this.middlewareOptions.framework === frameworkName; |
| 48 | + }); |
49 | 49 |
|
50 | | -function _validateRequest(requestOptions) { |
51 | | - const paramValidationErrors = _validateParams(requestOptions); |
52 | | - const bodyValidationErrors = _validateBody(requestOptions); |
| 50 | + this.framework = frameworkToLoad ? require(`./frameworks/${frameworkToLoad}`) : require('./frameworks/express'); |
| 51 | + this.validationMiddleware = this.framework.getValidator((requestOptions) => this._validateRequest(requestOptions)); |
| 52 | + this.schemaEndpointResolver = new SchemaEndpointResolver(); |
| 53 | + } |
53 | 54 |
|
54 | | - const errors = paramValidationErrors.concat(bodyValidationErrors); |
| 55 | + _getContentType (headers) { |
| 56 | + // This is to filter out things like charset |
| 57 | + const contentType = headers['content-type']; |
| 58 | + return contentType && contentType.split(';')[0].trim(); |
| 59 | + } |
55 | 60 |
|
56 | | - if (errors.length) { |
57 | | - let error; |
| 61 | + _validateRequest (requestOptions) { |
| 62 | + const paramValidationErrors = this._validateParams(requestOptions); |
| 63 | + const bodyValidationErrors = this._validateBody(requestOptions); |
58 | 64 |
|
59 | | - if (middlewareOptions.errorFormatter) { |
60 | | - error = middlewareOptions.errorFormatter(errors, middlewareOptions); |
61 | | - } else { |
62 | | - error = new InputValidationError(errors, |
63 | | - { |
64 | | - beautifyErrors: middlewareOptions.beautifyErrors, |
65 | | - firstError: middlewareOptions.firstError |
66 | | - }); |
67 | | - } |
| 65 | + const errors = paramValidationErrors.concat(bodyValidationErrors); |
68 | 66 |
|
69 | | - return error; |
70 | | - } |
71 | | -} |
| 67 | + if (errors.length) { |
| 68 | + let error; |
72 | 69 |
|
73 | | -function _validateBody(requestOptions) { |
74 | | - const { body, path } = requestOptions; |
75 | | - const method = requestOptions.method.toLowerCase(); |
76 | | - const contentType = _getContentType(requestOptions.headers); |
77 | | - const methodSchema = schemaEndpointResolver.getMethodSchema(schemas, path, method) || {}; |
| 70 | + if (this.middlewareOptions.errorFormatter) { |
| 71 | + error = this.middlewareOptions.errorFormatter(errors, this.middlewareOptions); |
| 72 | + } else { |
| 73 | + error = new InputValidationError(errors, |
| 74 | + { |
| 75 | + beautifyErrors: this.middlewareOptions.beautifyErrors, |
| 76 | + firstError: this.middlewareOptions.firstError |
| 77 | + }); |
| 78 | + } |
78 | 79 |
|
79 | | - if (methodSchema.body) { |
80 | | - const validator = methodSchema.body[contentType] || methodSchema.body; |
81 | | - if (!validator.validate(body)) { |
82 | | - return validator.errors || []; |
| 80 | + return error; |
83 | 81 | } |
84 | 82 | } |
85 | 83 |
|
86 | | - return []; |
87 | | -} |
88 | | - |
89 | | -function _validateParams(requestOptions) { |
90 | | - const { headers, params: pathParams, query, files, path } = requestOptions; |
91 | | - const method = requestOptions.method.toLowerCase(); |
| 84 | + _validateBody(requestOptions) { |
| 85 | + const { body, path } = requestOptions; |
| 86 | + const method = requestOptions.method.toLowerCase(); |
| 87 | + const contentType = this._getContentType(requestOptions.headers); |
| 88 | + const methodSchema = this.schemaEndpointResolver.getMethodSchema(this.schemas, path, method) || {}; |
| 89 | + |
| 90 | + if (methodSchema.body) { |
| 91 | + const validator = methodSchema.body[contentType] || methodSchema.body; |
| 92 | + if (!validator.validate(body)) { |
| 93 | + return validator.errors || []; |
| 94 | + } |
| 95 | + } |
92 | 96 |
|
93 | | - const methodSchema = schemaEndpointResolver.getMethodSchema(schemas, path, method); |
94 | | - if (methodSchema && methodSchema.parameters && !methodSchema.parameters.validate({ query: query, headers: headers, path: pathParams, files: files })) { |
95 | | - return methodSchema.parameters.errors || []; |
| 97 | + return []; |
96 | 98 | } |
97 | 99 |
|
98 | | - return []; |
| 100 | + _validateParams (requestOptions) { |
| 101 | + const { headers, params: pathParams, query, files, path } = requestOptions; |
| 102 | + const method = requestOptions.method.toLowerCase(); |
| 103 | + |
| 104 | + const methodSchema = this.schemaEndpointResolver.getMethodSchema(this.schemas, path, method); |
| 105 | + if (methodSchema && methodSchema.parameters && !methodSchema.parameters.validate({ query: query, headers: headers, path: pathParams, files: files })) { |
| 106 | + return methodSchema.parameters.errors || []; |
| 107 | + } |
| 108 | + |
| 109 | + return []; |
| 110 | + } |
99 | 111 | } |
100 | 112 |
|
101 | | -module.exports = { |
102 | | - init, |
103 | | - initAsync, |
104 | | - validate, |
105 | | - InputValidationError |
106 | | -}; |
| 113 | +module.exports = new Middleware(); |
0 commit comments