swagger-express-middleware icon indicating copy to clipboard operation
swagger-express-middleware copied to clipboard

Validating Swagger document ?

Open NikhilNanjappa-zz opened this issue 7 years ago • 1 comments

In the README, it mentions that the package uses Swagger-Parser to parse, validate, and dereference Swagger files.

So, I'm assuming swagger-express-middleware can validate the swagger document (not the request). Same as what a swagger-parser does. eg.

const SwaggerParser = require('swagger-parser');
SwaggerParser.validate("my-api.yaml"); // does this functionality exist in 'swagger-express-middleware' ?

My current implementation is as follows:

createSwaggerMiddleware(swaggerDoc, app, (middlewareError, middleware) => {
    app.use(middleware.metadata());
    app.use(middleware.parseRequest());
    app.use(middleware.validateRequest());
    // app.use(middleware.validate(swaggerDoc)); --> tried my luck but this doesnt work

    app.use((err, req, res, next) => {
       console.log(err);
    });
});

Could you please guide me on how to validate a swagger spec using swagger-express-middleware?

NikhilNanjappa-zz avatar Oct 17 '18 15:10 NikhilNanjappa-zz

Good point. Swagger-Express-Middleware currently calls Swagger-Parser's dereference() method rather than the validate() method. There are a couple different ways this could be addressed:

Option 1: I could change the dereference() call to validate(). This is the easiest option, but it's a breaking change and some people may not like it. Perhaps it could be an optional feature that defaults to false.

Option 2: Swagger-Express-Middleware could expose its own validate() method, similar to your code example above. This option is more explicit and non-breaking, but it's a bit more complicated and makes the API less intuitive.

JamesMessinger avatar Oct 18 '18 09:10 JamesMessinger