mobx-react-form
Version:
Reactive MobX Form State Management
117 lines • 4.13 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = __importDefault(require("lodash"));
function isPromise(obj) {
return (!!obj &&
typeof obj.then === "function" &&
(typeof obj === "object" || typeof obj === "function"));
}
class SVK {
constructor({ config, state = null, promises = [], }) {
Object.defineProperty(this, "promises", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "config", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "state", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "extend", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "validator", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "schema", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
this.state = state;
this.promises = promises;
this.config = config;
this.extend = config === null || config === void 0 ? void 0 : config.extend;
this.schema = config.schema;
this.initValidator();
}
extendOptions(options = {}) {
return Object.assign(Object.assign({}, options), { errorDataPath: "property", allErrors: true, coerceTypes: true, v5: true });
}
initValidator() {
const AJV = this.config.package;
const validatorInstance = new AJV(this.extendOptions(this.config.options));
if (typeof this.extend === "function") {
this.extend({
form: this.state.form,
validator: validatorInstance,
});
}
this.validator = validatorInstance.compile(this.schema);
}
validate(field) {
const result = this.validator(field.state.form.validatedValues);
if (isPromise(result)) {
const $p = result
.then(() => field.setValidationAsyncData(true))
.catch((err) => err && this.handleAsyncError(field, err.errors))
.then(() => this.executeAsyncValidation(field));
this.promises.push($p);
return;
}
this.handleSyncError(field, this.validator.errors);
}
handleSyncError(field, errors) {
const fieldError = this.findError(field.path, errors);
if (!fieldError)
return;
const message = `${field.label} ${fieldError.message}`;
field.invalidate(message, false);
}
handleAsyncError(field, errors) {
const fieldError = this.findError(field.path, errors);
if (!fieldError)
return;
const message = `${field.label} ${fieldError.message}`;
field.setValidationAsyncData(false, message);
}
findError(path, errors) {
return lodash_1.default.find(errors, ({ dataPath }) => {
let $dataPath;
$dataPath = lodash_1.default.trimStart(dataPath, ".");
$dataPath = lodash_1.default.replace($dataPath, "]", "");
$dataPath = lodash_1.default.replace($dataPath, "[", ".");
return lodash_1.default.includes($dataPath, path);
});
}
executeAsyncValidation(field) {
const asyncData = field.validationAsyncData;
if (asyncData.valid === false) {
field.invalidate(asyncData.message, false, true);
}
}
}
exports.default = (config) => ({
class: (SVK),
config,
});
//# sourceMappingURL=SVK.js.map
;