<commit_before>/*!
 * Expose `invokeValidations`.
 */
module.exports = Validate;

/*!
 * Module dependencies.
 */
var _ = require('lodash');
var Validator = require('validator');
var util = require('util');
var helper = require('./helper');
var debug = require('debug')('poplar:validation');

/**
 * @class A wrapper to collect validation errors.
 *
 * @constructor
 * Create a new `ValidationError` with the given `options`.
 */
function ValidationError() {
  this._errors = {};
};

/**
 * add a new validation error
 * {
 *   email: { isEmail: 'not a valid email' }
 * }
 */
ValidationError.prototype.add = function(name, validatorName, msg) {
  msg = msg || this.defaultMessage(name, validatorName);
  var obj = this._errors[name] || {};
  obj[validatorName] = msg;
  this._errors[name] = obj;
};


/**
 * To human readable format
 *   example:  email is not a valid; name is required
 */
ValidationError.prototype.toHuman = function() {
  return this.flatten().join('; ');
};

/**
 * Flatten Error Message
 *   example:  ['email is not a valid', 'name is required']
 */

ValidationError.prototype.flatten = function() {
  var self = this;
  var messages;
  messages = _.map(self._errors, function(obj, name) {
    return _.map(obj, function(msg, validatorName) {
      return msg.toString() || self.defaultMessage(name, validatorName);
    });
  });
  return _.flatten(messages);
};

/**
 * To JSON  format
 */
ValidationError.prototype.asJSON = function() {
  return this._errors;
};

/**
 * Default message format
 */
ValidationError.prototype.defaultMessage = function(name, validatorName) {
  return util.format('%s: \'%s\' validation failed', name, validatorName);
};

/**
 * Check if there exists any error
 */
ValidationError.prototype.any = function() {
  return !_.isEmpty(this._errors);
};

/**
 * Iterate all accepts and params to check validations
 * Usage:
 *    validates: {
 *      require: { message: 'email is required' },
 *      isEmail: { message: 'not a valid email' },
 *      isLengthEnough: function(input) {
 *        if (!!input.length > 8) {
 *          return 'email's length should longer than 8';
 *        }
 *      }
 *    }
 */
function Validate(params, accepts) {

  var validationError = new ValidationError();
  params = params || {};
  accepts = accepts || [];

  _.each(accepts, function(accept) {
    var name = accept.arg;
    var val = params[name];

    var validators = _.extend({}, accept.validates);
    if (validators && _.isPlainObject(validators)) {

      if (helper.isEmpty(val)) {
        // check if value exists
        if (validators.require) {
          validationError.add(name, 'require', validators.require['message']);
          delete validators['require'];
        }
      } else {
        delete validators['require'];
        _.each(validators, function(validatorOpts, validatorName) {
          validatorOpts = validatorOpts || {};
          if (_.isFunction(validatorOpts)) {
            try {
              var result = validatorOpts(val);
              if (result) {
                validationError.add(name, validatorName, result);
              }
            } catch (e) {
              debug(e);
            }
          } else {
            if (!validatorOpts) { return; };
            var validator = Validator[validatorName];<commit_msg>better validation debug info<commit_after>/*!
 * Expose `invokeValidations`.
 */
module.exports = Validate;

/*!
 * Module dependencies.
 */
var _ = require('lodash');
var Validator = require('validator');
var util = require('util');
var helper = require('./helper');
var debug = require('debug')('poplar:validation');

/**
 * @class A wrapper to collect validation errors.
 *
 * @constructor
 * Create a new `ValidationError` with the given `options`.
 */
function ValidationError() {
  this._errors = {};
};

/**
 * add a new validation error
 * {
 *   email: { isEmail: 'not a valid email' }
 * }
 */
ValidationError.prototype.add = function(name, validatorName, msg) {
  msg = msg || this.defaultMessage(name, validatorName);
  var obj = this._errors[name] || {};
  obj[validatorName] = msg;
  this._errors[name] = obj;
};


/**
 * To human readable format
 *   example:  email is not a valid; name is required
 */
ValidationError.prototype.toHuman = function() {
  return this.flatten().join('; ');
};

/**
 * Flatten Error Message
 *   example:  ['email is not a valid', 'name is required']
 */

ValidationError.prototype.flatten = function() {
  var self = this;
  var messages;
  messages = _.map(self._errors, function(obj, name) {
    return _.map(obj, function(msg, validatorName) {
      return msg.toString() || self.defaultMessage(name, validatorName);
    });
  });
  return _.flatten(messages);
};

/**
 * To JSON  format
 */
ValidationError.prototype.asJSON = function() {
  return this._errors;
};

/**
 * Default message format
 */
ValidationError.prototype.defaultMessage = function(name, validatorName) {
  return util.format('%s: \'%s\' validation failed', name, validatorName);
};

/**
 * Check if there exists any error
 */
ValidationError.prototype.any = function() {
  return !_.isEmpty(this._errors);
};

/**
 * Iterate all accepts and params to check validations
 * Usage:
 *    validates: {
 *      require: { message: 'email is required' },
 *      isEmail: { message: 'not a valid email' },
 *      isLengthEnough: function(input) {
 *        if (!!input.length > 8) {
 *          return 'email's length should longer than 8';
 *        }
 *      }
 *    }
 */
function Validate(params, accepts) {

  var validationError = new ValidationError();
  params = params || {};
  accepts = accepts || [];

  _.each(accepts, function(accept) {
    var name = accept.arg;
    var val = params[name];

    var validators = _.extend({}, accept.validates);
    if (validators && _.isPlainObject(validators)) {

      if (helper.isEmpty(val)) {
        // check if value exists
        if (validators.require) {
          validationError.add(name, 'require', validators.require['message']);
          delete validators['require'];
        }
      } else {
        delete validators['require'];
        _.each(validators, function(validatorOpts, validatorName) {
          validatorOpts = validatorOpts || {};
          if (_.isFunction(validatorOpts)) {
            try {
              var result = validatorOpts(val);
              if (result) {
                validationError.add(name, validatorName, result);
              }
            } catch (e) {
              debug('Error: \'%s\' when calling function \'%s\'', e.message, validatorName);
            }
          } else {
            if (!validatorOpts) { return; };
            var validator = Validator[validatorName];
{'fraction_non_alphanumeric': 0.14783904619970195, 'fraction_numerical': 0.0005961251862891207, 'mean_word_length': 2.8130681818181817, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7103795', 'n_tokens_mistral': 1949, 'n_tokens_neox': 1945, 'n_words': 503}