UNPKG

@allgemein/schema-api

Version:
139 lines 4.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DataContainer = void 0; const lodash_1 = require("lodash"); const Constants_1 = require("./Constants"); const Validator_1 = require("./validation/Validator"); const ClassRef_1 = require("./ClassRef"); const base_1 = require("@allgemein/base"); /** * Container for validation of object */ class DataContainer { constructor(instance, registry) { this.errors = []; this.validation = {}; this.instance = instance; if (registry) { this.ref = (0, lodash_1.isFunction)(registry.getEntityRefFor) ? registry.getEntityRefFor(instance) : registry; } else { const clazz = base_1.ClassUtils.getFunction(instance); this.ref = ClassRef_1.ClassRef.get(clazz); } if (!this.ref) { throw new Error('none definition found for instance ' + JSON.stringify(instance)); } const properties = this.ref.getPropertyRefs(); for (const property of properties) { this.validation[property.name] = { key: property.name, valid: false, checked: false, messages: [] }; } ; } addError(e) { if (!(0, lodash_1.has)(e, 'type')) { e.type = 'error'; } this.errors.push(e); } hasErrors() { return this.errors.length > 0; } checked(str) { if (this.validation[str]) { return this.validation[str].checked; } return false; } value(str) { const wrap = {}; Object.defineProperty(wrap, str, { get: () => { // @ts-ignore return this.instance[str]; }, set: (y) => { // @ts-ignore this.instance[str] = y; } }); // @ts-ignore return wrap[str]; } valid(str) { if (this.validation[str]) { return this.validation[str].valid; } return false; } messages(str) { if (this.validation[str] && this.validation[str].messages) { return this.validation[str].messages; } return []; } async validate() { this.isValidated = true; (0, lodash_1.remove)(this.errors, error => error.type === 'validate'); let results = []; try { results = await Validator_1.Validator.validate(this.instance, this.ref); } catch (e) { console.error(e); } results.map(r => this.errors.push({ property: r.property, value: r.value, constraints: r.constraints, type: 'validate' })); this.isSuccessValidated = true; (0, lodash_1.keys)(this.validation).forEach(key => { if (this.validation[key]) { const valid = this.validation[key]; const found = this.errors.find(x => x.property === key); valid.messages = []; if (found) { valid.valid = false; Object.keys(found.constraints).forEach(c => { valid.messages.push({ type: c, content: found.constraints[c] }); }); } else { valid.valid = true; } this.isSuccessValidated = this.isSuccessValidated && valid.valid; valid.checked = true; } }); return this.isSuccessValidated; } applyState() { const $state = {}; DataContainer.keys.forEach(k => { const value = (0, lodash_1.get)(this, k, null); if ((0, lodash_1.isBoolean)(value) || !(0, lodash_1.isEmpty)(value)) { (0, lodash_1.set)($state, k, value); } }); (0, lodash_1.set)(this.instance, Constants_1.STATE_KEY, $state); } resetErrors() { this.errors = []; } } exports.DataContainer = DataContainer; DataContainer.keys = [ 'isValidated', 'isSuccess', 'isSuccessValidated', 'errors' ]; //# sourceMappingURL=DataContainer.js.map