all files / swagger-node-runner/fittings/ swagger_validator.js

100% Statements 13/13
100% Branches 2/2
100% Functions 2/2
100% Lines 13/13
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        13×   13×   81×             81× 81× 12× 12× 12×     81×      
'use strict';
 
var debug = require('debug')('swagger:swagger_validator');
var _ = require('lodash');
var util = require('util');
 
module.exports = function create(fittingDef, bagpipes) {
 
  debug('config: %j', fittingDef);
 
  return function swagger_validator(context, cb) {
 
    debug('exec');
 
    // todo: add support for validating accept header against produces declarations
    // see: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
    //var accept = req.headers['accept'];
    //var produces = _.union(operation.api.definition.produces, operation.definition.produces);
 
    var validateResult = context.request.swagger.operation.validateRequest(context.request);
    if (validateResult.errors.length > 0) {
      var error = new Error('Validation errors');
      error.statusCode = 400;
      error.errors = validateResult.errors;
    }
 
    cb(error);
  }
};