svelidate
Version:
Simple and lightweight form validation for Svelte with no dependencies
2 lines (1 loc) • 13.2 kB
JavaScript
;Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const V={mode:"default",pattern:{symbol:"[!\"#\\$%&'\\(\\)\\*\\+,-\\.\\/: ;<=>\\?@\\[\\]\\^_`}{~\\|\\\\]",email:"[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\\.[a-zA-Z]+"}};function K(e){const t=structuredClone(V),a=(n,r)=>{Object.keys(r).forEach(i=>{r[i]===null?n[i]=r[i]:typeof r[i]=="object"?a(n[i],r[i]):n[i]=r[i]})};return a(t,e),t}function U(e){return e&&e[0]==="$"}function $(e,t){Object.keys(e).forEach(a=>{U(a)||t(e[a],a)})}function F(e){const t={};return $(e,(a,n)=>{t[n]=a.value}),t}let S=!1;try{window&&(S=!0)}catch{S=!1}var p=S;const y=Object.freeze({"+":(e,t)=>e+t,"-":(e,t)=>e-t}),O=6e4,H=O*60*24,P=H*7,J=H*31,N=[".svg",".svgz"],W=[".tiff",".pjp",".jfif",".bmp",".gif",".png",".xbm",".dib",".jxl",".jpeg",".jpg",".webp",".ico",".tif",".pjpeg",".avif"],Q=[".ogm",".wmv",".mpg",".webm",".ogv",".mov",".asx",".mpeg",".mp4",".m4v",".avi"],X=[".opus",".flac",".webm",".weba",".wav",".ogg",".m4a",".oga",".mid",".mp3",".aiff",".wma",".au"];function G(e,t){Object.entries(t).forEach(([a,n])=>{e[a]=n})}function ee(e){return!(e.length<4||e[e.length-1]!==")"||e[0]!=="("||e[1]!=="?"||e[2]!=="="&&e[2]!=="!")}function te(e){const t=new Date(e.getTime());t.setHours(0,0,0,0),t.setDate(t.getDate()+3-(t.getDay()+6)%7);const a=new Date(t.getFullYear(),0,4);return 1+Math.round(((t.getTime()-a.getTime())/864e5-3+(a.getDay()+6)%7)/7)}function v(e){const t=String(e);return t.length===0?"00":t.length===1?`0${t}`:t}function R(e){return v(e.getDate())}function Y(e,t=":"){return`${v(e.getHours())}${t}${v(e.getMinutes())}`}function j(e,t="-"){return`${e.getFullYear()}${t}${v(e.getMonth()+1)}`}function C(e){const t=typeof e=="string"?new Date(e):e;if(t instanceof Date&&!Number.isNaN(t.getTime()))return t}function Z(e,t,a){const n=C(e);if(!!n)switch(t){case"date":return new Date(y[a](n.getTime(),H));case"datetime-local":return new Date(y[a](n.getTime(),O));case"month":return new Date(y[a](n.getTime(),J));case"time":return new Date(y[a](n.getTime(),O));case"week":return new Date(y[a](n.getTime(),P));default:return}}function x(e,t){const a=C(e);if(!a)return"";switch(t){case"date":return`${j(a)}-${R(a)}`;case"datetime-local":return`${j(a)}-${R(a)}T${Y(a)}`;case"month":return j(a);case"time":return Y(a);case"week":return`${a.getFullYear()}-W${v(te(a))}`;default:return""}}function D(e){const t=e.lastIndexOf(".");if(t!==-1)return e.substring(t)}function re(e){const t=D(e);return Q.some(a=>a===t)}function ne(e){const t=D(e);return X.some(a=>a===t)}function z(e,t){const a=D(e);switch(t){case"raster":return W.some(n=>n===a);case"vector":return N.some(n=>n===a);case void 0:return W.some(n=>n===a)||N.some(n=>n===a);default:return!1}}function d(e,t){switch(t){case"b":return e;case"kb":return e*1024;case"mb":return e*1048576;case"gb":return e*1073741824;case"tb":return e*1099511627776;default:return-1}}function E(e){let t=!1;$(e,a=>{a.invalid&&(t=!0)}),e.$st.invalid=t,e.$st.errors=e.$fn.getErrors()}function T(e,t,a){let{mode:n}=a;a.mode==="default"&&(p?n="js-only":n="html-only"),t.$el&&(n==="js-only"?t.$el.noValidate=!0:t.$el.noValidate=!1);let r="";const i=[],s={};e.validators.forEach(u=>{if(p&&n!=="html-only"){const B=u.js(e.value,t);B!==void 0&&i.push(B)}if(!e.type)return;const{pattern:f,...M}=u.html(e.type,t);f&&ee(f)&&(r+=f),G(s,M)}),r&&(s.pattern=`^${r}.+$`),e.errors=i,e.errors.length>0?e.invalid=!0:e.invalid=!1,G(e.attributes,s)}function ae(e){return Object.entries(e).reduce((t,[a,{attributes:n,...r}])=>{const i={errors:[],touched:!1,validators:[],invalid:!1,type:null,attributes:{name:a,...n},...r};return{...t,[a]:i}},{})}function q(e,t){e.forEach(a=>a(t))}function se(e,t=V){const a=t===V?V:K(t),n=[],r={...ae(e),$st:{invalid:!0,submitted:!1,initial:Object.freeze(e),form:null,errors:[]},$fn:{submit:s=>{s==null||s.preventDefault(),r.$st.submitted=!0,r.$fn.untouch(),r.$on.submit(s)},reset:()=>{Object.keys(r.$initial).forEach(s=>{r[s].touched=!1,r[s].value=r.$st.initial[s].value,T(r[s],r,a)}),E(r),q(n,r)},untouch:()=>{$(r,s=>{s.touched=!1}),q(n,r)},getErrors:()=>{let s=[];return $(r,u=>{u.errors.length>0&&(s=[...s,...u.errors])}),s}},$on:{submit:()=>{},touch:()=>{}},$el:null};$(r,s=>T(s,r,a)),E(r);let i=F(r);return{subscribe(s){return s(r),n.push(s),()=>n.splice(n.indexOf(s),1)},set(s){s.$el&&!s.$el.hasSvelidateListener&&(s.$el.addEventListener("submit",u=>s.$fn.submit(u)),s.$el.hasSvelidateListener=!0),$(s,(u,f)=>{i[f]!==u.value&&(u.touched||(u.touched=!0,s.$on.touch(f))),T(u,r,a)}),E(s),q(n,s),i=F(s)}}}function ie(e,t=()=>({}),a=!0){let n=!0;return!p&&!a&&(n=!1),(r="")=>Object.freeze({js:(i,s)=>{if(!e(i,s))return r},html:n?t:()=>({})})}function A(e,t=()=>({}),a=!0){let n=!0;return!p&&!a&&(n=!1),(r="")=>Object.freeze({js:(i,s)=>{if(!e(!!i,s))return r},html:n?t:()=>({})})}function g(e,t=()=>({}),a=!0){let n=!0;return!p&&!a&&(n=!1),(r="")=>Object.freeze({js:(i,s)=>{const u=typeof i=="string"?i:i==null?void 0:i.toString();if(typeof u!="string")return r;if(!e(u,s))return r},html:n?t:()=>({})})}function b(e,t=()=>({}),a=!0){let n=!0;return!p&&!a&&(n=!1),(r="")=>Object.freeze({js:(i,s)=>{const u=typeof i=="number"?i:parseFloat(String(i));if(Number.isNaN(u))return r;if(!e(u,s))return r},html:n?t:()=>({})})}function w(e,t=()=>({}),a=!0){let n=!0;return!p&&!a&&(n=!1),(r="")=>Object.freeze({js:(i,s)=>{const u=C(i);if(!u)return r;if(!e(u,s))return r},html:n?t:()=>({})})}function c(e,t=()=>({}),a=!0){let n=!0;return!p&&!a&&(n=!1),(r="")=>Object.freeze({js:(i,s)=>{try{return i instanceof FileList&&e(i,s)?void 0:r}catch{return r}},html:n?t:()=>({})})}function _(e,t){return{js:(a,n)=>{if(e(a,n))return t.js(a,n)},html:t.html}}function ue(e,t){return Array.isArray(t)?t.map(a=>_(e,a)):_(e,t)}function L(e,t,a){return typeof e=="function"?[n=>{const r=e(n);return t(r)?r:a},!1]:[()=>e,!0]}function k(e){return L(e,t=>typeof t=="string","")}function o(e){return L(e,t=>typeof t=="number",-1)}function m(e){return L(e,t=>t instanceof Date,new Date(0))}const I={textarea:["textarea"],select:["select-multiple","select-one"],color:["color"],radio:["radio"],file:["file"],numbers:["number","range"],dates:["date","datetime-local","month","time","week"],strings:["email","password","search","tel","text","url"],checkbox:["checkbox"],other:["hidden","reset","submit"]};function l(e,t){let a={};return e&&Object.keys(I).every(n=>{var i,s;const r=n;return I[r].some(u=>u===e)?(a=(s=(i=t[r])==null?void 0:i.call(t,e))!=null?s:{},!1):!0}),a}const oe={required:b(()=>!0,e=>l(e,{numbers:()=>({required:!0})})),gt(e){const[t,a]=o(e);return b((n,r)=>n>t(r),(n,r)=>l(n,{numbers:()=>({min:t(r)+1})}),a)},gte(e){const[t,a]=o(e);return b((n,r)=>n>=t(r),(n,r)=>l(n,{numbers:()=>({min:t(r)})}),a)},lt(e){const[t,a]=o(e);return b((n,r)=>n<t(r),(n,r)=>l(n,{numbers:()=>({max:t(r)-1})}),a)},lte(e){const[t,a]=o(e);return b((n,r)=>n<=t(r),(n,r)=>l(n,{numbers:()=>({max:t(r)})}),a)},inside(e,t){const[a,n]=o(e),[r,i]=o(t);return b((s,u)=>s>=a(u)&&s<=r(u),(s,u)=>l(s,{numbers:()=>({min:a(u),max:r(u)})}),n&&i)},outside(e,t){const[a,n]=o(e),[r,i]=o(t);return b((s,u)=>s<a(u)&&s>r(u),()=>({}),n&&i)},neq(e){const[t,a]=o(e);return b((n,r)=>n!==t(r),()=>({}),a)},eq(e){const[t,a]=o(e);return b((n,r)=>n===t(r),(n,r)=>l(n,{numbers:()=>({min:t(r),max:t(r)})}),a)}},le={required:g(e=>e.length>0,e=>l(e,{strings:()=>({required:!0}),textarea:()=>({required:!0})})),email:g(e=>new RegExp(V.pattern.email).test(e),e=>l(e,{strings:()=>({pattern:`(?=^${V.pattern.email}$)`,minLength:5}),textarea:()=>({minLength:5})})),upperCase:g(e=>e.toLowerCase()!==e,e=>l(e,{strings:()=>({pattern:"(?=.*[A-Z])"})})),lowerCase:g(e=>e.toUpperCase()!==e,e=>l(e,{strings:()=>({pattern:"(?=.*[a-z])"})})),number:g(e=>/[0-9]/.test(e),e=>l(e,{strings:()=>({pattern:"(?=.*[0-9])"})})),symbol:g(e=>new RegExp(V.pattern.symbol).test(e),e=>l(e,{strings:()=>({pattern:`(?=.*${V.pattern.symbol})`})})),regex(e){const[t,a]=L(e,n=>n instanceof RegExp,/$^/);return g((n,r)=>t(r).test(n),(n,r)=>l(n,{strings:()=>({pattern:`(?=.*${t(r).source})`})}),a)},eq(e){const[t,a]=k(e);return g((n,r)=>n===t(r),(n,r)=>l(n,{strings:()=>({pattern:`(?=^${t(r)}$)`})}),a)},neq(e){const[t,a]=k(e);return g((n,r)=>n!==t(r),(n,r)=>l(n,{strings:()=>({pattern:`(?!${t(r)}$)`})}),a)},length:{gt(e){const[t,a]=o(e);return g((n,r)=>n.length>t(r),(n,r)=>l(n,{strings:()=>({minLength:Math.floor(t(r))+1}),textarea:()=>({minLength:Math.floor(t(r))+1})}),a)},gte(e){const[t,a]=o(e);return g((n,r)=>n.length>=t(r),(n,r)=>l(n,{strings:()=>({minLength:Math.floor(t(r))}),textarea:()=>({minLength:Math.floor(t(r))})}),a)},lt(e){const[t,a]=o(e);return g((n,r)=>n.length<t(r),(n,r)=>l(n,{strings:()=>({maxLength:Math.floor(t(r))-1}),textarea:()=>({maxLength:Math.floor(t(r))-1})}),a)},lte(e){const[t,a]=o(e);return g((n,r)=>n.length<=t(r),(n,r)=>l(n,{strings:()=>({maxLength:Math.floor(t(r))}),textarea:()=>({maxLength:Math.floor(t(r))})}),a)},inside(e,t){const[a,n]=o(e),[r,i]=o(t);return g((s,u)=>s.length>=a(u)&&s.length<=r(u),(s,u)=>l(s,{strings:()=>({minLength:Math.floor(a(u)),maxLength:Math.floor(r(u))}),textarea:()=>({minLength:Math.floor(a(u)),maxLength:Math.floor(r(u))})}),n&&i)},outside(e,t){const[a,n]=o(e),[r,i]=o(t);return g((s,u)=>s.length<r(u)&&s.length>r(u),(s,u)=>l(s,{strings:()=>({pattern:`(?=(.{0,${Math.floor(Math.max(0,a(u)-1))}}|.{${Math.floor(r(u)+1)},})$)`})}),n&&i)},neq(e){const[t,a]=o(e);return g((n,r)=>n.length!==t(r),(n,r)=>l(n,{strings:()=>({pattern:`(?=(.{0,${Math.floor(Math.max(Math.floor(t(r))-1,0))}}|.{${Math.floor(Math.floor(t(r))+1)},})$)`})}),a)},eq(e){const[t,a]=o(e);return g((n,r)=>n.length===t(r),(n,r)=>l(n,{strings:()=>({minLength:Math.floor(t(r)),maxLength:Math.floor(t(r))}),textarea:()=>({minLength:Math.floor(t(r)),maxLength:Math.floor(t(r))})}),a)}}},ce={true:A(e=>e,e=>l(e,{checkbox:()=>({required:!0})})),false:A(e=>!e,()=>({}))},ge={required:w(()=>!0,e=>l(e,{dates:()=>({required:!0})})),gt(e){const[t,a]=m(e);return w((n,r)=>n>t(r),(n,r)=>l(n,{dates:i=>{const s=Z(t(r),i,"+");return s?{min:x(s,i)}:{}}}),a)},gte(e){const[t,a]=m(e);return w((n,r)=>n>=t(r),(n,r)=>l(n,{dates:i=>({min:x(t(r),i)})}),a)},lt(e){const[t,a]=m(e);return w((n,r)=>n<t(r),(n,r)=>l(n,{dates:i=>{const s=Z(t(r),i,"-");return s?{max:x(s,i)}:{}}}),a)},lte(e){const[t,a]=m(e);return w((n,r)=>n<=t(r),(n,r)=>l(n,{dates:i=>({max:x(t(r),i)})}),a)},inside(e,t){const[a,n]=m(e),[r,i]=m(t);return w(s=>e<=s&&s<=t,(s,u)=>l(s,{dates:f=>({min:x(a(u),f),max:x(r(u),f)})}),n&&i)},outside(e,t){const[a,n]=m(e),[r,i]=m(t);return w((s,u)=>s<a(u)||s>r(u),()=>({}),n&&i)},neq(e){const[t,a]=m(e);return w((n,r)=>n!==t(r),()=>({}),a)},eq(e){const[t,a]=m(e);return w((n,r)=>n===t(r),(n,r)=>l(n,{dates:i=>({min:x(t(r),i),max:x(t(r),i)})}),a)}};function h(e,t){for(let a=0;a<e.length;a+=1)if(!t(e[a]))return!1;return!0}const fe={required:c(e=>e.length>0,()=>({required:!0})),files:{type:{image:c(e=>h(e,t=>z(t.name)),e=>l(e,{file:()=>({accept:"image/*"})})),raster:c(e=>h(e,t=>z(t.name,"raster")),e=>l(e,{file:()=>({accept:W.join(",")})})),vector:c(e=>h(e,t=>z(t.name,"vector")),e=>l(e,{file:()=>({accept:N.join(",")})})),video:c(e=>h(e,t=>re(t.name)),e=>l(e,{file:()=>({accept:"video/*"})})),audio:c(e=>h(e,t=>ne(t.name)),e=>l(e,{file:()=>({accept:"audio/*"})})),is:e=>{const[t,a]=L(e,n=>Array.isArray(n)&&n.every(r=>typeof r=="string"),[]);return c((n,r)=>h(n,i=>t(r).some(s=>s===D(i.name))),(n,r)=>l(n,{file:()=>({accept:t(r).join(",")})}),a)}},size:{gt(e,t="b"){const[a,n]=o(e);return c((r,i)=>h(r,s=>s.size>d(a(i),t)),()=>({}),n)},gte(e,t="b"){const[a,n]=o(e);return c((r,i)=>h(r,s=>s.size>=d(a(i),t)),()=>({}),n)},lt(e,t="b"){const[a,n]=o(e);return c((r,i)=>h(r,s=>s.size<d(a(i),t)),()=>({}),n)},lte(e,t="b"){const[a,n]=o(e);return c((r,i)=>h(r,s=>s.size<=d(a(i),t)),()=>({}),n)},inside(e,t,a="b"){const[n,r]=o(e),[i,s]=o(t);return c((u,f)=>h(u,M=>M.size>=d(n(f),a)&&M.size<=d(i(f),a)),()=>({}),r&&s)},outside(e,t,a="b"){const[n,r]=o(e),[i,s]=o(t);return c((u,f)=>h(u,M=>M.size<d(n(f),a)||M.size>d(i(f),a)),()=>({}),r&&s)},neq(e,t="b"){const[a,n]=o(e);return c((r,i)=>h(r,s=>s.size!==d(a(i),t)),()=>({}),n)},eq(e,t="b"){const[a,n]=o(e);return c((r,i)=>h(r,s=>s.size===d(a(i),t)),()=>({}),n)}}},length:{gt(e){const[t,a]=o(e);return c((n,r)=>n.length>t(r),()=>({}),a)},gte(e){const[t,a]=o(e);return c((n,r)=>n.length>=t(r),()=>({}),a)},lt(e){const[t,a]=o(e);return c((n,r)=>n.length<t(r),()=>({}),a)},lte(e){const[t,a]=o(e);return c((n,r)=>n.length<=t(r),()=>({}),a)},inside(e,t){const[a,n]=o(e),[r,i]=o(t);return c((s,u)=>s.length>=a(u)&&s.length<=r(u),()=>({}),n&&i)},outside(e,t){const[a,n]=o(e),[r,i]=o(t);return c((s,u)=>s.length<a(u)&&s.length>r(u),()=>({}),n&&i)},neq(e){const[t,a]=o(e);return c((n,r)=>n.length!==t(r),()=>({}),a)},eq(e){const[t,a]=o(e);return c((n,r)=>n.length===t(r),()=>({}),a)}}};exports.boolean=ce;exports.createBooleanValidatorWrapperFactory=A;exports.createDateValidatorWrapperFactory=w;exports.createFileListValidatorWrapperFactory=c;exports.createNumberValidatorWrapperFactory=b;exports.createStringValidatorWrapperFactory=g;exports.createValidatorWrapperFactory=ie;exports.date=ge;exports.filelist=fe;exports.getMatchingHtmlValidator=l;exports.number=oe;exports.string=le;exports.svelidate=se;exports.svelidateConfig=V;exports.validateIf=ue;