Skip to content

Response validation

Compare
Choose a tag to compare
@theganyo theganyo released this 04 Nov 01:02
· 132 commits to master since this release

New Features

Response validation

Response validation is now enabled as an event listener. No longer will turning on response validation overwrite your API responses - so now you may turn it on anytime and log the errors however you'd like without affecting your API clients. To catch validation errors, just listen for the 'responseValidationError' on the runner you create.

For example, if you have a swagger-node connect, express, restify, or hapi application, your app.js would look something like this:

SwaggerExpress.create(config, function(err, swaggerExpress) {
  if (err) { throw err; }

  // install middleware
  swaggerExpress.register(app);

  // install response validation listener (this will only be called if there actually are any errors or warnings)
  swaggerExpress.runner.on('responseValidationError', function(validationResponse, request, response) {
    // log your validationResponse here...
    console.error(validationResponse.errors);
  });

  var port = process.env.PORT || 10010;
  app.listen(port);

If you have a swagger-node sails application, the bottom of your app.js would look like this:

  sails.lift(rc('sails'), function(err, server) {

    sails.hooks['swagger-sails-hook'].runner.on('responseValidationError', function(validationResponse, request, response) {
      // log your validationResponse here...
      console.error(validationResponse.errors);
    });
  });

Notes:

  • The validationResponse returned is the same format as Sway returns for its validateResponse function - (docs here).
  • If there are no responseValidationError handlers, the response will not be validated, so there is no overhead.
  • The validateResponse configuration property on the swagger_validator fitting is not longer used or valid.