UNPKG

@cosva-lab/form-builder

Version:
226 lines (222 loc) 10 kB
'use strict'; var _tslib = require('../../_virtual/_tslib.js'); var mobx = require('mobx'); var FieldBuilder = require('../builders/FieldBuilder.js'); var InputsValidator = /** @class */ (function () { function InputsValidator(_a) { var fields = _a.fields, validate = _a.validate; this.valid = true; this.fieldsMap = {}; this._validate = false; mobx.makeObservable(this); if (typeof validate !== 'undefined') this._validate = validate; this.callbackField = this.callbackField.bind(this); this.addErrors = this.addErrors.bind(this); this.hasErrors = this.hasErrors.bind(this); this.getErrors = this.getErrors.bind(this); this.fields = fields.map(function (field) { return new FieldBuilder.FieldBuilder(field); }); for (var _i = 0, _b = this.fields; _i < _b.length; _i++) { var field = _b[_i]; var name_1 = field.name; this.fieldsMap[name_1] = field; } } Object.defineProperty(InputsValidator.prototype, "invalid", { get: function () { return !this.valid; }, enumerable: false, configurable: true }); Object.defineProperty(InputsValidator.prototype, "validate", { get: function () { return typeof this._validate === 'function' ? this._validate(this) : this._validate; }, set: function (validate) { this._validate = validate; if (validate) this.validity(); for (var _i = 0, _a = this.fields || []; _i < _a.length; _i++) { var field = _a[_i]; if (validate && !field._validate) field._validate = true; else field.errors = undefined; } }, enumerable: false, configurable: true }); InputsValidator.prototype.callbackField = function (callback) { return _tslib.__awaiter(this, void 0, void 0, function () { var fields, _loop_1, _i, _a, field, state_1; return _tslib.__generator(this, function (_b) { switch (_b.label) { case 0: fields = this.fields; _loop_1 = function (field) { var cancel; return _tslib.__generator(this, function (_c) { switch (_c.label) { case 0: cancel = false; return [4 /*yield*/, callback(field, function () { return (cancel = true); })]; case 1: _c.sent(); if (cancel) return [2 /*return*/, "break"]; return [2 /*return*/]; } }); }; _i = 0, _a = this.fields || []; _b.label = 1; case 1: if (!(_i < _a.length)) return [3 /*break*/, 4]; field = _a[_i]; return [5 /*yield**/, _loop_1(field)]; case 2: state_1 = _b.sent(); if (state_1 === "break") return [3 /*break*/, 4]; _b.label = 3; case 3: _i++; return [3 /*break*/, 1]; case 4: return [2 /*return*/, fields]; } }); }); }; InputsValidator.prototype.validityBase = function (args) { return _tslib.__awaiter(this, void 0, void 0, function () { var _a, _b, setErrors, _c, throwFirstError; var _this = this; return _tslib.__generator(this, function (_d) { switch (_d.label) { case 0: this.valid = true; _a = _tslib.__assign({}, args), _b = _a.setErrors, setErrors = _b === void 0 ? true : _b, _c = _a.throwFirstError, throwFirstError = _c === void 0 ? false : _c; return [4 /*yield*/, this.callbackField(function (field, cancel) { return _tslib.__awaiter(_this, void 0, void 0, function () { var valid_1, _a; var _this = this; return _tslib.__generator(this, function (_b) { switch (_b.label) { case 0: if (!field.enabled) return [3 /*break*/, 5]; if (!setErrors) return [3 /*break*/, 2]; return [4 /*yield*/, field.validity()]; case 1: _a = _b.sent(); return [3 /*break*/, 4]; case 2: return [4 /*yield*/, field.hasErrors()]; case 3: _a = !(_b.sent()); _b.label = 4; case 4: valid_1 = _a; mobx.runInAction(function () { if (!valid_1) _this.valid = valid_1; }); if (throwFirstError && !this.valid) cancel(); _b.label = 5; case 5: return [2 /*return*/]; } }); }); })]; case 1: _d.sent(); return [2 /*return*/, this.valid]; } }); }); }; InputsValidator.prototype.validity = function () { this._validate = true; return this.validityBase(); }; InputsValidator.prototype.hasErrors = function (params) { return _tslib.__awaiter(this, void 0, void 0, function () { var _a, _b, setErrors, _c, throwFirstError, valid; return _tslib.__generator(this, function (_d) { switch (_d.label) { case 0: _a = _tslib.__assign({}, params), _b = _a.setErrors, setErrors = _b === void 0 ? false : _b, _c = _a.throwFirstError, throwFirstError = _c === void 0 ? false : _c; if (setErrors) this._validate = true; return [4 /*yield*/, this.validityBase({ setErrors: setErrors, throwFirstError: throwFirstError, })]; case 1: valid = _d.sent(); return [2 /*return*/, !valid]; } }); }); }; InputsValidator.prototype.addErrors = function (errors) { if (!this.validate) this.validate = true; var _loop_2 = function (key) { if (errors.hasOwnProperty(key)) { var error_1 = errors[key]; this_1.callbackField(function (field) { var keys = [field.name]; if (keys.some(function (name) { return name === key; })) field.addErrors(error_1); }); } }; var this_1 = this; for (var key in errors) { _loop_2(key); } }; InputsValidator.prototype.setErrors = function (errors) { errors && this.addErrors(errors); }; InputsValidator.prototype.getErrors = function () { return _tslib.__awaiter(this, void 0, void 0, function () { var fieldsErrors, _i, _a, _b, name_2, errors, enabled; return _tslib.__generator(this, function (_c) { fieldsErrors = {}; for (_i = 0, _a = this.fields; _i < _a.length; _i++) { _b = _a[_i], name_2 = _b.name, errors = _b.errors, enabled = _b.enabled; if (errors && enabled) fieldsErrors[name_2] = errors; } return [2 /*return*/, fieldsErrors]; }); }); }; _tslib.__decorate([ mobx.observable ], InputsValidator.prototype, "valid", void 0); _tslib.__decorate([ mobx.observable ], InputsValidator.prototype, "fields", void 0); _tslib.__decorate([ mobx.observable ], InputsValidator.prototype, "_validate", void 0); _tslib.__decorate([ mobx.action ], InputsValidator.prototype, "validityBase", null); _tslib.__decorate([ mobx.action ], InputsValidator.prototype, "hasErrors", null); _tslib.__decorate([ mobx.action ], InputsValidator.prototype, "addErrors", null); _tslib.__decorate([ mobx.action ], InputsValidator.prototype, "setErrors", null); return InputsValidator; }()); module.exports = InputsValidator; //# sourceMappingURL=InputsValidator.js.map