form-atoms
Version:
Form primitives for Jotai
2 lines (1 loc) • 694 B
JavaScript
import{isValiError as p}from"valibot";function w(d){return function c(i,f={}){let{on:r,when:a,formatError:l=t=>t.issues.map(e=>e.message)}=f,o=[],V=!!(a!=null&&a.includes("dirty")),m=!!(a!=null&&a.includes("touched")),u=Object.assign(async t=>{let e;if((t.event==="user"||t.event==="submit"||!!(r!=null&&r.includes(t.event)))&&(a===void 0||V&&t.dirty||m&&t.touched)){let s=typeof i=="function"?i(t.get):i;try{await d(s,t.value),e=[]}catch(n){if(p(n))return l(n);throw n}}if(o.length>0)for(let s of o){let n=await s(t);if((n!=null&&n.length||n)&&(e=e?e.concat(n):n),e)return e}return e},{or(t){let e=c(i,{formatError:l,...t});return o.push(e),u}});return u}}export{w as createValibotValidator};