<commit_before>'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

/**
 * Validator
 *
 * 封装常用的校验工具方法
 */

var Validator = function () {
  function Validator() {
    _classCallCheck(this, Validator);
  }

  _createClass(Validator, null, [{
    key: 'validate',


    /**
     * 验证
     * @param obj
     * @param rules
     * @param opts
     */
    value: function validate(obj, rules, opts) {
      var result = { result: true, errors: {} };
      var options = Object.assign({}, {
        oneError: false
      }, opts);

      for (var field in rules) {
        if (rules.hasOwnProperty(field)) {
          /**
           * 获取规则对象, 例如:
           * {
           *   userName: {
           *   required: true,
           *   email: true,
           *   message: {
           *    required: '用户名必填',
           *    email: '邮箱'
           *   }}
           * }
           */
          var ruleMap = rules[field];
          //将要校验的值
          var value = obj[field];
          //校验的信息
          var message = rules[field]['message'] || {};
          //错误集合
          var errors = [];

          for (var rule in ruleMap) {
            if (ruleMap.hasOwnProperty(rule) && rule != 'message') {
              var ruleValue = ruleMap[rule];
              //如果规则的值为false,跳过校验
              if (typeof ruleValue === 'boolean' && ruleValue === false) {
                continue;
              }
              var validateMethod = Validator[rule];
              //没有通过校验
              if (!validateMethod(value)) {
                errors.push(message[rule]);
                //如果开启一个错误
                if (options.oneError) {
                  break;
                }
              }
            }
          }

          if (errors.length != 0) {
            result.result = false;
            result.errors[field] = errors;

            //如果开启oneError，立刻返回错误
            if (options.oneError) {
              return result;
            }
          }
        }<commit_msg>rebuild validator.js<commit_after>'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

/**
 * Validator
 *
 * 封装常用的校验工具方法
 */

var Validator = function () {
  function Validator() {
    _classCallCheck(this, Validator);
  }

  _createClass(Validator, null, [{
    key: 'validate',


    /**
     * 验证
     * @param obj
     * @param rules
     * @param opts
     */
    value: function validate(obj, rules, options) {
      //保存错误信息
      var result = { result: true, errors: {} };
      //获取配置参数
      var opts = Object.assign({}, {
        oneError: false,
        debug: false
      }, options);

      for (var field in rules) {
        if (rules.hasOwnProperty(field)) {
          /**
           * 获取规则对象, 例如:
           * {
           *   userName: {
           *   required: true,
           *   email: true,
           *   message: {
           *    required: '用户名必填',
           *    email: '邮箱'
           *   }}
           * }
           */
          var ruleMap = rules[field];
          //将要校验的值
          var value = obj[field];
          //校验的信息
          var message = rules[field]['message'] || {};
          //错误集合
          var errors = [];

          if (opts.debug) {
            console.groupCollapsed('validate \'' + field + '\'');
          }

          for (var rule in ruleMap) {
            if (ruleMap.hasOwnProperty(rule) && rule != 'message') {
              //获取当前的校验规则
              var ruleValue = ruleMap[rule];
              //封装传递给校验函数的参数
              var args = [];

              //如果规则的值为布尔类型且为false,跳过校验
              if (typeof ruleValue === 'boolean' && ruleValue === false) {
                continue;
              } else if (typeof ruleValue === 'boolean') {
                args.push(value);
              } else {
                args.push(ruleValue);
                args.push(value);
              }

              var validateMethod = Validator[rule];
              //如果validateMethod不存在，给出警告
              if (!validateMethod) {
                console.warn && console.warn('can not find \'' + rule + '\' rule in \'' + field + '\'');
                continue;
              }

              if (opts.debug) {
                console.log && console.log('validator rule => \'' + rule + ', ruleValue => \'' + ruleValue + '\'');
              }

              //没有通过校验
              if (!validateMethod.apply(null, args)) {
                if (opts.debug) {
                  console.log('result: failed');
                }
                errors.push(message[rule]);
                //如果开启一个错误
                if (opts.oneError) {
                  break;
                }
              } else {
                if (opts.debug) {
                  console.log('result: ok');
                }
              }
            }
          }

          if (opts.debug) {
            console.groupEnd && console.groupEnd();
          }

          if (errors.length != 0) {
            result.result = false;
            result.errors[field] = errors;

            //如果开启oneError，立刻返回错误
            if (opts.oneError) {
              return result;
            }
          }
        }
{'fraction_non_alphanumeric': 0.12805340615991503, 'fraction_numerical': 0.0006068881808526779, 'mean_word_length': 1.567978184651344, 'pattern_counts': {'":': 0, '<': 5, '<?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': '5677140', 'n_tokens_mistral': 1890, 'n_tokens_neox': 1928, 'n_words': 414}