<commit_before>import Ember from 'ember';
import layout from '../templates/components/validation-wrapper';
import runValidations from '../utils/run-validations';

const {
  observer,
  on,
  Component,
  computed,
  run
  } = Ember;

export default Component.extend({
  layout: layout,

  tagName: '',
  value: null,
  submitErrors: null,
  errors: null,
  valid: null,
  selectedRules: null,
  isRequired: false,
  register: null,


  totalErrors: computed('submitErrors', 'errors', function() {
    let submitErrors = this.get('submitErrors') || [];
    let errors = this.get('errors') || [];

    let total = [].concat(submitErrors).concat(errors);

    if (total.length) {
      if (this.get('valid') !== false) {
        run.next(this, this.set, 'valid', false);
      }
    }

    return total;
  }),

  validationState: computed('valid', function() {
    const valid = this.get('valid');
    let state = {};

    state.valid = valid
    state.isValid = valid === true;
    state.isInvalid = valid === false;
    state.isInitial = valid === null;
    state.text = valid === true ? 'valid' : (valid === false ? 'invalid' : 'initial');

    return state;
  }),

  actions: {
    checkForValid(value) {
      if(value !== undefined) {
        this.set('value', value);
      }
      const errors = runValidations(this);

      if (!errors.length) {
        this.setProperties({
          valid: true,
          errors: null
        });
      }
    },

    validate(value) {
      if(value !== undefined) {
        this.set('value', value);
      }
      const errors = runValidations(this);

      if (errors.length) {
        this.setProperties({
          valid: false,
          errors: errors
        });
      } else {
        this.setProperties({
          valid: true,
          errors: null
        });
      }
    },

    reset() {
      this.setProperties({
        valid: null,
        errors: null
      });
    }
  },

  init: function() {
    this._super();

    let rules = this.get('rules');
    const register = this.get('register');

    rules = rules ? rules.split(' ') : [];

    if (rules.indexOf('required') !== -1 || rules.indexOf('exists') !== -1) {
      this.set('isRequired', true);
    }

    rules = rules.map(rule => {
      return this.container.lookupFactory(`validation:${rule}`);
    });

    this.set('selectedRules', rules);

    register(this);
  }
});<commit_msg>filter non existent rules<commit_after>import Ember from 'ember';
import layout from '../templates/components/validation-wrapper';
import runValidations from '../utils/run-validations';

const {
  observer,
  on,
  Component,
  computed,
  run
  } = Ember;

export default Component.extend({
  layout: layout,

  tagName: '',
  value: null,
  submitErrors: null,
  errors: null,
  valid: null,
  selectedRules: null,
  isRequired: false,
  register: null,


  totalErrors: computed('submitErrors', 'errors', function() {
    let submitErrors = this.get('submitErrors') || [];
    let errors = this.get('errors') || [];

    let total = [].concat(submitErrors).concat(errors);

    if (total.length) {
      if (this.get('valid') !== false) {
        run.next(this, this.set, 'valid', false);
      }
    }

    return total;
  }),

  validationState: computed('valid', function() {
    const valid = this.get('valid');
    let state = {};

    state.valid = valid
    state.isValid = valid === true;
    state.isInvalid = valid === false;
    state.isInitial = valid === null;
    state.text = valid === true ? 'valid' : (valid === false ? 'invalid' : 'initial');

    return state;
  }),

  actions: {
    checkForValid(value) {
      if(value !== undefined) {
        this.set('value', value);
      }
      const errors = runValidations(this);

      if (!errors.length) {
        this.setProperties({
          valid: true,
          errors: null
        });
      }
    },

    validate(value) {
      if(value !== undefined) {
        this.set('value', value);
      }
      const errors = runValidations(this);

      if (errors.length) {
        this.setProperties({
          valid: false,
          errors: errors
        });
      } else {
        this.setProperties({
          valid: true,
          errors: null
        });
      }
    },

    reset() {
      this.setProperties({
        valid: null,
        errors: null
      });
    }
  },

  init: function() {
    this._super();

    let rules = this.get('rules');
    const register = this.get('register');

    rules = rules ? rules.split(' ') : [];

    if (rules.indexOf('required') !== -1 || rules.indexOf('exists') !== -1) {
      this.set('isRequired', true);
    }

    rules = rules.map(rule => this.container.lookupFactory(`validation:${rule}`))
    .filter(rule => !!rule);

    this.set('selectedRules', rules);

    register(this);
  }
});
{'fraction_non_alphanumeric': 0.16369664318276006, 'fraction_numerical': 0.0008288437629506838, 'mean_word_length': 2.3175257731958765, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, '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': '6121233', 'n_tokens_mistral': 1468, 'n_tokens_neox': 1431, 'n_words': 326}