• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PayU / openapi-validator-middleware / 3632820362

pending completion
3632820362

Pull #188

github

GitHub
Merge 6a1acf7ae into 1effab313
Pull Request #188: Bump qs from 6.5.2 to 6.5.3

51 of 54 branches covered (94.44%)

Branch coverage included in aggregate %.

87 of 87 relevant lines covered (100.0%)

105.44 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.59
/src/middleware.js
1
'use strict';
2
const autoBind = require('auto-bind');
5✔
3
const SchemaEndpointResolver = require('./utils/schemaEndpointResolver');
5✔
4
const InputValidationError = require('./inputValidationError'),
5✔
5
    apiSchemaBuilder = require('api-schema-builder');
5✔
6
const allowedFrameworks = ['express', 'koa', 'fastify'];
5✔
7

8
class Middleware {
9
    constructor(swaggerPath, options) {
10
        this.schemas = {};
9✔
11
        this.middlewareOptions = undefined;
9✔
12
        this.framework = undefined;
9✔
13
        this.schemaEndpointResolver = undefined;
9✔
14
        this.validationMiddleware = undefined;
9✔
15
        this.InputValidationError = InputValidationError;
9✔
16
        if (swaggerPath){
9✔
17
            this.init(swaggerPath, options);
4✔
18
        }
19
        autoBind(this);
9✔
20
    }
21

22
    async initAsync(swaggerPath, options) {
23
        this._baseInit(options);
2✔
24
        // build schema for requests only
25
        const schemaBuilderOptions = Object.assign({}, options, { buildRequests: true, buildResponses: false });
2✔
26
        this.schemas = await apiSchemaBuilder.buildSchema(swaggerPath, schemaBuilderOptions);
2✔
27
    }
28

29
    init (swaggerPath, options) {
30
        this._baseInit(options);
30✔
31
        // build schema for requests only
32
        const schemaBuilderOptions = Object.assign({}, options, { buildRequests: true, buildResponses: false });
30✔
33
        this.schemas = apiSchemaBuilder.buildSchemaSync(swaggerPath, schemaBuilderOptions);
30✔
34
    }
35

36
    getNewMiddleware(swaggerPath, options) {
37
        return new Middleware(swaggerPath, options);
4✔
38
    }
39

40
    validate(...args) {
41
        return this.validationMiddleware(...args);
288✔
42
    }
43

44
    _baseInit (options) {
45
        this.middlewareOptions = options || {};
32✔
46
        const frameworkToLoad = allowedFrameworks.find((frameworkName) => {
32✔
47
            return this.middlewareOptions.framework === frameworkName;
87✔
48
        });
49

50
        this.framework = frameworkToLoad ? require(`./frameworks/${frameworkToLoad}`) : require('./frameworks/express');
32✔
51
        this.validationMiddleware = this.framework.getValidator((requestOptions) => this._validateRequest(requestOptions));
288✔
52
        this.schemaEndpointResolver = new SchemaEndpointResolver();
32✔
53
    }
54

55
    _getContentType (headers) {
56
        // This is to filter out things like charset
57
        const contentType = headers['content-type'];
288✔
58
        return contentType && contentType.split(';')[0].trim();
288✔
59
    }
60

61
    _validateRequest (requestOptions) {
62
        const paramValidationErrors = this._validateParams(requestOptions);
288✔
63
        const bodyValidationErrors = this._validateBody(requestOptions);
288✔
64

65
        const errors = paramValidationErrors.concat(bodyValidationErrors);
288✔
66

67
        if (errors.length) {
288✔
68
            let error;
69

70
            if (this.middlewareOptions.errorFormatter) {
226✔
71
                error = this.middlewareOptions.errorFormatter(errors, this.middlewareOptions);
1✔
72
            } else {
73
                error = new InputValidationError(errors,
225✔
74
                    {
75
                        beautifyErrors: this.middlewareOptions.beautifyErrors,
76
                        firstError: this.middlewareOptions.firstError
77
                    });
78
            }
79

80
            return error;
226✔
81
        }
82
    }
83

84
    _validateBody(requestOptions) {
85
        const { body, path } = requestOptions;
288✔
86
        const method = requestOptions.method.toLowerCase();
288✔
87
        const contentType = this._getContentType(requestOptions.headers);
288✔
88
        const methodSchema = this.schemaEndpointResolver.getMethodSchema(this.schemas, path, method) || {};
288✔
89

90
        if (methodSchema.body) {
288✔
91
            const validator = methodSchema.body[contentType] || methodSchema.body;
175✔
92
            if (!validator.validate(body)) {
175✔
93
                return validator.errors || [];
138!
94
            }
95
        }
96

97
        return [];
150✔
98
    }
99

100
    _validateParams (requestOptions) {
101
        const { headers, params: pathParams, query, files, path } = requestOptions;
288✔
102
        const method = requestOptions.method.toLowerCase();
288✔
103

104
        const methodSchema = this.schemaEndpointResolver.getMethodSchema(this.schemas, path, method);
288✔
105
        if (methodSchema && methodSchema.parameters && !methodSchema.parameters.validate({ query: query, headers: headers, path: pathParams, files: files })) {
288✔
106
            return methodSchema.parameters.errors || [];
95!
107
        }
108

109
        return [];
193✔
110
    }
111
}
112

113
module.exports = new Middleware();
5✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc