<commit_before>'use strict';

/**
 * base logic
 * inherits from base controller
 * @type {Class}
 */
export default class extends think.controller.base {
  /**
   * check auth
   * @return {Promise} []
   */
  checkAuth(){

  }
  /**
   * parse validate data
   * {
   *   name: 'required|int|min:10|max:20',
   *   title: 'length:10,20|default:welefen|get',
   *   emai: 'required|email:{}',
   *   ids: 'required|array|int'
   * }
   * @param  {Array}  data []
   * @return {Array}      []
   */
  _parseValidateData(data = {}){
    let result = {};
    for(let name in data){
      let rules = data[name].split('|');
      let itemData = {};
      rules.forEach(item => {
        if(!item){
          return;
        }
        let pos = item.indexOf(':');
        if(pos > -1){
          let name = item.substr(0, pos);
          let args = item.substr(pos + 1).trim();
          if(args[0] === '{' || args[0] === '['){
            args = [(new Function('', `return ${args}`))()];
          }else if(name !== 'default'){
            args = args.split(/\s*,\s*/);
          }
          itemData[name] = args;
        }else{
          itemData[item] = true;
        }
      });
      let method = this.http.method.toLowerCase();
      if(itemData.get){
        method = 'get';
        delete itemData.get;
      }else if(itemData.file){
        method = 'file';
        delete itemData.file;
      }
      let value = this[method](name);

      if(!value && itemData.default){
        value = itemData.default;
      }
      delete itemData.default;

      if(itemData.int && !itemData.array){
        value = parseInt(value, 10);
      }else if(itemData.float && !itemData.array){
        value = parseFloat(value);
      }else if(itemData.array){
        if(!think.isArray(value)){
          value = think.isString(value) ? value.split(/\s*,\s*/) : [value];
        }
        value = value.map(itemValue => {
          if(itemData.int){
            return parseInt(itemValue, 10);
          }else if(itemData.float){
            return parseFloat(itemValue);
          }
          return itemValue;
        });
      }else if(itemData.boolean){
        if(!think.isBoolean(value)){
          value = ['yes', 'on', '1', 'true'].indexOf(value) > -1;
        }
      }else if(itemData.object){
        if(!think.isObject(value)){
          try{
            value = JSON.parse(value);
          }catch(e){
            value = '';
          }
        }
      }else{
        itemData.string = true;
      }
      //set value to request
      this[method](name, value);
      itemData.value = value;
      result[name] = itemData;
    }
    return result;
  }
  /**
   * validate data
   * this.validate({
   *   welefen: 'required|length:4,20|alpha',
   *   email: 'required|email',
   *   title: 'required|maxLength:10000'
   * })
   * @param  {Object} data      []
   * @return {}           []
   */
  validate(data) {
    if(think.isEmpty(data)){
      return true;
    }
    data = this._parseValidateData(data);
    let ret = think.validate(data, this.locale());
    if(!think.isEmpty(ret)){
      this.assign('errors', ret);
      return false;
    }
    return true;
  }
  /**
   * get validate errors
   * @return {Object} []
   */
  errors() {
    return this.assign('errors');
  }
  /**
   * auto validate
   * @return {} []
   */
  __after(){
    if(think.isEmpty(this.rules)){
      return;
    }
    let flag = this.validate(this.rules);
    if(!flag){
      let error = this.config('error');
      let errors = this.errors();
      let msg = Object.keys(errors).map(item => {
        return errors[item];<commit_msg>add _validateInvoked property to check validate method is invoked<commit_after>'use strict';

/**
 * base logic
 * inherits from base controller
 * @type {Class}
 */
export default class extends think.controller.base {
  /**
   * check auth
   * @return {Promise} []
   */
  checkAuth(){

  }
  /**
   * parse validate data
   * {
   *   name: 'required|int|min:10|max:20',
   *   title: 'length:10,20|default:welefen|get',
   *   emai: 'required|email:{}',
   *   ids: 'required|array|int'
   * }
   * @param  {Array}  data []
   * @return {Array}      []
   */
  _parseValidateData(data = {}){
    let result = {};
    for(let name in data){
      let rules = data[name].split('|');
      let itemData = {};
      rules.forEach(item => {
        if(!item){
          return;
        }
        let pos = item.indexOf(':');
        if(pos > -1){
          let name = item.substr(0, pos);
          let args = item.substr(pos + 1).trim();
          if(args[0] === '{' || args[0] === '['){
            args = [(new Function('', `return ${args}`))()];
          }else if(name !== 'default'){
            args = args.split(/\s*,\s*/);
          }
          itemData[name] = args;
        }else{
          itemData[item] = true;
        }
      });
      let method = this.http.method.toLowerCase();
      if(itemData.get){
        method = 'get';
        delete itemData.get;
      }else if(itemData.file){
        method = 'file';
        delete itemData.file;
      }
      let value = this[method](name);

      if(!value && itemData.default){
        value = itemData.default;
      }
      delete itemData.default;

      if(itemData.int && !itemData.array){
        value = parseInt(value, 10);
      }else if(itemData.float && !itemData.array){
        value = parseFloat(value);
      }else if(itemData.array){
        if(!think.isArray(value)){
          value = think.isString(value) ? value.split(/\s*,\s*/) : [value];
        }
        value = value.map(itemValue => {
          if(itemData.int){
            return parseInt(itemValue, 10);
          }else if(itemData.float){
            return parseFloat(itemValue);
          }
          return itemValue;
        });
      }else if(itemData.boolean){
        if(!think.isBoolean(value)){
          value = ['yes', 'on', '1', 'true'].indexOf(value) > -1;
        }
      }else if(itemData.object){
        if(!think.isObject(value)){
          try{
            value = JSON.parse(value);
          }catch(e){
            value = '';
          }
        }
      }else{
        itemData.string = true;
      }
      //set value to request
      this[method](name, value);
      itemData.value = value;
      result[name] = itemData;
    }
    return result;
  }
  /**
   * validate data
   * this.validate({
   *   welefen: 'required|length:4,20|alpha',
   *   email: 'required|email',
   *   title: 'required|maxLength:10000'
   * })
   * @param  {Object} data      []
   * @return {}           []
   */
  validate(data) {
    this._validateInvoked = true;
    if(think.isEmpty(data)){
      return true;
    }
    data = this._parseValidateData(data);
    let ret = think.validate(data, this.locale());
    if(!think.isEmpty(ret)){
      this.assign('errors', ret);
      return false;
    }
    return true;
  }
  /**
   * get validate errors
   * @return {Object} []
   */
  errors() {
    return this.assign('errors');
  }
  /**
   * auto validate
   * @return {} []
   */
  __after(){
    if(think.isEmpty(this.rules) || this._validateInvoked){
      return;
    }
    let flag = this.validate(this.rules);
    if(!flag){
      let error = this.config('error');
      let errors = this.errors();
      let msg = Object.keys(errors).map(item => {
        return errors[item];
{'fraction_non_alphanumeric': 0.1694040993620198, 'fraction_numerical': 0.0073299850685489346, 'mean_word_length': 1.9053627760252365, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '7104262', 'n_tokens_mistral': 2371, 'n_tokens_neox': 2346, 'n_words': 458}