@esmj/schema 0.7.0 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -440,6 +440,7 @@ const schema = s.object({
440
440
  - `.nullable()` - Makes field nullable
441
441
  - `.nullish()` - Makes field optional and nullable
442
442
  - `.default(value)` - Sets default value
443
+ - `.catch(value)` - Returns fallback value on any parse failure
443
444
 
444
445
  ### Transformations
445
446
 
@@ -800,6 +801,35 @@ Sets a default value for the schema.
800
801
  const defaultSchema = stringSchema.default('default value');
801
802
  ```
802
803
 
804
+ #### `catch(catchValue)`
805
+
806
+ Returns a fallback value whenever parsing fails, instead of throwing or returning an error.
807
+ Unlike `default()` which only fires when the input is `undefined`, `catch()` fires on **any** validation failure.
808
+
809
+ The fallback can be a static value or a function that receives a context object `{ input, error }`:
810
+ - `input` — the original raw input value
811
+ - `error` — the `ErrorStructure` with the failure message
812
+
813
+ **Note:** The fallback value is returned as-is without re-validation. `catch()` only intercepts failures from schemas and refinements placed **before** it in the chain.
814
+
815
+ ```typescript
816
+ // Static fallback
817
+ const schema = s.string().catch('unknown');
818
+ schema.parse(123); // 'unknown'
819
+ schema.parse('hello'); // 'hello'
820
+
821
+ // Function fallback with context
822
+ const schema2 = s.number().catch((ctx) => {
823
+ console.warn(`Invalid input: ${ctx.input} — ${ctx.error.message}`);
824
+ return 0;
825
+ });
826
+ schema2.parse('bad'); // 0
827
+
828
+ // Distinction from default()
829
+ s.string().catch('fallback').parse(null); // 'fallback' — catch fires for null
830
+ s.string().default('fallback').parse(null); // throws — default does not fire for null
831
+ ```
832
+
803
833
  #### `transform(callback)`
804
834
 
805
835
  Transforms the parsed value using the provided callback.
@@ -1388,4 +1418,3 @@ const userSchema = s.object({
1388
1418
  ## License
1389
1419
 
1390
1420
  MIT
1391
-
package/dist/array.cjs CHANGED
@@ -1 +1 @@
1
- 'use strict';var k={abortEarly:true};function y(e,c){if(!c)return e;let i=e?.cause?.key?`${c}.${e.cause.key}`:`${c}`;return {message:`Error parsing key "${i}": ${e.message}`,cause:{key:i}}}function T(e,c,i){if(e.errors?.length)for(let u=0;u<e.errors.length;u++)c.push(y(e.errors[u],i));}function S(e){return {...k,...e}}var w=e=>typeof e=="string"||e instanceof String,P=e=>(typeof e=="number"||e instanceof Number)&&!Number.isNaN(e),A=e=>e===true||e===false,E=e=>e instanceof Date&&!Number.isNaN(e.getTime()),_=e=>Array.isArray(e),b=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),R={object(e,c){let i=h(b,{...c,type:"object"});return i._getDescription=()=>`object({ ${Object.entries(e).map(([o,r])=>`${o}: ${r._getDescription()}`).join(", ")} })`,l(i,"_parse",(u,o,r)=>{let t=u(o,r),{abortEarly:n}=S(r);if(t.success===false)return t;let a={},s=[];for(let p in e){let f=e[p]._parse(t.data[p],r);if(f.success)a[p]=f.data;else {if(f=f,n!==false){let m=y(f.error,p);return {success:false,error:m,errors:[m]}}T(f,s,p);}}return s.length>0?{success:false,error:s[0],errors:s}:{success:true,data:a}}),i},string(e){return h(w,{...e,type:"string"})},number(e){return h(P,{...e,type:"number"})},boolean(e){return h(A,{...e,type:"boolean"})},date(e){return h(E,{...e,type:"date"})},enum(e,c){let i=t=>e.includes(t),u=t=>`Invalid ${o} value. Expected ${e.map(n=>`"${n}"`).join(" | ")}, received "${t}".`,o="enum",r=h(i,{message:u,...c,type:o});return r._getDescription=()=>`enum(${e.map(t=>`"${t}"`).join(" | ")})`,r},array(e,c){let i=h(_,{...c,type:"array"});return i._getDescription=()=>`array(${e._getDescription()})`,l(i,"_parse",(u,o,r)=>{let t=u(o,r),{abortEarly:n}=S(r);if(t.success===false)return t;let a=[],s=[];for(let p=0;p<t.data.length;p++){let f=e._parse(t.data[p],r);if(f.success)a.push(f.data);else {if(f=f,n!==false){let m=y(f.error,p);return {success:false,error:m,errors:[m]}}T(f,s,p);}}return s.length>0?{success:false,error:s[0],errors:s}:{success:true,data:a}}),i},any(){return h(()=>true)},preprocess(e,c){return l(c,"_parse",(i,u)=>(u=e(u),i(u))),c},union(e,c){return h(r=>{for(let t=0;t<e.length;t++){let n=e[t]._parse(r);if(n.success)return n}return false},{message:r=>`Invalid union value. Expected the value to match one of the schemas:${e.map((t,n)=>` ${n+1}. ${t._getDescription()}`).join(",")} but received "${typeof r}" with value: ${b(r)?JSON.stringify(r):`"${r}"`}`,...c,type:"union"})},literal(e,c){let o=h(r=>r===e,{message:r=>c?.message?typeof c.message=="function"?c.message(r):c.message:`Expected literal value "${e}", received "${r}"`,name:c?.name,type:"literal"});return o._getDescription=()=>`literal("${e}")`,o}};function O(e){return c=>`The value "${c}" must be type of ${e} but is type of "${typeof c}".`}function l(e,c,i){let u=e[c];e[c]=(...o)=>i(u,...o);}function h(e,{type:c="any",name:i,message:u}={}){u=u||O(c);let o={name:i,message:u,type:c},r={_getName(){return i},_getType(){return c},_getDescription(){return this._getName()??this._getType()},_parse(t,n){let a=e(t);if(a===true)return {success:true,data:t};if(typeof a=="object"&&a?.success===true)return a;let s={message:typeof u=="function"?u(t):u};return {success:false,error:s,errors:[s]}},parse(t,n){let a=r._parse(t,n);if(!a.success)throw a=a,new Error(a.error.message,{cause:a.error.cause});return a.data},safeParse(t,n){return r._parse(t,n)},transform(t){return l(this,"_parse",(n,a,s)=>{let p=n(a,s);return p.success&&(p.data=t(p.data)),p}),this},optional(){return l(this,"_parse",(t,n,a)=>{let s=t(n,a);return !s.success&&n===void 0&&(s.data=void 0,s.success=true),s}),this},nullable(){return l(this,"_parse",(t,n,a)=>{let s=t(n,a);return !s.success&&n===null&&(s.data=null,s.success=true),s}),this},nullish(){return l(this,"_parse",(t,n,a)=>{let s=t(n,a);return !s.success&&n==null&&(s.success=true,s.data=n),s}),this},default(t){return l(this,"_parse",(n,a,s)=>(a===void 0&&(a=typeof t=="function"?t():t),n(a,s))),this},pipe(t){return l(this,"_parse",(n,a,s)=>{let p=n(a,s);return p.success?t._parse(p.data,s):p}),this},refine(t,{message:n,type:a}={}){return a&&(n=n||O(a),c=a),l(this,"_parse",(s,p,f)=>{let m=s(p,f);S(f);if(!m.success)return m;let I=t(m.data);if(I===true||typeof I=="object"&&I?.success===true)return m;let g={message:typeof n=="function"?n(p):n};return {success:false,error:g,errors:[g]}}),this}};return d.length>0?d.reduce((t,n)=>n(t,e,o)??t,r):r}var d=[];function x(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");d.push(e);}x((e,c,i)=>{if(i?.type==="array"){let u=e;u.min=function(o,{message:r}={}){return this.refine(t=>t.length>=o,{message:r||`Array must contain at least ${o} items.`})},u.max=function(o,{message:r}={}){return this.refine(t=>t.length<=o,{message:r||`Array must contain at most ${o} items.`})},u.length=function(o,{message:r}={}){return this.refine(t=>t.length===o,{message:r||`Array must contain exactly ${o} items.`})},u.nonEmpty=function({message:o}={}){return this.refine(r=>r.length>0,{message:o||"Array must not be empty."})},u.unique=function({message:o}={}){return this.refine(r=>{let t=new Set;try{return r.every(n=>{let a=JSON.stringify(n);return t.has(a)?!1:(t.add(a),!0)})}catch{return new Set(r).size===r.length}},{message:o||"Array items must be unique."})},u.sort=function(){return this.transform(o=>[...o].sort())},u.reverse=function(){return this.transform(o=>[...o].reverse())};}return e});exports.extend=x;exports.hookOriginal=l;exports.s=R;
1
+ 'use strict';var k={abortEarly:true};function y(e,c){if(!c)return e;let p=e?.cause?.key?`${c}.${e.cause.key}`:`${c}`;return {message:`Error parsing key "${p}": ${e.message}`,cause:{key:p}}}function T(e,c,p){if(e.errors?.length)for(let u=0;u<e.errors.length;u++)c.push(y(e.errors[u],p));}function S(e){return {...k,...e}}var w=e=>typeof e=="string"||e instanceof String,P=e=>(typeof e=="number"||e instanceof Number)&&!Number.isNaN(e),A=e=>e===true||e===false,E=e=>e instanceof Date&&!Number.isNaN(e.getTime()),_=e=>Array.isArray(e),O=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),R={object(e,c){let p=l(O,{...c,type:"object"});return p._getDescription=()=>`object({ ${Object.entries(e).map(([o,r])=>`${o}: ${r._getDescription()}`).join(", ")} })`,m(p,"_parse",(u,o,r)=>{let t=u(o,r),{abortEarly:n}=S(r);if(t.success===false)return t;let a={},s=[];for(let i in e){let f=e[i]._parse(t.data[i],r);if(f.success)a[i]=f.data;else {if(f=f,n!==false){let h=y(f.error,i);return {success:false,error:h,errors:[h]}}T(f,s,i);}}return s.length>0?{success:false,error:s[0],errors:s}:{success:true,data:a}}),p},string(e){return l(w,{...e,type:"string"})},number(e){return l(P,{...e,type:"number"})},boolean(e){return l(A,{...e,type:"boolean"})},date(e){return l(E,{...e,type:"date"})},enum(e,c){let p=t=>e.includes(t),u=t=>`Invalid ${o} value. Expected ${e.map(n=>`"${n}"`).join(" | ")}, received "${t}".`,o="enum",r=l(p,{message:u,...c,type:o});return r._getDescription=()=>`enum(${e.map(t=>`"${t}"`).join(" | ")})`,r},array(e,c){let p=l(_,{...c,type:"array"});return p._getDescription=()=>`array(${e._getDescription()})`,m(p,"_parse",(u,o,r)=>{let t=u(o,r),{abortEarly:n}=S(r);if(t.success===false)return t;let a=[],s=[];for(let i=0;i<t.data.length;i++){let f=e._parse(t.data[i],r);if(f.success)a.push(f.data);else {if(f=f,n!==false){let h=y(f.error,i);return {success:false,error:h,errors:[h]}}T(f,s,i);}}return s.length>0?{success:false,error:s[0],errors:s}:{success:true,data:a}}),p},any(){return l(()=>true)},preprocess(e,c){return m(c,"_parse",(p,u)=>(u=e(u),p(u))),c},union(e,c){return l(r=>{for(let t=0;t<e.length;t++){let n=e[t]._parse(r);if(n.success)return n}return false},{message:r=>`Invalid union value. Expected the value to match one of the schemas:${e.map((t,n)=>` ${n+1}. ${t._getDescription()}`).join(",")} but received "${typeof r}" with value: ${O(r)?JSON.stringify(r):`"${r}"`}`,...c,type:"union"})},literal(e,c){let o=l(r=>r===e,{message:r=>c?.message?typeof c.message=="function"?c.message(r):c.message:`Expected literal value "${e}", received "${r}"`,name:c?.name,type:"literal"});return o._getDescription=()=>`literal("${e}")`,o}};function b(e){return c=>`The value "${c}" must be type of ${e} but is type of "${typeof c}".`}function m(e,c,p){let u=e[c];e[c]=(...o)=>p(u,...o);}function l(e,{type:c="any",name:p,message:u}={}){u=u||b(c);let o={name:p,message:u,type:c},r={_getName(){return p},_getType(){return c},_getDescription(){return this._getName()??this._getType()},_parse(t,n){let a=e(t);if(a===true)return {success:true,data:t};if(typeof a=="object"&&a?.success===true)return a;let s={message:typeof u=="function"?u(t):u};return {success:false,error:s,errors:[s]}},parse(t,n){let a=r._parse(t,n);if(!a.success)throw a=a,new Error(a.error.message,{cause:a.error.cause});return a.data},safeParse(t,n){return r._parse(t,n)},transform(t){return m(this,"_parse",(n,a,s)=>{let i=n(a,s);return i.success&&(i.data=t(i.data)),i}),this},optional(){return m(this,"_parse",(t,n,a)=>{let s=t(n,a);return !s.success&&n===void 0&&(s.data=void 0,s.success=true),s}),this},nullable(){return m(this,"_parse",(t,n,a)=>{let s=t(n,a);return !s.success&&n===null&&(s.data=null,s.success=true),s}),this},nullish(){return m(this,"_parse",(t,n,a)=>{let s=t(n,a);return !s.success&&n==null&&(s.success=true,s.data=n),s}),this},default(t){return m(this,"_parse",(n,a,s)=>(a===void 0&&(a=typeof t=="function"?t():t),n(a,s))),this},catch(t){return m(this,"_parse",(n,a,s)=>{let i=n(a,s);return i.success?i:{success:true,data:typeof t=="function"?t({input:a,error:i.error}):t}}),this},pipe(t){return m(this,"_parse",(n,a,s)=>{let i=n(a,s);return i.success?t._parse(i.data,s):i}),this},refine(t,{message:n,type:a}={}){return a&&(n=n||b(a),c=a),m(this,"_parse",(s,i,f)=>{let h=s(i,f);S(f);if(!h.success)return h;let I=t(h.data);if(I===true||typeof I=="object"&&I?.success===true)return h;let g={message:typeof n=="function"?n(i):n};return {success:false,error:g,errors:[g]}}),this}};return d.length>0?d.reduce((t,n)=>n(t,e,o)??t,r):r}var d=[];function x(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");d.push(e);}x((e,c,p)=>{if(p?.type==="array"){let u=e;u.min=function(o,{message:r}={}){return this.refine(t=>t.length>=o,{message:r||`Array must contain at least ${o} items.`})},u.max=function(o,{message:r}={}){return this.refine(t=>t.length<=o,{message:r||`Array must contain at most ${o} items.`})},u.length=function(o,{message:r}={}){return this.refine(t=>t.length===o,{message:r||`Array must contain exactly ${o} items.`})},u.nonEmpty=function({message:o}={}){return this.refine(r=>r.length>0,{message:o||"Array must not be empty."})},u.unique=function({message:o}={}){return this.refine(r=>{let t=new Set;try{return r.every(n=>{let a=JSON.stringify(n);return t.has(a)?!1:(t.add(a),!0)})}catch{return new Set(r).size===r.length}},{message:o||"Array items must be unique."})},u.sort=function(){return this.transform(o=>[...o].sort())},u.reverse=function(){return this.transform(o=>[...o].reverse())};}return e});exports.extend=x;exports.hookOriginal=m;exports.s=R;
package/dist/array.mjs CHANGED
@@ -1 +1 @@
1
- import'./chunk-ELVLLQRH.mjs';export{c as extend,b as hookOriginal,a as s}from'./chunk-V2T3CZFC.mjs';
1
+ import'./chunk-4JJPVRF6.mjs';export{c as extend,b as hookOriginal,a as s}from'./chunk-2NCXW7ID.mjs';
@@ -0,0 +1 @@
1
+ var k={abortEarly:true};function d(e,s){if(!s)return e;let i=e?.cause?.key?`${s}.${e.cause.key}`:`${s}`;return {message:`Error parsing key "${i}": ${e.message}`,cause:{key:i}}}function T(e,s,i){if(e.errors?.length)for(let o=0;o<e.errors.length;o++)s.push(d(e.errors[o],i));}function y(e){return {...k,...e}}var x=e=>typeof e=="string"||e instanceof String,w=e=>(typeof e=="number"||e instanceof Number)&&!Number.isNaN(e),P=e=>e===true||e===false,E=e=>e instanceof Date&&!Number.isNaN(e.getTime()),_=e=>Array.isArray(e),b=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),$={object(e,s){let i=h(b,{...s,type:"object"});return i._getDescription=()=>`object({ ${Object.entries(e).map(([p,c])=>`${p}: ${c._getDescription()}`).join(", ")} })`,m(i,"_parse",(o,p,c)=>{let t=o(p,c),{abortEarly:n}=y(c);if(t.success===false)return t;let a={},r=[];for(let u in e){let f=e[u]._parse(t.data[u],c);if(f.success)a[u]=f.data;else {if(f=f,n!==false){let l=d(f.error,u);return {success:false,error:l,errors:[l]}}T(f,r,u);}}return r.length>0?{success:false,error:r[0],errors:r}:{success:true,data:a}}),i},string(e){return h(x,{...e,type:"string"})},number(e){return h(w,{...e,type:"number"})},boolean(e){return h(P,{...e,type:"boolean"})},date(e){return h(E,{...e,type:"date"})},enum(e,s){let i=t=>e.includes(t),o=t=>`Invalid ${p} value. Expected ${e.map(n=>`"${n}"`).join(" | ")}, received "${t}".`,p="enum",c=h(i,{message:o,...s,type:p});return c._getDescription=()=>`enum(${e.map(t=>`"${t}"`).join(" | ")})`,c},array(e,s){let i=h(_,{...s,type:"array"});return i._getDescription=()=>`array(${e._getDescription()})`,m(i,"_parse",(o,p,c)=>{let t=o(p,c),{abortEarly:n}=y(c);if(t.success===false)return t;let a=[],r=[];for(let u=0;u<t.data.length;u++){let f=e._parse(t.data[u],c);if(f.success)a.push(f.data);else {if(f=f,n!==false){let l=d(f.error,u);return {success:false,error:l,errors:[l]}}T(f,r,u);}}return r.length>0?{success:false,error:r[0],errors:r}:{success:true,data:a}}),i},any(){return h(()=>true)},preprocess(e,s){return m(s,"_parse",(i,o)=>(o=e(o),i(o))),s},union(e,s){return h(c=>{for(let t=0;t<e.length;t++){let n=e[t]._parse(c);if(n.success)return n}return false},{message:c=>`Invalid union value. Expected the value to match one of the schemas:${e.map((t,n)=>` ${n+1}. ${t._getDescription()}`).join(",")} but received "${typeof c}" with value: ${b(c)?JSON.stringify(c):`"${c}"`}`,...s,type:"union"})},literal(e,s){let p=h(c=>c===e,{message:c=>s?.message?typeof s.message=="function"?s.message(c):s.message:`Expected literal value "${e}", received "${c}"`,name:s?.name,type:"literal"});return p._getDescription=()=>`literal("${e}")`,p}};function O(e){return s=>`The value "${s}" must be type of ${e} but is type of "${typeof s}".`}function m(e,s,i){let o=e[s];e[s]=(...p)=>i(o,...p);}function h(e,{type:s="any",name:i,message:o}={}){o=o||O(s);let p={name:i,message:o,type:s},c={_getName(){return i},_getType(){return s},_getDescription(){return this._getName()??this._getType()},_parse(t,n){let a=e(t);if(a===true)return {success:true,data:t};if(typeof a=="object"&&a?.success===true)return a;let r={message:typeof o=="function"?o(t):o};return {success:false,error:r,errors:[r]}},parse(t,n){let a=c._parse(t,n);if(!a.success)throw a=a,new Error(a.error.message,{cause:a.error.cause});return a.data},safeParse(t,n){return c._parse(t,n)},transform(t){return m(this,"_parse",(n,a,r)=>{let u=n(a,r);return u.success&&(u.data=t(u.data)),u}),this},optional(){return m(this,"_parse",(t,n,a)=>{let r=t(n,a);return !r.success&&n===void 0&&(r.data=void 0,r.success=true),r}),this},nullable(){return m(this,"_parse",(t,n,a)=>{let r=t(n,a);return !r.success&&n===null&&(r.data=null,r.success=true),r}),this},nullish(){return m(this,"_parse",(t,n,a)=>{let r=t(n,a);return !r.success&&n==null&&(r.success=true,r.data=n),r}),this},default(t){return m(this,"_parse",(n,a,r)=>(a===void 0&&(a=typeof t=="function"?t():t),n(a,r))),this},catch(t){return m(this,"_parse",(n,a,r)=>{let u=n(a,r);return u.success?u:{success:true,data:typeof t=="function"?t({input:a,error:u.error}):t}}),this},pipe(t){return m(this,"_parse",(n,a,r)=>{let u=n(a,r);return u.success?t._parse(u.data,r):u}),this},refine(t,{message:n,type:a}={}){return a&&(n=n||O(a),s=a),m(this,"_parse",(r,u,f)=>{let l=r(u,f);y(f);if(!l.success)return l;let I=t(l.data);if(I===true||typeof I=="object"&&I?.success===true)return l;let g={message:typeof n=="function"?n(u):n};return {success:false,error:g,errors:[g]}}),this}};return S.length>0?S.reduce((t,n)=>n(t,e,p)??t,c):c}var S=[];function A(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");S.push(e);}export{$ as a,m as b,A as c};
@@ -1 +1 @@
1
- import {c}from'./chunk-V2T3CZFC.mjs';c((a,o,m)=>{if(m?.type==="array"){let r=a;r.min=function(e,{message:t}={}){return this.refine(n=>n.length>=e,{message:t||`Array must contain at least ${e} items.`})},r.max=function(e,{message:t}={}){return this.refine(n=>n.length<=e,{message:t||`Array must contain at most ${e} items.`})},r.length=function(e,{message:t}={}){return this.refine(n=>n.length===e,{message:t||`Array must contain exactly ${e} items.`})},r.nonEmpty=function({message:e}={}){return this.refine(t=>t.length>0,{message:e||"Array must not be empty."})},r.unique=function({message:e}={}){return this.refine(t=>{let n=new Set;try{return t.every(c=>{let s=JSON.stringify(c);return n.has(s)?!1:(n.add(s),!0)})}catch{return new Set(t).size===t.length}},{message:e||"Array items must be unique."})},r.sort=function(){return this.transform(e=>[...e].sort())},r.reverse=function(){return this.transform(e=>[...e].reverse())};}return a});
1
+ import {c}from'./chunk-2NCXW7ID.mjs';c((a,o,m)=>{if(m?.type==="array"){let r=a;r.min=function(e,{message:t}={}){return this.refine(n=>n.length>=e,{message:t||`Array must contain at least ${e} items.`})},r.max=function(e,{message:t}={}){return this.refine(n=>n.length<=e,{message:t||`Array must contain at most ${e} items.`})},r.length=function(e,{message:t}={}){return this.refine(n=>n.length===e,{message:t||`Array must contain exactly ${e} items.`})},r.nonEmpty=function({message:e}={}){return this.refine(t=>t.length>0,{message:e||"Array must not be empty."})},r.unique=function({message:e}={}){return this.refine(t=>{let n=new Set;try{return t.every(c=>{let s=JSON.stringify(c);return n.has(s)?!1:(n.add(s),!0)})}catch{return new Set(t).size===t.length}},{message:e||"Array items must be unique."})},r.sort=function(){return this.transform(e=>[...e].sort())},r.reverse=function(){return this.transform(e=>[...e].reverse())};}return a});
@@ -1 +1 @@
1
- import {c}from'./chunk-V2T3CZFC.mjs';c((i,s,c)=>{if(c?.type==="string"){let r=i;r.min=function(t,{message:n}={}){return this.refine(e=>e.length>=t,{message:n||(e=>`String must be at least ${t} characters long (received ${e.length} characters: "${e}")`)})},r.max=function(t,{message:n}={}){return this.refine(e=>e.length<=t,{message:n||(e=>`String must be at most ${t} characters long (received ${e.length} characters: "${e}")`)})},r.length=function(t,{message:n}={}){return this.refine(e=>e.length===t,{message:n||(e=>`String must be exactly ${t} characters long (received ${e.length} characters: "${e}")`)})},r.nonEmpty=function({message:t}={}){return this.refine(n=>n.length>0,{message:t||"String must not be empty (received empty string)"})},r.startsWith=function(t,{message:n}={}){return this.refine(e=>e.startsWith(t),{message:n||(e=>`String must start with "${t}" (received: "${e}")`)})},r.endsWith=function(t,{message:n}={}){return this.refine(e=>e.endsWith(t),{message:n||(e=>`String must end with "${t}" (received: "${e}")`)})},r.includes=function(t,{message:n}={}){return this.refine(e=>e.includes(t),{message:n||(e=>`String must include "${t}" (received: "${e}")`)})},r.toLowerCase=function(){return this.transform(t=>t.toLowerCase())},r.toUpperCase=function(){return this.transform(t=>t.toUpperCase())},r.trim=function(){return this.transform(t=>t.trim())},r.padStart=function(t,n=" "){return this.transform(e=>e.padStart(t,n))},r.padEnd=function(t,n=" "){return this.transform(e=>e.padEnd(t,n))},r.replace=function(t,n){return this.transform(e=>e.replace(t,n))};}return i});
1
+ import {c}from'./chunk-2NCXW7ID.mjs';c((i,s,c)=>{if(c?.type==="string"){let r=i;r.min=function(t,{message:n}={}){return this.refine(e=>e.length>=t,{message:n||(e=>`String must be at least ${t} characters long (received ${e.length} characters: "${e}")`)})},r.max=function(t,{message:n}={}){return this.refine(e=>e.length<=t,{message:n||(e=>`String must be at most ${t} characters long (received ${e.length} characters: "${e}")`)})},r.length=function(t,{message:n}={}){return this.refine(e=>e.length===t,{message:n||(e=>`String must be exactly ${t} characters long (received ${e.length} characters: "${e}")`)})},r.nonEmpty=function({message:t}={}){return this.refine(n=>n.length>0,{message:t||"String must not be empty (received empty string)"})},r.startsWith=function(t,{message:n}={}){return this.refine(e=>e.startsWith(t),{message:n||(e=>`String must start with "${t}" (received: "${e}")`)})},r.endsWith=function(t,{message:n}={}){return this.refine(e=>e.endsWith(t),{message:n||(e=>`String must end with "${t}" (received: "${e}")`)})},r.includes=function(t,{message:n}={}){return this.refine(e=>e.includes(t),{message:n||(e=>`String must include "${t}" (received: "${e}")`)})},r.toLowerCase=function(){return this.transform(t=>t.toLowerCase())},r.toUpperCase=function(){return this.transform(t=>t.toUpperCase())},r.trim=function(){return this.transform(t=>t.trim())},r.padStart=function(t,n=" "){return this.transform(e=>e.padStart(t,n))},r.padEnd=function(t,n=" "){return this.transform(e=>e.padEnd(t,n))},r.replace=function(t,n){return this.transform(e=>e.replace(t,n))};}return i});
@@ -1 +1 @@
1
- import {c}from'./chunk-V2T3CZFC.mjs';c((i,c,m)=>{if(m?.type==="number"){let n=i;n.min=function(e,{message:t}={}){return this.refine(r=>r>=e,{message:t||`Number must be greater than or equal to ${e}.`})},n.max=function(e,{message:t}={}){return this.refine(r=>r<=e,{message:t||`Number must be less than or equal to ${e}.`})},n.positive=function({message:e}={}){return this.refine(t=>t>0,{message:e||"Number must be positive."})},n.negative=function({message:e}={}){return this.refine(t=>t<0,{message:e||"Number must be negative."})},n.int=function({message:e}={}){return this.refine(t=>Number.isInteger(t),{message:e||"Number must be an integer."})},n.float=function({message:e}={}){return this.refine(t=>Number.isFinite(t)&&!Number.isInteger(t),{message:e||"Number must be a floating point (non-integer)."})},n.multipleOf=function(e,{message:t}={}){return this.refine(r=>r%e===0,{message:t||`Number must be a multiple of ${e}.`})},n.finite=function({message:e}={}){return this.refine(t=>Number.isFinite(t),{message:e||"Number must be finite."})};}return i});
1
+ import {c}from'./chunk-2NCXW7ID.mjs';c((i,c,m)=>{if(m?.type==="number"){let n=i;n.min=function(e,{message:t}={}){return this.refine(r=>r>=e,{message:t||`Number must be greater than or equal to ${e}.`})},n.max=function(e,{message:t}={}){return this.refine(r=>r<=e,{message:t||`Number must be less than or equal to ${e}.`})},n.positive=function({message:e}={}){return this.refine(t=>t>0,{message:e||"Number must be positive."})},n.negative=function({message:e}={}){return this.refine(t=>t<0,{message:e||"Number must be negative."})},n.int=function({message:e}={}){return this.refine(t=>Number.isInteger(t),{message:e||"Number must be an integer."})},n.float=function({message:e}={}){return this.refine(t=>Number.isFinite(t)&&!Number.isInteger(t),{message:e||"Number must be a floating point (non-integer)."})},n.multipleOf=function(e,{message:t}={}){return this.refine(r=>r%e===0,{message:t||`Number must be a multiple of ${e}.`})},n.finite=function({message:e}={}){return this.refine(t=>Number.isFinite(t),{message:e||"Number must be finite."})};}return i});
package/dist/full.cjs CHANGED
@@ -1 +1 @@
1
- 'use strict';var w={abortEarly:true};function y(n,u){if(!u)return n;let o=n?.cause?.key?`${u}.${n.cause.key}`:`${u}`;return {message:`Error parsing key "${o}": ${n.message}`,cause:{key:o}}}function O(n,u,o){if(n.errors?.length)for(let a=0;a<n.errors.length;a++)u.push(y(n.errors[a],o));}function g(n){return {...w,...n}}var k=n=>typeof n=="string"||n instanceof String,P=n=>(typeof n=="number"||n instanceof Number)&&!Number.isNaN(n),$=n=>n===true||n===false,N=n=>n instanceof Date&&!Number.isNaN(n.getTime()),E=n=>Array.isArray(n),T=n=>typeof n=="object"&&n!==null&&!Array.isArray(n),v={object(n,u){let o=h(T,{...u,type:"object"});return o._getDescription=()=>`object({ ${Object.entries(n).map(([t,r])=>`${t}: ${r._getDescription()}`).join(", ")} })`,S(o,"_parse",(a,t,r)=>{let e=a(t,r),{abortEarly:s}=g(r);if(e.success===false)return e;let c={},i=[];for(let m in n){let f=n[m]._parse(e.data[m],r);if(f.success)c[m]=f.data;else {if(f=f,s!==false){let p=y(f.error,m);return {success:false,error:p,errors:[p]}}O(f,i,m);}}return i.length>0?{success:false,error:i[0],errors:i}:{success:true,data:c}}),o},string(n){return h(k,{...n,type:"string"})},number(n){return h(P,{...n,type:"number"})},boolean(n){return h($,{...n,type:"boolean"})},date(n){return h(N,{...n,type:"date"})},enum(n,u){let o=e=>n.includes(e),a=e=>`Invalid ${t} value. Expected ${n.map(s=>`"${s}"`).join(" | ")}, received "${e}".`,t="enum",r=h(o,{message:a,...u,type:t});return r._getDescription=()=>`enum(${n.map(e=>`"${e}"`).join(" | ")})`,r},array(n,u){let o=h(E,{...u,type:"array"});return o._getDescription=()=>`array(${n._getDescription()})`,S(o,"_parse",(a,t,r)=>{let e=a(t,r),{abortEarly:s}=g(r);if(e.success===false)return e;let c=[],i=[];for(let m=0;m<e.data.length;m++){let f=n._parse(e.data[m],r);if(f.success)c.push(f.data);else {if(f=f,s!==false){let p=y(f.error,m);return {success:false,error:p,errors:[p]}}O(f,i,m);}}return i.length>0?{success:false,error:i[0],errors:i}:{success:true,data:c}}),o},any(){return h(()=>true)},preprocess(n,u){return S(u,"_parse",(o,a)=>(a=n(a),o(a))),u},union(n,u){return h(r=>{for(let e=0;e<n.length;e++){let s=n[e]._parse(r);if(s.success)return s}return false},{message:r=>`Invalid union value. Expected the value to match one of the schemas:${n.map((e,s)=>` ${s+1}. ${e._getDescription()}`).join(",")} but received "${typeof r}" with value: ${T(r)?JSON.stringify(r):`"${r}"`}`,...u,type:"union"})},literal(n,u){let t=h(r=>r===n,{message:r=>u?.message?typeof u.message=="function"?u.message(r):u.message:`Expected literal value "${n}", received "${r}"`,name:u?.name,type:"literal"});return t._getDescription=()=>`literal("${n}")`,t}};function x(n){return u=>`The value "${u}" must be type of ${n} but is type of "${typeof u}".`}function S(n,u,o){let a=n[u];n[u]=(...t)=>o(a,...t);}function h(n,{type:u="any",name:o,message:a}={}){a=a||x(u);let t={name:o,message:a,type:u},r={_getName(){return o},_getType(){return u},_getDescription(){return this._getName()??this._getType()},_parse(e,s){let c=n(e);if(c===true)return {success:true,data:e};if(typeof c=="object"&&c?.success===true)return c;let i={message:typeof a=="function"?a(e):a};return {success:false,error:i,errors:[i]}},parse(e,s){let c=r._parse(e,s);if(!c.success)throw c=c,new Error(c.error.message,{cause:c.error.cause});return c.data},safeParse(e,s){return r._parse(e,s)},transform(e){return S(this,"_parse",(s,c,i)=>{let m=s(c,i);return m.success&&(m.data=e(m.data)),m}),this},optional(){return S(this,"_parse",(e,s,c)=>{let i=e(s,c);return !i.success&&s===void 0&&(i.data=void 0,i.success=true),i}),this},nullable(){return S(this,"_parse",(e,s,c)=>{let i=e(s,c);return !i.success&&s===null&&(i.data=null,i.success=true),i}),this},nullish(){return S(this,"_parse",(e,s,c)=>{let i=e(s,c);return !i.success&&s==null&&(i.success=true,i.data=s),i}),this},default(e){return S(this,"_parse",(s,c,i)=>(c===void 0&&(c=typeof e=="function"?e():e),s(c,i))),this},pipe(e){return S(this,"_parse",(s,c,i)=>{let m=s(c,i);return m.success?e._parse(m.data,i):m}),this},refine(e,{message:s,type:c}={}){return c&&(s=s||x(c),u=c),S(this,"_parse",(i,m,f)=>{let p=i(m,f);g(f);if(!p.success)return p;let l=e(p.data);if(l===true||typeof l=="object"&&l?.success===true)return p;let b={message:typeof s=="function"?s(m):s};return {success:false,error:b,errors:[b]}}),this}};return d.length>0?d.reduce((e,s)=>s(e,n,t)??e,r):r}var d=[];function I(n){if(typeof n!="function")throw new TypeError("extend() requires a function argument");d.push(n);}I((n,u,o)=>{if(o?.type==="string"){let a=n;a.min=function(t,{message:r}={}){return this.refine(e=>e.length>=t,{message:r||(e=>`String must be at least ${t} characters long (received ${e.length} characters: "${e}")`)})},a.max=function(t,{message:r}={}){return this.refine(e=>e.length<=t,{message:r||(e=>`String must be at most ${t} characters long (received ${e.length} characters: "${e}")`)})},a.length=function(t,{message:r}={}){return this.refine(e=>e.length===t,{message:r||(e=>`String must be exactly ${t} characters long (received ${e.length} characters: "${e}")`)})},a.nonEmpty=function({message:t}={}){return this.refine(r=>r.length>0,{message:t||"String must not be empty (received empty string)"})},a.startsWith=function(t,{message:r}={}){return this.refine(e=>e.startsWith(t),{message:r||(e=>`String must start with "${t}" (received: "${e}")`)})},a.endsWith=function(t,{message:r}={}){return this.refine(e=>e.endsWith(t),{message:r||(e=>`String must end with "${t}" (received: "${e}")`)})},a.includes=function(t,{message:r}={}){return this.refine(e=>e.includes(t),{message:r||(e=>`String must include "${t}" (received: "${e}")`)})},a.toLowerCase=function(){return this.transform(t=>t.toLowerCase())},a.toUpperCase=function(){return this.transform(t=>t.toUpperCase())},a.trim=function(){return this.transform(t=>t.trim())},a.padStart=function(t,r=" "){return this.transform(e=>e.padStart(t,r))},a.padEnd=function(t,r=" "){return this.transform(e=>e.padEnd(t,r))},a.replace=function(t,r){return this.transform(e=>e.replace(t,r))};}return n});I((n,u,o)=>{if(o?.type==="number"){let a=n;a.min=function(t,{message:r}={}){return this.refine(e=>e>=t,{message:r||`Number must be greater than or equal to ${t}.`})},a.max=function(t,{message:r}={}){return this.refine(e=>e<=t,{message:r||`Number must be less than or equal to ${t}.`})},a.positive=function({message:t}={}){return this.refine(r=>r>0,{message:t||"Number must be positive."})},a.negative=function({message:t}={}){return this.refine(r=>r<0,{message:t||"Number must be negative."})},a.int=function({message:t}={}){return this.refine(r=>Number.isInteger(r),{message:t||"Number must be an integer."})},a.float=function({message:t}={}){return this.refine(r=>Number.isFinite(r)&&!Number.isInteger(r),{message:t||"Number must be a floating point (non-integer)."})},a.multipleOf=function(t,{message:r}={}){return this.refine(e=>e%t===0,{message:r||`Number must be a multiple of ${t}.`})},a.finite=function({message:t}={}){return this.refine(r=>Number.isFinite(r),{message:t||"Number must be finite."})};}return n});I((n,u,o)=>{if(o?.type==="array"){let a=n;a.min=function(t,{message:r}={}){return this.refine(e=>e.length>=t,{message:r||`Array must contain at least ${t} items.`})},a.max=function(t,{message:r}={}){return this.refine(e=>e.length<=t,{message:r||`Array must contain at most ${t} items.`})},a.length=function(t,{message:r}={}){return this.refine(e=>e.length===t,{message:r||`Array must contain exactly ${t} items.`})},a.nonEmpty=function({message:t}={}){return this.refine(r=>r.length>0,{message:t||"Array must not be empty."})},a.unique=function({message:t}={}){return this.refine(r=>{let e=new Set;try{return r.every(s=>{let c=JSON.stringify(s);return e.has(c)?!1:(e.add(c),!0)})}catch{return new Set(r).size===r.length}},{message:t||"Array items must be unique."})},a.sort=function(){return this.transform(t=>[...t].sort())},a.reverse=function(){return this.transform(t=>[...t].reverse())};}return n});exports.extend=I;exports.hookOriginal=S;exports.s=v;
1
+ 'use strict';var w={abortEarly:true};function y(n,u){if(!u)return n;let o=n?.cause?.key?`${u}.${n.cause.key}`:`${u}`;return {message:`Error parsing key "${o}": ${n.message}`,cause:{key:o}}}function O(n,u,o){if(n.errors?.length)for(let a=0;a<n.errors.length;a++)u.push(y(n.errors[a],o));}function d(n){return {...w,...n}}var k=n=>typeof n=="string"||n instanceof String,P=n=>(typeof n=="number"||n instanceof Number)&&!Number.isNaN(n),$=n=>n===true||n===false,N=n=>n instanceof Date&&!Number.isNaN(n.getTime()),E=n=>Array.isArray(n),T=n=>typeof n=="object"&&n!==null&&!Array.isArray(n),v={object(n,u){let o=S(T,{...u,type:"object"});return o._getDescription=()=>`object({ ${Object.entries(n).map(([t,r])=>`${t}: ${r._getDescription()}`).join(", ")} })`,p(o,"_parse",(a,t,r)=>{let e=a(t,r),{abortEarly:s}=d(r);if(e.success===false)return e;let c={},i=[];for(let m in n){let f=n[m]._parse(e.data[m],r);if(f.success)c[m]=f.data;else {if(f=f,s!==false){let h=y(f.error,m);return {success:false,error:h,errors:[h]}}O(f,i,m);}}return i.length>0?{success:false,error:i[0],errors:i}:{success:true,data:c}}),o},string(n){return S(k,{...n,type:"string"})},number(n){return S(P,{...n,type:"number"})},boolean(n){return S($,{...n,type:"boolean"})},date(n){return S(N,{...n,type:"date"})},enum(n,u){let o=e=>n.includes(e),a=e=>`Invalid ${t} value. Expected ${n.map(s=>`"${s}"`).join(" | ")}, received "${e}".`,t="enum",r=S(o,{message:a,...u,type:t});return r._getDescription=()=>`enum(${n.map(e=>`"${e}"`).join(" | ")})`,r},array(n,u){let o=S(E,{...u,type:"array"});return o._getDescription=()=>`array(${n._getDescription()})`,p(o,"_parse",(a,t,r)=>{let e=a(t,r),{abortEarly:s}=d(r);if(e.success===false)return e;let c=[],i=[];for(let m=0;m<e.data.length;m++){let f=n._parse(e.data[m],r);if(f.success)c.push(f.data);else {if(f=f,s!==false){let h=y(f.error,m);return {success:false,error:h,errors:[h]}}O(f,i,m);}}return i.length>0?{success:false,error:i[0],errors:i}:{success:true,data:c}}),o},any(){return S(()=>true)},preprocess(n,u){return p(u,"_parse",(o,a)=>(a=n(a),o(a))),u},union(n,u){return S(r=>{for(let e=0;e<n.length;e++){let s=n[e]._parse(r);if(s.success)return s}return false},{message:r=>`Invalid union value. Expected the value to match one of the schemas:${n.map((e,s)=>` ${s+1}. ${e._getDescription()}`).join(",")} but received "${typeof r}" with value: ${T(r)?JSON.stringify(r):`"${r}"`}`,...u,type:"union"})},literal(n,u){let t=S(r=>r===n,{message:r=>u?.message?typeof u.message=="function"?u.message(r):u.message:`Expected literal value "${n}", received "${r}"`,name:u?.name,type:"literal"});return t._getDescription=()=>`literal("${n}")`,t}};function x(n){return u=>`The value "${u}" must be type of ${n} but is type of "${typeof u}".`}function p(n,u,o){let a=n[u];n[u]=(...t)=>o(a,...t);}function S(n,{type:u="any",name:o,message:a}={}){a=a||x(u);let t={name:o,message:a,type:u},r={_getName(){return o},_getType(){return u},_getDescription(){return this._getName()??this._getType()},_parse(e,s){let c=n(e);if(c===true)return {success:true,data:e};if(typeof c=="object"&&c?.success===true)return c;let i={message:typeof a=="function"?a(e):a};return {success:false,error:i,errors:[i]}},parse(e,s){let c=r._parse(e,s);if(!c.success)throw c=c,new Error(c.error.message,{cause:c.error.cause});return c.data},safeParse(e,s){return r._parse(e,s)},transform(e){return p(this,"_parse",(s,c,i)=>{let m=s(c,i);return m.success&&(m.data=e(m.data)),m}),this},optional(){return p(this,"_parse",(e,s,c)=>{let i=e(s,c);return !i.success&&s===void 0&&(i.data=void 0,i.success=true),i}),this},nullable(){return p(this,"_parse",(e,s,c)=>{let i=e(s,c);return !i.success&&s===null&&(i.data=null,i.success=true),i}),this},nullish(){return p(this,"_parse",(e,s,c)=>{let i=e(s,c);return !i.success&&s==null&&(i.success=true,i.data=s),i}),this},default(e){return p(this,"_parse",(s,c,i)=>(c===void 0&&(c=typeof e=="function"?e():e),s(c,i))),this},catch(e){return p(this,"_parse",(s,c,i)=>{let m=s(c,i);return m.success?m:{success:true,data:typeof e=="function"?e({input:c,error:m.error}):e}}),this},pipe(e){return p(this,"_parse",(s,c,i)=>{let m=s(c,i);return m.success?e._parse(m.data,i):m}),this},refine(e,{message:s,type:c}={}){return c&&(s=s||x(c),u=c),p(this,"_parse",(i,m,f)=>{let h=i(m,f);d(f);if(!h.success)return h;let l=e(h.data);if(l===true||typeof l=="object"&&l?.success===true)return h;let b={message:typeof s=="function"?s(m):s};return {success:false,error:b,errors:[b]}}),this}};return g.length>0?g.reduce((e,s)=>s(e,n,t)??e,r):r}var g=[];function I(n){if(typeof n!="function")throw new TypeError("extend() requires a function argument");g.push(n);}I((n,u,o)=>{if(o?.type==="string"){let a=n;a.min=function(t,{message:r}={}){return this.refine(e=>e.length>=t,{message:r||(e=>`String must be at least ${t} characters long (received ${e.length} characters: "${e}")`)})},a.max=function(t,{message:r}={}){return this.refine(e=>e.length<=t,{message:r||(e=>`String must be at most ${t} characters long (received ${e.length} characters: "${e}")`)})},a.length=function(t,{message:r}={}){return this.refine(e=>e.length===t,{message:r||(e=>`String must be exactly ${t} characters long (received ${e.length} characters: "${e}")`)})},a.nonEmpty=function({message:t}={}){return this.refine(r=>r.length>0,{message:t||"String must not be empty (received empty string)"})},a.startsWith=function(t,{message:r}={}){return this.refine(e=>e.startsWith(t),{message:r||(e=>`String must start with "${t}" (received: "${e}")`)})},a.endsWith=function(t,{message:r}={}){return this.refine(e=>e.endsWith(t),{message:r||(e=>`String must end with "${t}" (received: "${e}")`)})},a.includes=function(t,{message:r}={}){return this.refine(e=>e.includes(t),{message:r||(e=>`String must include "${t}" (received: "${e}")`)})},a.toLowerCase=function(){return this.transform(t=>t.toLowerCase())},a.toUpperCase=function(){return this.transform(t=>t.toUpperCase())},a.trim=function(){return this.transform(t=>t.trim())},a.padStart=function(t,r=" "){return this.transform(e=>e.padStart(t,r))},a.padEnd=function(t,r=" "){return this.transform(e=>e.padEnd(t,r))},a.replace=function(t,r){return this.transform(e=>e.replace(t,r))};}return n});I((n,u,o)=>{if(o?.type==="number"){let a=n;a.min=function(t,{message:r}={}){return this.refine(e=>e>=t,{message:r||`Number must be greater than or equal to ${t}.`})},a.max=function(t,{message:r}={}){return this.refine(e=>e<=t,{message:r||`Number must be less than or equal to ${t}.`})},a.positive=function({message:t}={}){return this.refine(r=>r>0,{message:t||"Number must be positive."})},a.negative=function({message:t}={}){return this.refine(r=>r<0,{message:t||"Number must be negative."})},a.int=function({message:t}={}){return this.refine(r=>Number.isInteger(r),{message:t||"Number must be an integer."})},a.float=function({message:t}={}){return this.refine(r=>Number.isFinite(r)&&!Number.isInteger(r),{message:t||"Number must be a floating point (non-integer)."})},a.multipleOf=function(t,{message:r}={}){return this.refine(e=>e%t===0,{message:r||`Number must be a multiple of ${t}.`})},a.finite=function({message:t}={}){return this.refine(r=>Number.isFinite(r),{message:t||"Number must be finite."})};}return n});I((n,u,o)=>{if(o?.type==="array"){let a=n;a.min=function(t,{message:r}={}){return this.refine(e=>e.length>=t,{message:r||`Array must contain at least ${t} items.`})},a.max=function(t,{message:r}={}){return this.refine(e=>e.length<=t,{message:r||`Array must contain at most ${t} items.`})},a.length=function(t,{message:r}={}){return this.refine(e=>e.length===t,{message:r||`Array must contain exactly ${t} items.`})},a.nonEmpty=function({message:t}={}){return this.refine(r=>r.length>0,{message:t||"Array must not be empty."})},a.unique=function({message:t}={}){return this.refine(r=>{let e=new Set;try{return r.every(s=>{let c=JSON.stringify(s);return e.has(c)?!1:(e.add(c),!0)})}catch{return new Set(r).size===r.length}},{message:t||"Array items must be unique."})},a.sort=function(){return this.transform(t=>[...t].sort())},a.reverse=function(){return this.transform(t=>[...t].reverse())};}return n});exports.extend=I;exports.hookOriginal=p;exports.s=v;
package/dist/full.mjs CHANGED
@@ -1 +1 @@
1
- import'./chunk-ELVLLQRH.mjs';import'./chunk-W2H6TLSH.mjs';import'./chunk-FEEE6IG6.mjs';export{c as extend,b as hookOriginal,a as s}from'./chunk-V2T3CZFC.mjs';
1
+ import'./chunk-4JJPVRF6.mjs';import'./chunk-PH4B25KI.mjs';import'./chunk-LZRUIXQL.mjs';export{c as extend,b as hookOriginal,a as s}from'./chunk-2NCXW7ID.mjs';
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- 'use strict';var k={abortEarly:true};function d(e,s){if(!s)return e;let i=e?.cause?.key?`${s}.${e.cause.key}`:`${s}`;return {message:`Error parsing key "${i}": ${e.message}`,cause:{key:i}}}function T(e,s,i){if(e.errors?.length)for(let u=0;u<e.errors.length;u++)s.push(d(e.errors[u],i));}function y(e){return {...k,...e}}var x=e=>typeof e=="string"||e instanceof String,w=e=>(typeof e=="number"||e instanceof Number)&&!Number.isNaN(e),P=e=>e===true||e===false,E=e=>e instanceof Date&&!Number.isNaN(e.getTime()),_=e=>Array.isArray(e),b=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),$={object(e,s){let i=l(b,{...s,type:"object"});return i._getDescription=()=>`object({ ${Object.entries(e).map(([p,c])=>`${p}: ${c._getDescription()}`).join(", ")} })`,h(i,"_parse",(u,p,c)=>{let t=u(p,c),{abortEarly:n}=y(c);if(t.success===false)return t;let a={},r=[];for(let o in e){let f=e[o]._parse(t.data[o],c);if(f.success)a[o]=f.data;else {if(f=f,n!==false){let m=d(f.error,o);return {success:false,error:m,errors:[m]}}T(f,r,o);}}return r.length>0?{success:false,error:r[0],errors:r}:{success:true,data:a}}),i},string(e){return l(x,{...e,type:"string"})},number(e){return l(w,{...e,type:"number"})},boolean(e){return l(P,{...e,type:"boolean"})},date(e){return l(E,{...e,type:"date"})},enum(e,s){let i=t=>e.includes(t),u=t=>`Invalid ${p} value. Expected ${e.map(n=>`"${n}"`).join(" | ")}, received "${t}".`,p="enum",c=l(i,{message:u,...s,type:p});return c._getDescription=()=>`enum(${e.map(t=>`"${t}"`).join(" | ")})`,c},array(e,s){let i=l(_,{...s,type:"array"});return i._getDescription=()=>`array(${e._getDescription()})`,h(i,"_parse",(u,p,c)=>{let t=u(p,c),{abortEarly:n}=y(c);if(t.success===false)return t;let a=[],r=[];for(let o=0;o<t.data.length;o++){let f=e._parse(t.data[o],c);if(f.success)a.push(f.data);else {if(f=f,n!==false){let m=d(f.error,o);return {success:false,error:m,errors:[m]}}T(f,r,o);}}return r.length>0?{success:false,error:r[0],errors:r}:{success:true,data:a}}),i},any(){return l(()=>true)},preprocess(e,s){return h(s,"_parse",(i,u)=>(u=e(u),i(u))),s},union(e,s){return l(c=>{for(let t=0;t<e.length;t++){let n=e[t]._parse(c);if(n.success)return n}return false},{message:c=>`Invalid union value. Expected the value to match one of the schemas:${e.map((t,n)=>` ${n+1}. ${t._getDescription()}`).join(",")} but received "${typeof c}" with value: ${b(c)?JSON.stringify(c):`"${c}"`}`,...s,type:"union"})},literal(e,s){let p=l(c=>c===e,{message:c=>s?.message?typeof s.message=="function"?s.message(c):s.message:`Expected literal value "${e}", received "${c}"`,name:s?.name,type:"literal"});return p._getDescription=()=>`literal("${e}")`,p}};function O(e){return s=>`The value "${s}" must be type of ${e} but is type of "${typeof s}".`}function h(e,s,i){let u=e[s];e[s]=(...p)=>i(u,...p);}function l(e,{type:s="any",name:i,message:u}={}){u=u||O(s);let p={name:i,message:u,type:s},c={_getName(){return i},_getType(){return s},_getDescription(){return this._getName()??this._getType()},_parse(t,n){let a=e(t);if(a===true)return {success:true,data:t};if(typeof a=="object"&&a?.success===true)return a;let r={message:typeof u=="function"?u(t):u};return {success:false,error:r,errors:[r]}},parse(t,n){let a=c._parse(t,n);if(!a.success)throw a=a,new Error(a.error.message,{cause:a.error.cause});return a.data},safeParse(t,n){return c._parse(t,n)},transform(t){return h(this,"_parse",(n,a,r)=>{let o=n(a,r);return o.success&&(o.data=t(o.data)),o}),this},optional(){return h(this,"_parse",(t,n,a)=>{let r=t(n,a);return !r.success&&n===void 0&&(r.data=void 0,r.success=true),r}),this},nullable(){return h(this,"_parse",(t,n,a)=>{let r=t(n,a);return !r.success&&n===null&&(r.data=null,r.success=true),r}),this},nullish(){return h(this,"_parse",(t,n,a)=>{let r=t(n,a);return !r.success&&n==null&&(r.success=true,r.data=n),r}),this},default(t){return h(this,"_parse",(n,a,r)=>(a===void 0&&(a=typeof t=="function"?t():t),n(a,r))),this},pipe(t){return h(this,"_parse",(n,a,r)=>{let o=n(a,r);return o.success?t._parse(o.data,r):o}),this},refine(t,{message:n,type:a}={}){return a&&(n=n||O(a),s=a),h(this,"_parse",(r,o,f)=>{let m=r(o,f);y(f);if(!m.success)return m;let I=t(m.data);if(I===true||typeof I=="object"&&I?.success===true)return m;let g={message:typeof n=="function"?n(o):n};return {success:false,error:g,errors:[g]}}),this}};return S.length>0?S.reduce((t,n)=>n(t,e,p)??t,c):c}var S=[];function A(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");S.push(e);}exports.extend=A;exports.hookOriginal=h;exports.s=$;
1
+ 'use strict';var k={abortEarly:true};function d(e,s){if(!s)return e;let i=e?.cause?.key?`${s}.${e.cause.key}`:`${s}`;return {message:`Error parsing key "${i}": ${e.message}`,cause:{key:i}}}function T(e,s,i){if(e.errors?.length)for(let o=0;o<e.errors.length;o++)s.push(d(e.errors[o],i));}function y(e){return {...k,...e}}var x=e=>typeof e=="string"||e instanceof String,w=e=>(typeof e=="number"||e instanceof Number)&&!Number.isNaN(e),P=e=>e===true||e===false,E=e=>e instanceof Date&&!Number.isNaN(e.getTime()),_=e=>Array.isArray(e),b=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),$={object(e,s){let i=h(b,{...s,type:"object"});return i._getDescription=()=>`object({ ${Object.entries(e).map(([p,c])=>`${p}: ${c._getDescription()}`).join(", ")} })`,m(i,"_parse",(o,p,c)=>{let t=o(p,c),{abortEarly:n}=y(c);if(t.success===false)return t;let a={},r=[];for(let u in e){let f=e[u]._parse(t.data[u],c);if(f.success)a[u]=f.data;else {if(f=f,n!==false){let l=d(f.error,u);return {success:false,error:l,errors:[l]}}T(f,r,u);}}return r.length>0?{success:false,error:r[0],errors:r}:{success:true,data:a}}),i},string(e){return h(x,{...e,type:"string"})},number(e){return h(w,{...e,type:"number"})},boolean(e){return h(P,{...e,type:"boolean"})},date(e){return h(E,{...e,type:"date"})},enum(e,s){let i=t=>e.includes(t),o=t=>`Invalid ${p} value. Expected ${e.map(n=>`"${n}"`).join(" | ")}, received "${t}".`,p="enum",c=h(i,{message:o,...s,type:p});return c._getDescription=()=>`enum(${e.map(t=>`"${t}"`).join(" | ")})`,c},array(e,s){let i=h(_,{...s,type:"array"});return i._getDescription=()=>`array(${e._getDescription()})`,m(i,"_parse",(o,p,c)=>{let t=o(p,c),{abortEarly:n}=y(c);if(t.success===false)return t;let a=[],r=[];for(let u=0;u<t.data.length;u++){let f=e._parse(t.data[u],c);if(f.success)a.push(f.data);else {if(f=f,n!==false){let l=d(f.error,u);return {success:false,error:l,errors:[l]}}T(f,r,u);}}return r.length>0?{success:false,error:r[0],errors:r}:{success:true,data:a}}),i},any(){return h(()=>true)},preprocess(e,s){return m(s,"_parse",(i,o)=>(o=e(o),i(o))),s},union(e,s){return h(c=>{for(let t=0;t<e.length;t++){let n=e[t]._parse(c);if(n.success)return n}return false},{message:c=>`Invalid union value. Expected the value to match one of the schemas:${e.map((t,n)=>` ${n+1}. ${t._getDescription()}`).join(",")} but received "${typeof c}" with value: ${b(c)?JSON.stringify(c):`"${c}"`}`,...s,type:"union"})},literal(e,s){let p=h(c=>c===e,{message:c=>s?.message?typeof s.message=="function"?s.message(c):s.message:`Expected literal value "${e}", received "${c}"`,name:s?.name,type:"literal"});return p._getDescription=()=>`literal("${e}")`,p}};function O(e){return s=>`The value "${s}" must be type of ${e} but is type of "${typeof s}".`}function m(e,s,i){let o=e[s];e[s]=(...p)=>i(o,...p);}function h(e,{type:s="any",name:i,message:o}={}){o=o||O(s);let p={name:i,message:o,type:s},c={_getName(){return i},_getType(){return s},_getDescription(){return this._getName()??this._getType()},_parse(t,n){let a=e(t);if(a===true)return {success:true,data:t};if(typeof a=="object"&&a?.success===true)return a;let r={message:typeof o=="function"?o(t):o};return {success:false,error:r,errors:[r]}},parse(t,n){let a=c._parse(t,n);if(!a.success)throw a=a,new Error(a.error.message,{cause:a.error.cause});return a.data},safeParse(t,n){return c._parse(t,n)},transform(t){return m(this,"_parse",(n,a,r)=>{let u=n(a,r);return u.success&&(u.data=t(u.data)),u}),this},optional(){return m(this,"_parse",(t,n,a)=>{let r=t(n,a);return !r.success&&n===void 0&&(r.data=void 0,r.success=true),r}),this},nullable(){return m(this,"_parse",(t,n,a)=>{let r=t(n,a);return !r.success&&n===null&&(r.data=null,r.success=true),r}),this},nullish(){return m(this,"_parse",(t,n,a)=>{let r=t(n,a);return !r.success&&n==null&&(r.success=true,r.data=n),r}),this},default(t){return m(this,"_parse",(n,a,r)=>(a===void 0&&(a=typeof t=="function"?t():t),n(a,r))),this},catch(t){return m(this,"_parse",(n,a,r)=>{let u=n(a,r);return u.success?u:{success:true,data:typeof t=="function"?t({input:a,error:u.error}):t}}),this},pipe(t){return m(this,"_parse",(n,a,r)=>{let u=n(a,r);return u.success?t._parse(u.data,r):u}),this},refine(t,{message:n,type:a}={}){return a&&(n=n||O(a),s=a),m(this,"_parse",(r,u,f)=>{let l=r(u,f);y(f);if(!l.success)return l;let I=t(l.data);if(I===true||typeof I=="object"&&I?.success===true)return l;let g={message:typeof n=="function"?n(u):n};return {success:false,error:g,errors:[g]}}),this}};return S.length>0?S.reduce((t,n)=>n(t,e,p)??t,c):c}var S=[];function A(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");S.push(e);}exports.extend=A;exports.hookOriginal=m;exports.s=$;
package/dist/index.d.mts CHANGED
@@ -30,6 +30,10 @@ interface SchemaInterface<Input, Output> {
30
30
  nullable(): SchemaInterface<Input, Output | null>;
31
31
  nullish(): SchemaInterface<Input, Output | undefined | null>;
32
32
  default(defaultValue: Partial<Input> | (() => Partial<Input>) | Partial<Output>): SchemaInterface<Input, Output>;
33
+ catch(catchValue: Output | ((ctx: {
34
+ input: unknown;
35
+ error: ErrorStructure;
36
+ }) => Output)): SchemaInterface<Input, Output>;
33
37
  pipe<NewOutput>(schema: SchemaInterface<Output, NewOutput>): SchemaInterface<Output, NewOutput>;
34
38
  refine(validation: ValidationMethod<Input, Output>, options?: CreateSchemaInterfaceOptions): SchemaInterface<Input, Output>;
35
39
  }
package/dist/index.d.ts CHANGED
@@ -30,6 +30,10 @@ interface SchemaInterface<Input, Output> {
30
30
  nullable(): SchemaInterface<Input, Output | null>;
31
31
  nullish(): SchemaInterface<Input, Output | undefined | null>;
32
32
  default(defaultValue: Partial<Input> | (() => Partial<Input>) | Partial<Output>): SchemaInterface<Input, Output>;
33
+ catch(catchValue: Output | ((ctx: {
34
+ input: unknown;
35
+ error: ErrorStructure;
36
+ }) => Output)): SchemaInterface<Input, Output>;
33
37
  pipe<NewOutput>(schema: SchemaInterface<Output, NewOutput>): SchemaInterface<Output, NewOutput>;
34
38
  refine(validation: ValidationMethod<Input, Output>, options?: CreateSchemaInterfaceOptions): SchemaInterface<Input, Output>;
35
39
  }
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- export{c as extend,b as hookOriginal,a as s}from'./chunk-V2T3CZFC.mjs';
1
+ export{c as extend,b as hookOriginal,a as s}from'./chunk-2NCXW7ID.mjs';
package/dist/number.cjs CHANGED
@@ -1 +1 @@
1
- 'use strict';var k={abortEarly:true};function S(e,u){if(!u)return e;let i=e?.cause?.key?`${u}.${e.cause.key}`:`${u}`;return {message:`Error parsing key "${i}": ${e.message}`,cause:{key:i}}}function g(e,u,i){if(e.errors?.length)for(let o=0;o<e.errors.length;o++)u.push(S(e.errors[o],i));}function d(e){return {...k,...e}}var w=e=>typeof e=="string"||e instanceof String,P=e=>(typeof e=="number"||e instanceof Number)&&!Number.isNaN(e),N=e=>e===true||e===false,E=e=>e instanceof Date&&!Number.isNaN(e.getTime()),_=e=>Array.isArray(e),O=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),R={object(e,u){let i=h(O,{...u,type:"object"});return i._getDescription=()=>`object({ ${Object.entries(e).map(([s,n])=>`${s}: ${n._getDescription()}`).join(", ")} })`,l(i,"_parse",(o,s,n)=>{let t=o(s,n),{abortEarly:r}=d(n);if(t.success===false)return t;let c={},a=[];for(let p in e){let f=e[p]._parse(t.data[p],n);if(f.success)c[p]=f.data;else {if(f=f,r!==false){let m=S(f.error,p);return {success:false,error:m,errors:[m]}}g(f,a,p);}}return a.length>0?{success:false,error:a[0],errors:a}:{success:true,data:c}}),i},string(e){return h(w,{...e,type:"string"})},number(e){return h(P,{...e,type:"number"})},boolean(e){return h(N,{...e,type:"boolean"})},date(e){return h(E,{...e,type:"date"})},enum(e,u){let i=t=>e.includes(t),o=t=>`Invalid ${s} value. Expected ${e.map(r=>`"${r}"`).join(" | ")}, received "${t}".`,s="enum",n=h(i,{message:o,...u,type:s});return n._getDescription=()=>`enum(${e.map(t=>`"${t}"`).join(" | ")})`,n},array(e,u){let i=h(_,{...u,type:"array"});return i._getDescription=()=>`array(${e._getDescription()})`,l(i,"_parse",(o,s,n)=>{let t=o(s,n),{abortEarly:r}=d(n);if(t.success===false)return t;let c=[],a=[];for(let p=0;p<t.data.length;p++){let f=e._parse(t.data[p],n);if(f.success)c.push(f.data);else {if(f=f,r!==false){let m=S(f.error,p);return {success:false,error:m,errors:[m]}}g(f,a,p);}}return a.length>0?{success:false,error:a[0],errors:a}:{success:true,data:c}}),i},any(){return h(()=>true)},preprocess(e,u){return l(u,"_parse",(i,o)=>(o=e(o),i(o))),u},union(e,u){return h(n=>{for(let t=0;t<e.length;t++){let r=e[t]._parse(n);if(r.success)return r}return false},{message:n=>`Invalid union value. Expected the value to match one of the schemas:${e.map((t,r)=>` ${r+1}. ${t._getDescription()}`).join(",")} but received "${typeof n}" with value: ${O(n)?JSON.stringify(n):`"${n}"`}`,...u,type:"union"})},literal(e,u){let s=h(n=>n===e,{message:n=>u?.message?typeof u.message=="function"?u.message(n):u.message:`Expected literal value "${e}", received "${n}"`,name:u?.name,type:"literal"});return s._getDescription=()=>`literal("${e}")`,s}};function T(e){return u=>`The value "${u}" must be type of ${e} but is type of "${typeof u}".`}function l(e,u,i){let o=e[u];e[u]=(...s)=>i(o,...s);}function h(e,{type:u="any",name:i,message:o}={}){o=o||T(u);let s={name:i,message:o,type:u},n={_getName(){return i},_getType(){return u},_getDescription(){return this._getName()??this._getType()},_parse(t,r){let c=e(t);if(c===true)return {success:true,data:t};if(typeof c=="object"&&c?.success===true)return c;let a={message:typeof o=="function"?o(t):o};return {success:false,error:a,errors:[a]}},parse(t,r){let c=n._parse(t,r);if(!c.success)throw c=c,new Error(c.error.message,{cause:c.error.cause});return c.data},safeParse(t,r){return n._parse(t,r)},transform(t){return l(this,"_parse",(r,c,a)=>{let p=r(c,a);return p.success&&(p.data=t(p.data)),p}),this},optional(){return l(this,"_parse",(t,r,c)=>{let a=t(r,c);return !a.success&&r===void 0&&(a.data=void 0,a.success=true),a}),this},nullable(){return l(this,"_parse",(t,r,c)=>{let a=t(r,c);return !a.success&&r===null&&(a.data=null,a.success=true),a}),this},nullish(){return l(this,"_parse",(t,r,c)=>{let a=t(r,c);return !a.success&&r==null&&(a.success=true,a.data=r),a}),this},default(t){return l(this,"_parse",(r,c,a)=>(c===void 0&&(c=typeof t=="function"?t():t),r(c,a))),this},pipe(t){return l(this,"_parse",(r,c,a)=>{let p=r(c,a);return p.success?t._parse(p.data,a):p}),this},refine(t,{message:r,type:c}={}){return c&&(r=r||T(c),u=c),l(this,"_parse",(a,p,f)=>{let m=a(p,f);d(f);if(!m.success)return m;let I=t(m.data);if(I===true||typeof I=="object"&&I?.success===true)return m;let b={message:typeof r=="function"?r(p):r};return {success:false,error:b,errors:[b]}}),this}};return y.length>0?y.reduce((t,r)=>r(t,e,s)??t,n):n}var y=[];function x(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");y.push(e);}x((e,u,i)=>{if(i?.type==="number"){let o=e;o.min=function(s,{message:n}={}){return this.refine(t=>t>=s,{message:n||`Number must be greater than or equal to ${s}.`})},o.max=function(s,{message:n}={}){return this.refine(t=>t<=s,{message:n||`Number must be less than or equal to ${s}.`})},o.positive=function({message:s}={}){return this.refine(n=>n>0,{message:s||"Number must be positive."})},o.negative=function({message:s}={}){return this.refine(n=>n<0,{message:s||"Number must be negative."})},o.int=function({message:s}={}){return this.refine(n=>Number.isInteger(n),{message:s||"Number must be an integer."})},o.float=function({message:s}={}){return this.refine(n=>Number.isFinite(n)&&!Number.isInteger(n),{message:s||"Number must be a floating point (non-integer)."})},o.multipleOf=function(s,{message:n}={}){return this.refine(t=>t%s===0,{message:n||`Number must be a multiple of ${s}.`})},o.finite=function({message:s}={}){return this.refine(n=>Number.isFinite(n),{message:s||"Number must be finite."})};}return e});exports.extend=x;exports.hookOriginal=l;exports.s=R;
1
+ 'use strict';var k={abortEarly:true};function S(e,u){if(!u)return e;let p=e?.cause?.key?`${u}.${e.cause.key}`:`${u}`;return {message:`Error parsing key "${p}": ${e.message}`,cause:{key:p}}}function g(e,u,p){if(e.errors?.length)for(let o=0;o<e.errors.length;o++)u.push(S(e.errors[o],p));}function d(e){return {...k,...e}}var w=e=>typeof e=="string"||e instanceof String,P=e=>(typeof e=="number"||e instanceof Number)&&!Number.isNaN(e),N=e=>e===true||e===false,E=e=>e instanceof Date&&!Number.isNaN(e.getTime()),_=e=>Array.isArray(e),O=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),R={object(e,u){let p=l(O,{...u,type:"object"});return p._getDescription=()=>`object({ ${Object.entries(e).map(([c,n])=>`${c}: ${n._getDescription()}`).join(", ")} })`,m(p,"_parse",(o,c,n)=>{let t=o(c,n),{abortEarly:r}=d(n);if(t.success===false)return t;let s={},a=[];for(let i in e){let f=e[i]._parse(t.data[i],n);if(f.success)s[i]=f.data;else {if(f=f,r!==false){let h=S(f.error,i);return {success:false,error:h,errors:[h]}}g(f,a,i);}}return a.length>0?{success:false,error:a[0],errors:a}:{success:true,data:s}}),p},string(e){return l(w,{...e,type:"string"})},number(e){return l(P,{...e,type:"number"})},boolean(e){return l(N,{...e,type:"boolean"})},date(e){return l(E,{...e,type:"date"})},enum(e,u){let p=t=>e.includes(t),o=t=>`Invalid ${c} value. Expected ${e.map(r=>`"${r}"`).join(" | ")}, received "${t}".`,c="enum",n=l(p,{message:o,...u,type:c});return n._getDescription=()=>`enum(${e.map(t=>`"${t}"`).join(" | ")})`,n},array(e,u){let p=l(_,{...u,type:"array"});return p._getDescription=()=>`array(${e._getDescription()})`,m(p,"_parse",(o,c,n)=>{let t=o(c,n),{abortEarly:r}=d(n);if(t.success===false)return t;let s=[],a=[];for(let i=0;i<t.data.length;i++){let f=e._parse(t.data[i],n);if(f.success)s.push(f.data);else {if(f=f,r!==false){let h=S(f.error,i);return {success:false,error:h,errors:[h]}}g(f,a,i);}}return a.length>0?{success:false,error:a[0],errors:a}:{success:true,data:s}}),p},any(){return l(()=>true)},preprocess(e,u){return m(u,"_parse",(p,o)=>(o=e(o),p(o))),u},union(e,u){return l(n=>{for(let t=0;t<e.length;t++){let r=e[t]._parse(n);if(r.success)return r}return false},{message:n=>`Invalid union value. Expected the value to match one of the schemas:${e.map((t,r)=>` ${r+1}. ${t._getDescription()}`).join(",")} but received "${typeof n}" with value: ${O(n)?JSON.stringify(n):`"${n}"`}`,...u,type:"union"})},literal(e,u){let c=l(n=>n===e,{message:n=>u?.message?typeof u.message=="function"?u.message(n):u.message:`Expected literal value "${e}", received "${n}"`,name:u?.name,type:"literal"});return c._getDescription=()=>`literal("${e}")`,c}};function T(e){return u=>`The value "${u}" must be type of ${e} but is type of "${typeof u}".`}function m(e,u,p){let o=e[u];e[u]=(...c)=>p(o,...c);}function l(e,{type:u="any",name:p,message:o}={}){o=o||T(u);let c={name:p,message:o,type:u},n={_getName(){return p},_getType(){return u},_getDescription(){return this._getName()??this._getType()},_parse(t,r){let s=e(t);if(s===true)return {success:true,data:t};if(typeof s=="object"&&s?.success===true)return s;let a={message:typeof o=="function"?o(t):o};return {success:false,error:a,errors:[a]}},parse(t,r){let s=n._parse(t,r);if(!s.success)throw s=s,new Error(s.error.message,{cause:s.error.cause});return s.data},safeParse(t,r){return n._parse(t,r)},transform(t){return m(this,"_parse",(r,s,a)=>{let i=r(s,a);return i.success&&(i.data=t(i.data)),i}),this},optional(){return m(this,"_parse",(t,r,s)=>{let a=t(r,s);return !a.success&&r===void 0&&(a.data=void 0,a.success=true),a}),this},nullable(){return m(this,"_parse",(t,r,s)=>{let a=t(r,s);return !a.success&&r===null&&(a.data=null,a.success=true),a}),this},nullish(){return m(this,"_parse",(t,r,s)=>{let a=t(r,s);return !a.success&&r==null&&(a.success=true,a.data=r),a}),this},default(t){return m(this,"_parse",(r,s,a)=>(s===void 0&&(s=typeof t=="function"?t():t),r(s,a))),this},catch(t){return m(this,"_parse",(r,s,a)=>{let i=r(s,a);return i.success?i:{success:true,data:typeof t=="function"?t({input:s,error:i.error}):t}}),this},pipe(t){return m(this,"_parse",(r,s,a)=>{let i=r(s,a);return i.success?t._parse(i.data,a):i}),this},refine(t,{message:r,type:s}={}){return s&&(r=r||T(s),u=s),m(this,"_parse",(a,i,f)=>{let h=a(i,f);d(f);if(!h.success)return h;let I=t(h.data);if(I===true||typeof I=="object"&&I?.success===true)return h;let b={message:typeof r=="function"?r(i):r};return {success:false,error:b,errors:[b]}}),this}};return y.length>0?y.reduce((t,r)=>r(t,e,c)??t,n):n}var y=[];function x(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");y.push(e);}x((e,u,p)=>{if(p?.type==="number"){let o=e;o.min=function(c,{message:n}={}){return this.refine(t=>t>=c,{message:n||`Number must be greater than or equal to ${c}.`})},o.max=function(c,{message:n}={}){return this.refine(t=>t<=c,{message:n||`Number must be less than or equal to ${c}.`})},o.positive=function({message:c}={}){return this.refine(n=>n>0,{message:c||"Number must be positive."})},o.negative=function({message:c}={}){return this.refine(n=>n<0,{message:c||"Number must be negative."})},o.int=function({message:c}={}){return this.refine(n=>Number.isInteger(n),{message:c||"Number must be an integer."})},o.float=function({message:c}={}){return this.refine(n=>Number.isFinite(n)&&!Number.isInteger(n),{message:c||"Number must be a floating point (non-integer)."})},o.multipleOf=function(c,{message:n}={}){return this.refine(t=>t%c===0,{message:n||`Number must be a multiple of ${c}.`})},o.finite=function({message:c}={}){return this.refine(n=>Number.isFinite(n),{message:c||"Number must be finite."})};}return e});exports.extend=x;exports.hookOriginal=m;exports.s=R;
package/dist/number.mjs CHANGED
@@ -1 +1 @@
1
- import'./chunk-W2H6TLSH.mjs';export{c as extend,b as hookOriginal,a as s}from'./chunk-V2T3CZFC.mjs';
1
+ import'./chunk-PH4B25KI.mjs';export{c as extend,b as hookOriginal,a as s}from'./chunk-2NCXW7ID.mjs';
package/dist/string.cjs CHANGED
@@ -1 +1 @@
1
- 'use strict';var k={abortEarly:true};function I(t,o){if(!o)return t;let i=t?.cause?.key?`${o}.${t.cause.key}`:`${o}`;return {message:`Error parsing key "${i}": ${t.message}`,cause:{key:i}}}function b(t,o,i){if(t.errors?.length)for(let s=0;s<t.errors.length;s++)o.push(I(t.errors[s],i));}function d(t){return {...k,...t}}var w=t=>typeof t=="string"||t instanceof String,P=t=>(typeof t=="number"||t instanceof Number)&&!Number.isNaN(t),$=t=>t===true||t===false,E=t=>t instanceof Date&&!Number.isNaN(t.getTime()),_=t=>Array.isArray(t),T=t=>typeof t=="object"&&t!==null&&!Array.isArray(t),A={object(t,o){let i=h(T,{...o,type:"object"});return i._getDescription=()=>`object({ ${Object.entries(t).map(([r,n])=>`${r}: ${n._getDescription()}`).join(", ")} })`,l(i,"_parse",(s,r,n)=>{let e=s(r,n),{abortEarly:a}=d(n);if(e.success===false)return e;let u={},c=[];for(let p in t){let f=t[p]._parse(e.data[p],n);if(f.success)u[p]=f.data;else {if(f=f,a!==false){let m=I(f.error,p);return {success:false,error:m,errors:[m]}}b(f,c,p);}}return c.length>0?{success:false,error:c[0],errors:c}:{success:true,data:u}}),i},string(t){return h(w,{...t,type:"string"})},number(t){return h(P,{...t,type:"number"})},boolean(t){return h($,{...t,type:"boolean"})},date(t){return h(E,{...t,type:"date"})},enum(t,o){let i=e=>t.includes(e),s=e=>`Invalid ${r} value. Expected ${t.map(a=>`"${a}"`).join(" | ")}, received "${e}".`,r="enum",n=h(i,{message:s,...o,type:r});return n._getDescription=()=>`enum(${t.map(e=>`"${e}"`).join(" | ")})`,n},array(t,o){let i=h(_,{...o,type:"array"});return i._getDescription=()=>`array(${t._getDescription()})`,l(i,"_parse",(s,r,n)=>{let e=s(r,n),{abortEarly:a}=d(n);if(e.success===false)return e;let u=[],c=[];for(let p=0;p<e.data.length;p++){let f=t._parse(e.data[p],n);if(f.success)u.push(f.data);else {if(f=f,a!==false){let m=I(f.error,p);return {success:false,error:m,errors:[m]}}b(f,c,p);}}return c.length>0?{success:false,error:c[0],errors:c}:{success:true,data:u}}),i},any(){return h(()=>true)},preprocess(t,o){return l(o,"_parse",(i,s)=>(s=t(s),i(s))),o},union(t,o){return h(n=>{for(let e=0;e<t.length;e++){let a=t[e]._parse(n);if(a.success)return a}return false},{message:n=>`Invalid union value. Expected the value to match one of the schemas:${t.map((e,a)=>` ${a+1}. ${e._getDescription()}`).join(",")} but received "${typeof n}" with value: ${T(n)?JSON.stringify(n):`"${n}"`}`,...o,type:"union"})},literal(t,o){let r=h(n=>n===t,{message:n=>o?.message?typeof o.message=="function"?o.message(n):o.message:`Expected literal value "${t}", received "${n}"`,name:o?.name,type:"literal"});return r._getDescription=()=>`literal("${t}")`,r}};function O(t){return o=>`The value "${o}" must be type of ${t} but is type of "${typeof o}".`}function l(t,o,i){let s=t[o];t[o]=(...r)=>i(s,...r);}function h(t,{type:o="any",name:i,message:s}={}){s=s||O(o);let r={name:i,message:s,type:o},n={_getName(){return i},_getType(){return o},_getDescription(){return this._getName()??this._getType()},_parse(e,a){let u=t(e);if(u===true)return {success:true,data:e};if(typeof u=="object"&&u?.success===true)return u;let c={message:typeof s=="function"?s(e):s};return {success:false,error:c,errors:[c]}},parse(e,a){let u=n._parse(e,a);if(!u.success)throw u=u,new Error(u.error.message,{cause:u.error.cause});return u.data},safeParse(e,a){return n._parse(e,a)},transform(e){return l(this,"_parse",(a,u,c)=>{let p=a(u,c);return p.success&&(p.data=e(p.data)),p}),this},optional(){return l(this,"_parse",(e,a,u)=>{let c=e(a,u);return !c.success&&a===void 0&&(c.data=void 0,c.success=true),c}),this},nullable(){return l(this,"_parse",(e,a,u)=>{let c=e(a,u);return !c.success&&a===null&&(c.data=null,c.success=true),c}),this},nullish(){return l(this,"_parse",(e,a,u)=>{let c=e(a,u);return !c.success&&a==null&&(c.success=true,c.data=a),c}),this},default(e){return l(this,"_parse",(a,u,c)=>(u===void 0&&(u=typeof e=="function"?e():e),a(u,c))),this},pipe(e){return l(this,"_parse",(a,u,c)=>{let p=a(u,c);return p.success?e._parse(p.data,c):p}),this},refine(e,{message:a,type:u}={}){return u&&(a=a||O(u),o=u),l(this,"_parse",(c,p,f)=>{let m=c(p,f);d(f);if(!m.success)return m;let S=e(m.data);if(S===true||typeof S=="object"&&S?.success===true)return m;let y={message:typeof a=="function"?a(p):a};return {success:false,error:y,errors:[y]}}),this}};return g.length>0?g.reduce((e,a)=>a(e,t,r)??e,n):n}var g=[];function x(t){if(typeof t!="function")throw new TypeError("extend() requires a function argument");g.push(t);}x((t,o,i)=>{if(i?.type==="string"){let s=t;s.min=function(r,{message:n}={}){return this.refine(e=>e.length>=r,{message:n||(e=>`String must be at least ${r} characters long (received ${e.length} characters: "${e}")`)})},s.max=function(r,{message:n}={}){return this.refine(e=>e.length<=r,{message:n||(e=>`String must be at most ${r} characters long (received ${e.length} characters: "${e}")`)})},s.length=function(r,{message:n}={}){return this.refine(e=>e.length===r,{message:n||(e=>`String must be exactly ${r} characters long (received ${e.length} characters: "${e}")`)})},s.nonEmpty=function({message:r}={}){return this.refine(n=>n.length>0,{message:r||"String must not be empty (received empty string)"})},s.startsWith=function(r,{message:n}={}){return this.refine(e=>e.startsWith(r),{message:n||(e=>`String must start with "${r}" (received: "${e}")`)})},s.endsWith=function(r,{message:n}={}){return this.refine(e=>e.endsWith(r),{message:n||(e=>`String must end with "${r}" (received: "${e}")`)})},s.includes=function(r,{message:n}={}){return this.refine(e=>e.includes(r),{message:n||(e=>`String must include "${r}" (received: "${e}")`)})},s.toLowerCase=function(){return this.transform(r=>r.toLowerCase())},s.toUpperCase=function(){return this.transform(r=>r.toUpperCase())},s.trim=function(){return this.transform(r=>r.trim())},s.padStart=function(r,n=" "){return this.transform(e=>e.padStart(r,n))},s.padEnd=function(r,n=" "){return this.transform(e=>e.padEnd(r,n))},s.replace=function(r,n){return this.transform(e=>e.replace(r,n))};}return t});exports.extend=x;exports.hookOriginal=l;exports.s=A;
1
+ 'use strict';var k={abortEarly:true};function I(t,o){if(!o)return t;let p=t?.cause?.key?`${o}.${t.cause.key}`:`${o}`;return {message:`Error parsing key "${p}": ${t.message}`,cause:{key:p}}}function O(t,o,p){if(t.errors?.length)for(let s=0;s<t.errors.length;s++)o.push(I(t.errors[s],p));}function d(t){return {...k,...t}}var w=t=>typeof t=="string"||t instanceof String,P=t=>(typeof t=="number"||t instanceof Number)&&!Number.isNaN(t),E=t=>t===true||t===false,$=t=>t instanceof Date&&!Number.isNaN(t.getTime()),_=t=>Array.isArray(t),b=t=>typeof t=="object"&&t!==null&&!Array.isArray(t),A={object(t,o){let p=l(b,{...o,type:"object"});return p._getDescription=()=>`object({ ${Object.entries(t).map(([r,n])=>`${r}: ${n._getDescription()}`).join(", ")} })`,m(p,"_parse",(s,r,n)=>{let e=s(r,n),{abortEarly:a}=d(n);if(e.success===false)return e;let u={},c=[];for(let i in t){let f=t[i]._parse(e.data[i],n);if(f.success)u[i]=f.data;else {if(f=f,a!==false){let h=I(f.error,i);return {success:false,error:h,errors:[h]}}O(f,c,i);}}return c.length>0?{success:false,error:c[0],errors:c}:{success:true,data:u}}),p},string(t){return l(w,{...t,type:"string"})},number(t){return l(P,{...t,type:"number"})},boolean(t){return l(E,{...t,type:"boolean"})},date(t){return l($,{...t,type:"date"})},enum(t,o){let p=e=>t.includes(e),s=e=>`Invalid ${r} value. Expected ${t.map(a=>`"${a}"`).join(" | ")}, received "${e}".`,r="enum",n=l(p,{message:s,...o,type:r});return n._getDescription=()=>`enum(${t.map(e=>`"${e}"`).join(" | ")})`,n},array(t,o){let p=l(_,{...o,type:"array"});return p._getDescription=()=>`array(${t._getDescription()})`,m(p,"_parse",(s,r,n)=>{let e=s(r,n),{abortEarly:a}=d(n);if(e.success===false)return e;let u=[],c=[];for(let i=0;i<e.data.length;i++){let f=t._parse(e.data[i],n);if(f.success)u.push(f.data);else {if(f=f,a!==false){let h=I(f.error,i);return {success:false,error:h,errors:[h]}}O(f,c,i);}}return c.length>0?{success:false,error:c[0],errors:c}:{success:true,data:u}}),p},any(){return l(()=>true)},preprocess(t,o){return m(o,"_parse",(p,s)=>(s=t(s),p(s))),o},union(t,o){return l(n=>{for(let e=0;e<t.length;e++){let a=t[e]._parse(n);if(a.success)return a}return false},{message:n=>`Invalid union value. Expected the value to match one of the schemas:${t.map((e,a)=>` ${a+1}. ${e._getDescription()}`).join(",")} but received "${typeof n}" with value: ${b(n)?JSON.stringify(n):`"${n}"`}`,...o,type:"union"})},literal(t,o){let r=l(n=>n===t,{message:n=>o?.message?typeof o.message=="function"?o.message(n):o.message:`Expected literal value "${t}", received "${n}"`,name:o?.name,type:"literal"});return r._getDescription=()=>`literal("${t}")`,r}};function T(t){return o=>`The value "${o}" must be type of ${t} but is type of "${typeof o}".`}function m(t,o,p){let s=t[o];t[o]=(...r)=>p(s,...r);}function l(t,{type:o="any",name:p,message:s}={}){s=s||T(o);let r={name:p,message:s,type:o},n={_getName(){return p},_getType(){return o},_getDescription(){return this._getName()??this._getType()},_parse(e,a){let u=t(e);if(u===true)return {success:true,data:e};if(typeof u=="object"&&u?.success===true)return u;let c={message:typeof s=="function"?s(e):s};return {success:false,error:c,errors:[c]}},parse(e,a){let u=n._parse(e,a);if(!u.success)throw u=u,new Error(u.error.message,{cause:u.error.cause});return u.data},safeParse(e,a){return n._parse(e,a)},transform(e){return m(this,"_parse",(a,u,c)=>{let i=a(u,c);return i.success&&(i.data=e(i.data)),i}),this},optional(){return m(this,"_parse",(e,a,u)=>{let c=e(a,u);return !c.success&&a===void 0&&(c.data=void 0,c.success=true),c}),this},nullable(){return m(this,"_parse",(e,a,u)=>{let c=e(a,u);return !c.success&&a===null&&(c.data=null,c.success=true),c}),this},nullish(){return m(this,"_parse",(e,a,u)=>{let c=e(a,u);return !c.success&&a==null&&(c.success=true,c.data=a),c}),this},default(e){return m(this,"_parse",(a,u,c)=>(u===void 0&&(u=typeof e=="function"?e():e),a(u,c))),this},catch(e){return m(this,"_parse",(a,u,c)=>{let i=a(u,c);return i.success?i:{success:true,data:typeof e=="function"?e({input:u,error:i.error}):e}}),this},pipe(e){return m(this,"_parse",(a,u,c)=>{let i=a(u,c);return i.success?e._parse(i.data,c):i}),this},refine(e,{message:a,type:u}={}){return u&&(a=a||T(u),o=u),m(this,"_parse",(c,i,f)=>{let h=c(i,f);d(f);if(!h.success)return h;let S=e(h.data);if(S===true||typeof S=="object"&&S?.success===true)return h;let y={message:typeof a=="function"?a(i):a};return {success:false,error:y,errors:[y]}}),this}};return g.length>0?g.reduce((e,a)=>a(e,t,r)??e,n):n}var g=[];function x(t){if(typeof t!="function")throw new TypeError("extend() requires a function argument");g.push(t);}x((t,o,p)=>{if(p?.type==="string"){let s=t;s.min=function(r,{message:n}={}){return this.refine(e=>e.length>=r,{message:n||(e=>`String must be at least ${r} characters long (received ${e.length} characters: "${e}")`)})},s.max=function(r,{message:n}={}){return this.refine(e=>e.length<=r,{message:n||(e=>`String must be at most ${r} characters long (received ${e.length} characters: "${e}")`)})},s.length=function(r,{message:n}={}){return this.refine(e=>e.length===r,{message:n||(e=>`String must be exactly ${r} characters long (received ${e.length} characters: "${e}")`)})},s.nonEmpty=function({message:r}={}){return this.refine(n=>n.length>0,{message:r||"String must not be empty (received empty string)"})},s.startsWith=function(r,{message:n}={}){return this.refine(e=>e.startsWith(r),{message:n||(e=>`String must start with "${r}" (received: "${e}")`)})},s.endsWith=function(r,{message:n}={}){return this.refine(e=>e.endsWith(r),{message:n||(e=>`String must end with "${r}" (received: "${e}")`)})},s.includes=function(r,{message:n}={}){return this.refine(e=>e.includes(r),{message:n||(e=>`String must include "${r}" (received: "${e}")`)})},s.toLowerCase=function(){return this.transform(r=>r.toLowerCase())},s.toUpperCase=function(){return this.transform(r=>r.toUpperCase())},s.trim=function(){return this.transform(r=>r.trim())},s.padStart=function(r,n=" "){return this.transform(e=>e.padStart(r,n))},s.padEnd=function(r,n=" "){return this.transform(e=>e.padEnd(r,n))},s.replace=function(r,n){return this.transform(e=>e.replace(r,n))};}return t});exports.extend=x;exports.hookOriginal=m;exports.s=A;
package/dist/string.mjs CHANGED
@@ -1 +1 @@
1
- import'./chunk-FEEE6IG6.mjs';export{c as extend,b as hookOriginal,a as s}from'./chunk-V2T3CZFC.mjs';
1
+ import'./chunk-LZRUIXQL.mjs';export{c as extend,b as hookOriginal,a as s}from'./chunk-2NCXW7ID.mjs';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@esmj/schema",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
4
4
  "description": "Tiny extendable package for schema validation.",
5
5
  "keywords": [
6
6
  "schema",
@@ -1 +0,0 @@
1
- var k={abortEarly:true};function d(e,s){if(!s)return e;let i=e?.cause?.key?`${s}.${e.cause.key}`:`${s}`;return {message:`Error parsing key "${i}": ${e.message}`,cause:{key:i}}}function T(e,s,i){if(e.errors?.length)for(let u=0;u<e.errors.length;u++)s.push(d(e.errors[u],i));}function y(e){return {...k,...e}}var x=e=>typeof e=="string"||e instanceof String,w=e=>(typeof e=="number"||e instanceof Number)&&!Number.isNaN(e),P=e=>e===true||e===false,E=e=>e instanceof Date&&!Number.isNaN(e.getTime()),_=e=>Array.isArray(e),b=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),$={object(e,s){let i=l(b,{...s,type:"object"});return i._getDescription=()=>`object({ ${Object.entries(e).map(([p,c])=>`${p}: ${c._getDescription()}`).join(", ")} })`,h(i,"_parse",(u,p,c)=>{let t=u(p,c),{abortEarly:n}=y(c);if(t.success===false)return t;let a={},r=[];for(let o in e){let f=e[o]._parse(t.data[o],c);if(f.success)a[o]=f.data;else {if(f=f,n!==false){let m=d(f.error,o);return {success:false,error:m,errors:[m]}}T(f,r,o);}}return r.length>0?{success:false,error:r[0],errors:r}:{success:true,data:a}}),i},string(e){return l(x,{...e,type:"string"})},number(e){return l(w,{...e,type:"number"})},boolean(e){return l(P,{...e,type:"boolean"})},date(e){return l(E,{...e,type:"date"})},enum(e,s){let i=t=>e.includes(t),u=t=>`Invalid ${p} value. Expected ${e.map(n=>`"${n}"`).join(" | ")}, received "${t}".`,p="enum",c=l(i,{message:u,...s,type:p});return c._getDescription=()=>`enum(${e.map(t=>`"${t}"`).join(" | ")})`,c},array(e,s){let i=l(_,{...s,type:"array"});return i._getDescription=()=>`array(${e._getDescription()})`,h(i,"_parse",(u,p,c)=>{let t=u(p,c),{abortEarly:n}=y(c);if(t.success===false)return t;let a=[],r=[];for(let o=0;o<t.data.length;o++){let f=e._parse(t.data[o],c);if(f.success)a.push(f.data);else {if(f=f,n!==false){let m=d(f.error,o);return {success:false,error:m,errors:[m]}}T(f,r,o);}}return r.length>0?{success:false,error:r[0],errors:r}:{success:true,data:a}}),i},any(){return l(()=>true)},preprocess(e,s){return h(s,"_parse",(i,u)=>(u=e(u),i(u))),s},union(e,s){return l(c=>{for(let t=0;t<e.length;t++){let n=e[t]._parse(c);if(n.success)return n}return false},{message:c=>`Invalid union value. Expected the value to match one of the schemas:${e.map((t,n)=>` ${n+1}. ${t._getDescription()}`).join(",")} but received "${typeof c}" with value: ${b(c)?JSON.stringify(c):`"${c}"`}`,...s,type:"union"})},literal(e,s){let p=l(c=>c===e,{message:c=>s?.message?typeof s.message=="function"?s.message(c):s.message:`Expected literal value "${e}", received "${c}"`,name:s?.name,type:"literal"});return p._getDescription=()=>`literal("${e}")`,p}};function O(e){return s=>`The value "${s}" must be type of ${e} but is type of "${typeof s}".`}function h(e,s,i){let u=e[s];e[s]=(...p)=>i(u,...p);}function l(e,{type:s="any",name:i,message:u}={}){u=u||O(s);let p={name:i,message:u,type:s},c={_getName(){return i},_getType(){return s},_getDescription(){return this._getName()??this._getType()},_parse(t,n){let a=e(t);if(a===true)return {success:true,data:t};if(typeof a=="object"&&a?.success===true)return a;let r={message:typeof u=="function"?u(t):u};return {success:false,error:r,errors:[r]}},parse(t,n){let a=c._parse(t,n);if(!a.success)throw a=a,new Error(a.error.message,{cause:a.error.cause});return a.data},safeParse(t,n){return c._parse(t,n)},transform(t){return h(this,"_parse",(n,a,r)=>{let o=n(a,r);return o.success&&(o.data=t(o.data)),o}),this},optional(){return h(this,"_parse",(t,n,a)=>{let r=t(n,a);return !r.success&&n===void 0&&(r.data=void 0,r.success=true),r}),this},nullable(){return h(this,"_parse",(t,n,a)=>{let r=t(n,a);return !r.success&&n===null&&(r.data=null,r.success=true),r}),this},nullish(){return h(this,"_parse",(t,n,a)=>{let r=t(n,a);return !r.success&&n==null&&(r.success=true,r.data=n),r}),this},default(t){return h(this,"_parse",(n,a,r)=>(a===void 0&&(a=typeof t=="function"?t():t),n(a,r))),this},pipe(t){return h(this,"_parse",(n,a,r)=>{let o=n(a,r);return o.success?t._parse(o.data,r):o}),this},refine(t,{message:n,type:a}={}){return a&&(n=n||O(a),s=a),h(this,"_parse",(r,o,f)=>{let m=r(o,f);y(f);if(!m.success)return m;let I=t(m.data);if(I===true||typeof I=="object"&&I?.success===true)return m;let g={message:typeof n=="function"?n(o):n};return {success:false,error:g,errors:[g]}}),this}};return S.length>0?S.reduce((t,n)=>n(t,e,p)??t,c):c}var S=[];function A(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");S.push(e);}export{$ as a,h as b,A as c};