mobx-react-form
Version:
Reactive MobX Form State Management
3 lines (2 loc) • 1.75 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).MobxReactFormValidatorSVK=e()}(this,function(){"use strict";class t{promises;config;state;extend;validator;schema;constructor({config:t,state:e=null,promises:i=[]}){this.state=e,this.promises=i,this.config=t,this.extend=t?.extend,this.schema=t.schema,this.initValidator()}extendOptions(t={}){return{...t,errorDataPath:"property",allErrors:!0,coerceTypes:!0,v5:!0}}initValidator(){const t=new(0,this.config.package)(this.extendOptions(this.config.options));"function"==typeof this.extend&&this.extend({form:this.state.form,validator:t}),this.validator=t.compile(this.schema)}validate(t){const e=this.validator(t.state.form.flatMapValues);if((i=e)&&"function"==typeof i.then&&("object"==typeof i||"function"==typeof i)){const i=e.then(()=>t.setValidationAsyncData(!0)).catch(e=>e&&this.handleAsyncError(t,e.errors)).then(()=>this.executeAsyncValidation(t));return void this.promises.push(i)}var i;this.handleSyncError(t,this.validator.errors)}handleSyncError(t,e){const i=this.findError(t.path??"",e);if(!i)return;const a=`${t.label} ${i.message}`;t.invalidate(a,!1)}handleAsyncError(t,e){const i=this.findError(t.path??"",e);if(!i)return;const a=`${t.label} ${i.message}`;t.setValidationAsyncData(!1,a)}findError(t,e){if(e)return e.find(({dataPath:e})=>{let i;return i=e.replace(/^\.+/,""),i=i.replace(/\]/g,""),i=i.replace(/\[/g,"."),i.includes(t)})}executeAsyncValidation(t){!1===t.validationAsyncData.valid&&t.invalidate(t.validationAsyncData.message??void 0,!1,!0)}}return e=>({class:t,config:e})});
//# sourceMappingURL=MobxReactFormValidatorSVK.umd.min.js.map