UNPKG

mobx-react-form

Version:
63 lines (59 loc) 2.25 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.MobxReactFormValidatorJOI = factory()); })(this, (function () { 'use strict'; class JOI { promises; config; state; extend; validator; schema; constructor({ config, state = null, promises = [], }) { this.state = state; this.promises = promises; this.config = config; this.extend = 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.flatMapValues; 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) => { const label = detail.context?.label || detail.path.join("."); const message = detail.message.replace(`${detail.path.join(".")}`, label); return message; }); if (fieldErrors.length) { field.validationErrorStack = fieldErrors; } } } var JOI_default = (config) => ({ class: (JOI), config, }); return JOI_default; })); //# sourceMappingURL=MobxReactFormValidatorJOI.umd.js.map