UNPKG

form-atoms

Version:
2 lines (1 loc) 676 B
import{ZodError as m,ZodType as p}from"zod";function V(n,l={}){let{on:i,when:o,formatError:u=e=>e.flatten().formErrors}=l,d=[],f=!!(o!=null&&o.includes("dirty")),s=!!(o!=null&&o.includes("touched")),c=Object.assign(async e=>{let t;if((e.event==="user"||e.event==="submit"||!!(i!=null&&i.includes(e.event)))&&(o===void 0||f&&e.dirty||s&&e.touched)){let a=n instanceof p?n:n(e.get);try{await a.parseAsync(e.value),t=[]}catch(r){if(r instanceof m)return u(r);throw r}}if(d.length>0)for(let a of d){let r=await a(e);if((r!=null&&r.length||r)&&(t=t?t.concat(r):r),t)return t}return t},{or(e){let t=V(n,{formatError:u,...e});return d.push(t),c}});return c}export{V as zodValidate};