var debug = require('debug')('domain:validator'),
  _ = require('lodash'),
  tv4Module = require('tv4'),
  ValidationError = require('./errors/validationError');

/**
 * Returns a validator function.
 * @param {Object} options The options object.
 * @param {Object} schema  The schema object.
 * @returns {Function}
 */
function getValidator (options, schema) {
  options = options || {};
  options.schemas = options.schemas || {};
  options.formats = options.formats || {};

  if (!schema || !_.isObject(schema)) {
    var err = new Error('Please pass a valid schema!');
    debug(err);
    throw err;
  }

  var tv4 = tv4Module.freshApi();

  _.each(options.schemas, function (v, k) {
    tv4.addSchema(k, v);
  });

  tv4.addFormat(options.formats);

  return function (data) {
    var validation = tv4.validateMultiple(data, schema);

    if (validation.missing.length > 0) {
      var missingString = validation.missing[0];

      for (var m = 1, lenM = validation.missing.length; m < lenM; m++) {
        missingString += ', ' + validation.missing[m];
      }

      var err = new Error('Validation schema(s) "' + missingString + '" missing!');
      debug(err);
      return err;
    }

    if (!validation.valid) {
      var errors = validation.errors;
      var firstError = validation.errors[0];
      if (firstError.message.indexOf('not match any schemas') >= 0 && firstError.subErrors && firstError.subErrors.length > 0) {
        firstError = firstError.subErrors[0];
        errors = firstError.subErrors;
      }
      return new ValidationError(firstError.dataPath + ' => ' + firstError.message, errors);
    }

    return null;
  };
}

module.exports = getValidator;

@@ -1256,55 +1256,26 @@
 rors
-;%0A      var firstError = validation.errors%5B0%5D;%0A
+.map(error =%3E %7B%0A  
     
@@ -1280,22 +1280,17 @@
     if (
-firstE
+e
 rror.mes
@@ -1331,30 +1331,25 @@
 s') %3E= 0 && 
-firstE
+e
 rror.subErro
@@ -1354,22 +1354,17 @@
 rors && 
-firstE
+e
 rror.sub
@@ -1396,27 +1396,18 @@
     
-firstError = firstE
+  return e
 rror
@@ -1433,46 +1433,95 @@
     
-errors = firstError.subErrors;%0A      %7D
+%7D else %7B%0A          return error;%0A        %7D%0A      %7D);%0A      var firstError = errors%5B0%5D;%0A
 %0A   

Replace error for schema missmatch with first sub-error instead of replacing all errors with first error's sub-errors.