<commit_before>import Ember from 'ember';
<<<<<<< HEAD
import validatedForm from '../components/validated-form';
import validatedArea from '../components/validated-area';
import dasherizedToCamel from '../utils/dasherized-to-camel';
=======
>>>>>>> update registration

const {
  computed,
  on,
  Mixin
  } = Ember;

export default Mixin.create({

  isValid: null,

  selectedRules: null,
  errorMessages: null,

  required: false,

  register: 'register',

  registerWithParent: on('didInsertElement', function() {
    this.sendAction('register', this);
  }),

  targetObject: computed.alias('parentView'),

  actions: {
    checkForValid() {
      const errors = runValidations(this);
      const parentArea = this.nearestOfType(validatedArea);

      if (!errors.length) {
        parentArea.send('setState', {
          valid: true,
          errors: null
        });
      }
    },

    validate() {
      const errors = runValidations(this);
      const parentArea = this.nearestOfType(validatedArea);

      if (errors.length) {
        this.set('isValid', false);

        parentArea.send('setState', {
          valid: false,
          errors: errors
        });
      } else {
        this.set('isValid', true);

        parentArea.send('setState', {
          valid: true,
          errors: null
        });
      }
    },

    reset() {
      const parentArea = this.nearestOfType(validatedArea);

      this.set('isValid', null);

      parentArea.send('setState', {
        valid: null,
        errors: null
      });
    }
  },

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

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

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

    if (this.get('required')) {
      rules.unshift('required');
    }

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

    this.set('selectedRules', rules);
  }
});

function runValidations(self) {
  const value = self.get('value');

  if (self.get('optional') && !value) {
    return [];
  }

  const rules = self.get('selectedRules');

  return rules.map(rule => rule())
    .filter(rule => !!rule);
}
<commit_msg>use required not optional and auto detect the rule<commit_after>import Ember from 'ember';
<<<<<<< HEAD
import validatedForm from '../components/validated-form';
import validatedArea from '../components/validated-area';
import dasherizedToCamel from '../utils/dasherized-to-camel';
=======
>>>>>>> update registration

const {
  computed,
  on,
  Mixin
  } = Ember;

export default Mixin.create({

  isValid: null,

  selectedRules: null,
  errorMessages: null,

  required: false,

  register: 'register',

  registerWithParent: on('didInsertElement', function() {
    this.sendAction('register', this);
  }),

  targetObject: computed.alias('parentView'),

  actions: {
    checkForValid() {
      const errors = runValidations(this);
      const parentArea = this.nearestOfType(validatedArea);

      if (!errors.length) {
        parentArea.send('setState', {
          valid: true,
          errors: null
        });
      }
    },

    validate() {
      const errors = runValidations(this);
      const parentArea = this.nearestOfType(validatedArea);

      if (errors.length) {
        this.set('isValid', false);

        parentArea.send('setState', {
          valid: false,
          errors: errors
        });
      } else {
        this.set('isValid', true);

        parentArea.send('setState', {
          valid: true,
          errors: null
        });
      }
    },

    reset() {
      const parentArea = this.nearestOfType(validatedArea);

      this.set('isValid', null);

      parentArea.send('setState', {
        valid: null,
        errors: null
      });
    }
  },

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

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

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

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

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

    this.set('selectedRules', rules);
  }
});

function runValidations(self) {
  const value = self.get('value');

  if (!self.get('required') && !value) {
    return [];
  }

  const rules = self.get('selectedRules');

  return rules.map(rule => rule())
    .filter(rule => !!rule);
}

{'fraction_non_alphanumeric': 0.15769317413831943, 'fraction_numerical': 0.00022527596305474206, 'mean_word_length': 2.583535108958838, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, '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': '2245177', 'n_tokens_mistral': 1358, 'n_tokens_neox': 1319, 'n_words': 274}