mobx-react-form
Version:
Reactive MobX Form State Management
85 lines • 2.8 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
class JOI {
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.validator = config.package;
this.schema = config.schema;
this.extendValidator();
}
extendValidator() {
if (typeof this.extend === "function") {
this.extend({
validator: this.validator,
form: this.state.form,
});
}
}
validate(field) {
const data = this.state.form.validatedValues;
const { error } = this.schema.validate(data, { abortEarly: false });
if (!error)
return;
const fieldPathArray = field.path.split(".");
const fieldErrors = error.details
.filter((detail) => {
const errorPathString = detail.path.join(".");
const fieldPathString = fieldPathArray.join(".");
return (errorPathString === fieldPathString ||
errorPathString.startsWith(`${fieldPathString}.`));
})
.map((detail) => {
var _a;
const label = ((_a = detail.context) === null || _a === void 0 ? void 0 : _a.label) || detail.path.join(".");
const message = detail.message.replace(`${detail.path.join(".")}`, label);
return message;
});
if (fieldErrors.length) {
field.validationErrorStack = fieldErrors;
}
}
}
exports.default = (config) => ({
class: (JOI),
config,
});
//# sourceMappingURL=JOI.js.map
;