<commit_before>import Schema from "async-validator";
import {observable, runInAction, action, isObservable} from "mobx";

export default class Validator {
    validator;
    subject;
    _results = {};
    @observable results = {};

    constructor(subject, descriptor) {
        this.subject = subject;
        this.validator = new Schema(descriptor);
        this.setResults({status: null, message: null});
        this.results = this._results;
    }

    setResults = (result) => {
        Object.keys(this.subject).map((fieldName) => {
            this._results[fieldName] = result;
        })
    };

    validate = (success, fail) => {
        this.setResults({status: "validating", message: null});
        this.validator.validate(this.subject, (errors, fields) => {
            this.setResults({status: "success", message: null});
            if (errors) {
                Object.keys(fields).map((field) => {
                    this._results[field] = {status: "error", message: fields[field][0].message}
                });
                fail && fail(this._results);
            } else {
                success && success(this._results);
            }
            this.results = this._results;
        })
    };

    validateField = (fieldName, success, fail) => {
        this._results[fieldName] = {status: "validating", message: null};
        this.validator.validate({[fieldName]: this.subject[fieldName]}, (errors, fields) => {
            if (errors) {
                this._results[fieldName] = {status: "error", message: errors[0].message};
                fail && fail(this._results[fieldName]);
            } else {
                this._results[fieldName] = {status: "success", message: null};
                success && success(this._results[fieldName]);
            }
            this.results = this._results;
        })
    };
}<commit_msg>move position the observable results.<commit_after>import Schema from "async-validator";
import {observable, runInAction, action, isObservable} from "mobx";

export default class Validator {
    validator;
    subject;
    _results = {};
    @observable results = {};

    constructor(subject, descriptor) {
        this.subject = subject;
        this.validator = new Schema(descriptor);
        this.setResults({status: null, message: null});
        this.results = this._results;
    }

    setResults = (result) => {
        Object.keys(this.subject).map((fieldName) => {
            this._results[fieldName] = result;
        })
    };

    validate = (success, fail) => {
        this.setResults({status: "validating", message: null});
        this.validator.validate(this.subject, (errors, fields) => {
            this.setResults({status: "success", message: null});
            if (errors) {
                Object.keys(fields).map((field) => {
                    this._results[field] = {status: "error", message: fields[field][0].message}
                });
                this.results = this._results;
                fail && fail(this._results);
            } else {
                this.results = this._results;
                success && success(this._results);
            }
        })
    };

    validateField = (fieldName, success, fail) => {
        this._results[fieldName] = {status: "validating", message: null};
        this.validator.validate({[fieldName]: this.subject[fieldName]}, (errors, fields) => {
            if (errors) {
                this._results[fieldName] = {status: "error", message: errors[0].message};
                this.results = this._results;
                fail && fail(this._results[fieldName]);
            } else {
                this._results[fieldName] = {status: "success", message: null};
                this.results = this._results;
                success && success(this._results[fieldName]);
            }
        })
    };
}
{'fraction_non_alphanumeric': 0.1423017107309487, 'fraction_numerical': 0.0010368066355624676, 'mean_word_length': 2.0578446909667196, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '3063419', 'n_tokens_mistral': 1038, 'n_tokens_neox': 1013, 'n_words': 206}