UNPKG

@agile-ts/multieditor

Version:

Simple Form Manager for UI-Frameworks

82 lines (79 loc) 2.37 kB
import { defineConfig, generateId, isFunction, copy } from '@agile-ts/utils'; import { logCodeManager } from '../logCodeManager.js'; var __async = (__this, __arguments, generator) => { return new Promise((resolve, reject) => { var fulfilled = (value) => { try { step(generator.next(value)); } catch (e) { reject(e); } }; var rejected = (value) => { try { step(generator.throw(value)); } catch (e) { reject(e); } }; var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); step((generator = generator.apply(__this, __arguments)).next()); }); }; class Validator { constructor(config = {}) { this.config = {}; this.validationMethods = []; config = defineConfig(config, { key: generateId() }); this._key = config.key; } set key(value) { this._key = value; } get key() { return this._key; } validate(item, value) { return __async(this, null, function* () { let isValid = true; if (item == null || item._key == null) return false; const editor = item.editor(); item.status.statusTracker.track(); for (let i = 0; i < this.validationMethods.length; i++) isValid = (yield this.validationMethods[i].method(item._key, value, editor)) && isValid; const trackedStatuses = item.status.statusTracker.getTrackedStatuses(); if (trackedStatuses.length > 0) { item.status.set(trackedStatuses[0]); item.status.lastTrackedValues = copy(trackedStatuses); } else editor.resetStatus(item._key); return isValid; }); } addValidationMethod(method, config = {}) { if (!isFunction(method)) { logCodeManager.log("41:03:00"); return this; } this.validationMethods.push({ key: config.key, method }); return this; } append(validator) { if (validator === this) { logCodeManager.log("41:03:01"); return this; } this.validationMethods = this.validationMethods.concat(validator.validationMethods); return this; } copy() { const newValidator = new Validator({ key: this._key }); newValidator.validationMethods = copy(this.validationMethods); newValidator.config = copy(this.config); return newValidator; } } export { Validator };