UNPKG

@cosva-lab/form-builder

Version:
115 lines (113 loc) 3.36 kB
import { StatusField } from "../../enums.mjs"; import { __decorate } from "../../_virtual/_@oxc-project_runtime@0.112.0/helpers/decorate.mjs"; import { BaseField } from "../builders/BaseField.mjs"; import { action, makeObservable, observable, runInAction } from "mobx"; //#region src/utils/validate/InputValidator.ts var InputValidator = class InputValidator extends BaseField { static getValidation(obj) { return typeof obj._validate === "function" ? obj._validate(obj) : obj._validate; } get validate() { return !!InputValidator.getValidation(this); } set validate(validate) { this._validate = validate; if (validate) this.validity(); else this.errors = void 0; } get untouched() { return !this.touched; } constructor(props) { super(props); this._validate = false; this.markAsTouched = () => { this.touched = true; }; this.markAsUntouched = () => { this.touched = false; }; this.setError = (error) => { this.addError(error); }; this.setErrors = (errors) => { this.addErrors(errors); }; makeObservable(this); const { validate, validations, value } = props; if (typeof validate !== "undefined") this._validate = validate; this.validations = validations; this.originalProps = { value, validate }; } async validityBase() { const errors = await this.getErrors(); runInAction(() => { if (errors && errors.length) { this.errors = errors; this.status = StatusField.INVALID; } else { this.errors = []; this.status = StatusField.VALID; } }); return this.valid; } /** * @description Returns true if the field is valid * @return {Promise<boolean>} */ async validity() { this._validate = true; return this.validityBase(); } _calculateStatus() { var _this$errors; if ((_this$errors = this.errors) === null || _this$errors === void 0 ? void 0 : _this$errors.length) return StatusField.INVALID; return StatusField.VALID; } async updateValueAndValidity() { this._setInitialStatus(); if (this.enabled) { await this.validity(); runInAction(() => { this.status = this._calculateStatus(); }); } } reset() { this.markAsPristine(); this.markAsUntouched(); this._setInitialStatus(); const { originalProps } = this; this.errors = void 0; if (originalProps) { const { validate, value } = originalProps; this._validate = validate; this.value = value; } } addError(error) { if (error) { if (this.status !== StatusField.INVALID) this.status = StatusField.INVALID; if (!this.errors) this.errors = []; this.errors.unshift(error); } } addErrors(errors) { this.status = StatusField.INVALID; const oldErrors = this.errors || []; if (Array.isArray(oldErrors)) this.errors = [...errors || [], ...oldErrors]; } }; __decorate([observable], InputValidator.prototype, "validations", void 0); __decorate([action.bound], InputValidator.prototype, "validityBase", null); __decorate([action.bound], InputValidator.prototype, "validity", null); __decorate([action.bound], InputValidator.prototype, "updateValueAndValidity", null); __decorate([action.bound], InputValidator.prototype, "reset", null); __decorate([action.bound], InputValidator.prototype, "addError", null); __decorate([action.bound], InputValidator.prototype, "addErrors", null); //#endregion export { InputValidator, InputValidator as default };