@cosva-lab/form-builder
Version:
React form builder.
235 lines (230 loc) • 8.67 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var _tslib = require('../../_virtual/_tslib.js');
var mobx = require('mobx');
var enums = require('../../enums.js');
var Field = require('../builders/Field.js');
var validators = require('./validators.js');
var InputValidator = /** @class */ (function (_super) {
_tslib.__extends(InputValidator, _super);
function InputValidator(props) {
var _this = _super.call(this, props) || this;
_this._validate = false;
mobx.makeObservable(_this);
var validate = props.validate, validations = props.validations, value = props.value, globalProps = props.globalProps;
if (typeof validate !== 'undefined')
_this._validate = validate;
// validations is an array of validation rules specific to a form
_this.validations = validations;
_this.originalProps = { value: value, validate: validate };
_this.globalProps = globalProps;
return _this;
}
InputValidator.getValidation = function (obj) {
return typeof obj._validate === 'function'
? obj._validate(obj)
: obj._validate;
};
Object.defineProperty(InputValidator.prototype, "validate", {
get: function () {
return InputValidator.getValidation(this);
},
set: function (validate) {
this._validate = validate;
if (validate)
this.validity();
else
this.errors = [];
},
enumerable: false,
configurable: true
});
Object.defineProperty(InputValidator.prototype, "untouched", {
get: function () {
return !this.touched;
},
enumerable: false,
configurable: true
});
Object.defineProperty(InputValidator.prototype, "globalProps", {
get: function () {
return ((this.fieldsBuilder && this.fieldsBuilder.globalProps) ||
this._globalProps);
},
set: function (globalProps) {
if (this.fieldsBuilder)
this.fieldsBuilder.globalProps = globalProps;
else
this._globalProps = globalProps;
},
enumerable: false,
configurable: true
});
InputValidator.prototype.hasValidationError = function (validation) {
var rule = validation.rule || 'isEmpty';
var _a = validation.args, args = _a === void 0 ? [] : _a;
if (![
'contains',
'equals',
'isAfter',
'isAlpha',
'isAlphanumeric',
'isAscii',
'isDecimal',
'isEmail',
'isEmpty',
'isFloat',
'isNumeric',
].includes(rule)) {
console.error(rule, "the rule don't exists");
rule = 'isEmpty';
}
else {
var validator_1 = validators[rule];
if (validator_1) {
var boolean = false;
switch (rule) {
case 'isEmpty':
boolean = true;
break;
}
if (typeof this.value === 'string' &&
validator_1((this.value || '').toString(), args) === boolean) {
this.status = enums.StatusField.INVALID;
return true;
}
else
this.status = enums.StatusField.VALID;
}
}
return false;
};
InputValidator.prototype.markAsTouched = function () {
this.touched = true;
};
InputValidator.prototype.markAsUntouched = function () {
this.touched = false;
};
InputValidator.prototype.validityBase = function () {
return _tslib.__awaiter(this, void 0, void 0, function () {
var errors;
var _this = this;
return _tslib.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getErrors()];
case 1:
errors = _a.sent();
mobx.runInAction(function () {
if (errors && errors.length) {
_this.errors = errors;
_this.status = enums.StatusField.INVALID;
}
else {
_this.errors = _this.errors = [];
_this.status = enums.StatusField.VALID;
}
});
return [2 /*return*/, this.valid];
}
});
});
};
/**
* @description Returns true if the field is valid
* @return {Promise<boolean>}
*/
InputValidator.prototype.validity = function () {
return _tslib.__awaiter(this, void 0, void 0, function () {
return _tslib.__generator(this, function (_a) {
this._validate = true;
return [2 /*return*/, this.validityBase()];
});
});
};
InputValidator.prototype._calculateStatus = function () {
if (this.disabled)
return enums.StatusField.DISABLED;
else if (this.errors && this.errors.length)
return enums.StatusField.INVALID;
return enums.StatusField.VALID;
};
InputValidator.prototype.updateValueAndValidity = function () {
return _tslib.__awaiter(this, void 0, void 0, function () {
var _this = this;
return _tslib.__generator(this, function (_a) {
switch (_a.label) {
case 0:
this._setInitialStatus();
if (!this.enabled) return [3 /*break*/, 2];
return [4 /*yield*/, this.validity()];
case 1:
_a.sent();
mobx.runInAction(function () {
_this.status = _this._calculateStatus();
});
_a.label = 2;
case 2: return [2 /*return*/];
}
});
});
};
InputValidator.prototype.reset = function () {
this.markAsPristine();
this.markAsUntouched();
this._setInitialStatus();
var originalProps = this.originalProps;
if (originalProps) {
var validate = originalProps.validate, value = originalProps.value;
this._validate = validate;
this.value = value;
}
};
InputValidator.prototype.addError = function (error) {
if (error) {
if (this.status !== enums.StatusField.INVALID)
this.status = enums.StatusField.INVALID;
if (!this.errors)
this.errors = [];
this.errors.unshift(error);
}
};
InputValidator.prototype.addErrors = function (errors) {
this.status = enums.StatusField.INVALID;
var oldErrors = this.errors || [];
this.errors = _tslib.__spreadArray(_tslib.__spreadArray([], errors, true), oldErrors, true);
};
InputValidator.prototype.setError = function (error) {
this.addError(error);
};
InputValidator.prototype.setErrors = function (errors) {
this.addErrors(errors);
};
_tslib.__decorate([
mobx.observable
], InputValidator.prototype, "validations", void 0);
_tslib.__decorate([
mobx.action
], InputValidator.prototype, "hasValidationError", null);
_tslib.__decorate([
mobx.action
], InputValidator.prototype, "validityBase", null);
_tslib.__decorate([
mobx.action
], InputValidator.prototype, "validity", null);
_tslib.__decorate([
mobx.action
], InputValidator.prototype, "updateValueAndValidity", null);
_tslib.__decorate([
mobx.action
], InputValidator.prototype, "reset", null);
_tslib.__decorate([
mobx.action
], InputValidator.prototype, "addError", null);
_tslib.__decorate([
mobx.action
], InputValidator.prototype, "addErrors", null);
return InputValidator;
}(Field.Field));
exports.InputValidator = InputValidator;
exports.default = InputValidator;
//# sourceMappingURL=InputValidator.js.map