<commit_before>import { action, observable, computed, extendObservable } from 'mobx';
import FormField from './form.field';
import AJV from 'ajv';
import _ from 'lodash';

export default class Form {

  @observable fields = {};
  @observable validating = false;
  @observable genericErrorMessage = null;

  ajvValidate = null;
  ajvInstance = null;
  ajvOptions = null;
  ajvSchema = null;
  ajvExtend = null;

  constructor({ fields = {}, schema = false, options = {}, extend = null }) {
    const keys = Object.keys(fields);
    this.initAjv(schema, options, extend);
    this.initFields(keys, fields);
  }

  @action
  initFields(keys, obj) {
    keys.forEach((key) => extendObservable(this.fields, {
      [key]: new FormField(this, key, obj[key]),
    }));
  }

  @action
  initAjv(ajvSchema, ajvOptions = {}, ajvExtend) {
    if (!ajvSchema) return;
    // set ajv schema and options
    this.ajvSchema = ajvSchema;
    this.ajvOptions = ajvOptions;
    this.ajvExtend = ajvExtend;
    // create ajv instance
    this.ajvInstance = new AJV(_.merge(this.ajvOptions, {
      allErrors: true,
      coerceTypes: true,
    }));
    // extend with custom keywords
    if (this.ajvExtend) {
      _.forEach(this.ajvExtend, (val, key) =>
        this.ajvInstance.addKeyword(key, val));
    }
    // create ajvInstance validator (compiling rules)
    this.ajvValidate = this.ajvInstance.compile(this.ajvSchema);
  }

  fieldKeys() {
    return Object.keys(this.fields);
  }

  @computed
  get isValid() {
    if (this.validating) {
      return false; // consider the form invalid until the validation process finish
    }
    return this
      .fieldKeys()
      .reduce((seq, key) => {
        const field = this.fields[key];
        seq = seq && field.isValid; // eslint-disable-line no-param-reassign
        return seq;
      }, true);
  }

  @computed
  get isDirty() {
    return this<commit_msg>Turn on v5 option<commit_after>import { action, observable, computed, extendObservable } from 'mobx';
import FormField from './form.field';
import AJV from 'ajv';
import _ from 'lodash';

export default class Form {

  @observable fields = {};
  @observable validating = false;
  @observable genericErrorMessage = null;

  ajvValidate = null;
  ajvInstance = null;
  ajvOptions = null;
  ajvSchema = null;
  ajvExtend = null;

  constructor({ fields = {}, schema = false, options = {}, extend = null }) {
    const keys = Object.keys(fields);
    this.initAjv(schema, options, extend);
    this.initFields(keys, fields);
  }

  @action
  initFields(keys, obj) {
    keys.forEach((key) => extendObservable(this.fields, {
      [key]: new FormField(this, key, obj[key]),
    }));
  }

  @action
  initAjv(ajvSchema, ajvOptions = {}, ajvExtend) {
    if (!ajvSchema) return;
    // set ajv schema and options
    this.ajvSchema = ajvSchema;
    this.ajvOptions = ajvOptions;
    this.ajvExtend = ajvExtend;
    // create ajv instance
    this.ajvInstance = new AJV(_.merge(this.ajvOptions, {
      allErrors: true,
      coerceTypes: true,
      v5: true,
    }));
    // extend with custom keywords
    if (this.ajvExtend) {
      _.forEach(this.ajvExtend, (val, key) =>
        this.ajvInstance.addKeyword(key, val));
    }
    // create ajvInstance validator (compiling rules)
    this.ajvValidate = this.ajvInstance.compile(this.ajvSchema);
  }

  fieldKeys() {
    return Object.keys(this.fields);
  }

  @computed
  get isValid() {
    if (this.validating) {
      return false; // consider the form invalid until the validation process finish
    }
    return this
      .fieldKeys()
      .reduce((seq, key) => {
        const field = this.fields[key];
        seq = seq && field.isValid; // eslint-disable-line no-param-reassign
        return seq;
      }, true);
  }

  @computed
  get isDirty() {
    return this
{'fraction_non_alphanumeric': 0.1282051282051282, 'fraction_numerical': 0.0005232862375719519, 'mean_word_length': 2.9010204081632653, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '2425092', 'n_tokens_mistral': 1262, 'n_tokens_neox': 1247, 'n_words': 306}