@esmj/schema 0.4.0 → 0.5.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 +657 -13
- package/dist/array.cjs +1 -0
- package/dist/array.d.cts +13 -0
- package/dist/array.d.ts +13 -0
- package/dist/array.mjs +1 -0
- package/dist/chunk-BV6SWQSH.mjs +1 -0
- package/dist/chunk-NCHMO7B3.mjs +1 -0
- package/dist/chunk-PHNKAD7Q.mjs +1 -0
- package/dist/chunk-SHVCROKM.mjs +1 -0
- package/dist/full.cjs +1 -0
- package/dist/full.d.cts +4 -0
- package/dist/full.d.ts +4 -0
- package/dist/full.mjs +1 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +270 -0
- package/dist/index.d.ts +193 -3
- package/dist/index.mjs +1 -1
- package/dist/number.cjs +1 -0
- package/dist/number.d.cts +14 -0
- package/dist/number.d.ts +14 -0
- package/dist/number.mjs +1 -0
- package/dist/string.cjs +1 -0
- package/dist/string.d.cts +19 -0
- package/dist/string.d.ts +19 -0
- package/dist/string.mjs +1 -0
- package/package.json +43 -13
- package/dist/index.d.mts +0 -80
- package/dist/index.js +0 -1
package/dist/array.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
'use strict';var P={abortEarly:true};function l(e,o){if(!o)return e;let i=e?.cause?.key?`${o}.${e.cause.key}`:`${o}`;return {message:`Error parsing key "${i}": ${e.message}`,cause:{key:i}}}function g(e,o,i){!e.errors||e.errors.length===0||e.errors.forEach(u=>{let c=l(u,i);o.push(c);});}function S(e){return {...P,...e}}var k=e=>typeof e=="string"||e instanceof String,A=e=>typeof e=="number"||e instanceof Number,w=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,o){let i=y(O,{...o,type:"object"});return i._getDescription=()=>`object({ ${Object.entries(e).map(([c,s])=>`${c}: ${s._getDescription()}`).join(", ")} })`,h(i,"_parse",(u,c,s)=>{let t=u(c,s),{abortEarly:r}=S(s);if(t.success===false)return t;let n={},a=[];for(let p in e){let f=e[p]._parse(t.data[p],s);if(f.success)n[p]=f.data;else {if(f=f,r!==false){let m=l(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:n}}),i},string(e){return y(k,{...e,type:"string"})},number(e){return y(A,{...e,type:"number"})},boolean(e){return y(w,{...e,type:"boolean"})},date(e){return y(E,{...e,type:"date"})},enum(e,o){let i=t=>e.includes(t),u=t=>`Invalid ${c} value. Expected ${e.map(r=>`"${r}"`).join(" | ")}, received "${t}".`,c="enum",s=y(i,{message:u,...o,type:c});return s._getDescription=()=>`enum(${e.map(t=>`"${t}"`).join(" | ")})`,s},array(e,o){let i=y(_,{...o,type:"array"});return i._getDescription=()=>`array(${e._getDescription()})`,h(i,"_parse",(u,c,s)=>{let t=u(c,s),{abortEarly:r}=S(s);if(t.success===false)return t;let n=[],a=[];for(let p=0;p<t.data.length;p++){let f=e._parse(t.data[p],s);if(f.success)n.push(f.data);else {if(f=f,r!==false){let m=l(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:n}}),i},any(){return y(()=>true)},preprocess(e,o){return h(o,"_parse",(i,u)=>(u=e(u),i(u))),o},union(e,o){return y(s=>{for(let t=0;t<e.length;t++){let r=e[t]._parse(s);if(r.success)return r}return false},{message:s=>`Invalid union value. Expected the value to match one of the schemas:${e.map((t,r)=>` ${r+1}. ${t._getDescription()}`).join(",")} but received "${typeof s}" with value: ${O(s)?JSON.stringify(s):`"${s}"`}`,...o,type:"union"})}};function b(e){return o=>`The value "${o}" must be type of ${e} but is type of "${typeof o}".`}function h(e,o,i){let u=e[o];e[o]=(...c)=>i(u,...c);}function y(e,{type:o="any",name:i,message:u}={}){u=u||b(o);let c={name:i,message:u,type:o},s={_getName(){return i},_getType(){return o},_getDescription(){return this._getName()??this._getType()},_parse(t,r){let n=e(t);if(n===true)return {success:true,data:t};if(typeof n=="object"&&n?.success===true)return n;let a={message:typeof u=="function"?u(t):u};return {success:false,error:a,errors:[a]}},parse(t,r){let n=s._parse(t,r);if(!n.success)throw n=n,new Error(n.error.message,{cause:n.error.cause});return n.data},safeParse(t,r){return s._parse(t,r)},transform(t){return h(this,"_parse",(r,n,a)=>{let p=r(n,a);return p.success&&(p.data=t(p.data)),p}),this},optional(){return h(this,"_parse",(t,r,n)=>{let a=t(r,n);return !a.success&&r===void 0&&(a.data=void 0,a.success=true),a}),this},nullable(){return h(this,"_parse",(t,r,n)=>{let a=t(r,n);return !a.success&&r===null&&(a.data=null,a.success=true),a}),this},nullish(){return h(this,"_parse",(t,r,n)=>{let a=t(r,n);return !a.success&&r==null&&(a.success=true,a.data=r),a}),this},default(t){return h(this,"_parse",(r,n,a)=>(n===void 0&&(n=t,n=typeof t=="function"?t():t),r(n,a))),this},pipe(t){return h(this,"_parse",(r,n,a)=>{let p=r(n,a);return p.success?t._parse(p.data,a):p}),this},refine(t,{message:r,type:n}={}){return n&&(r=r||b(n),o=n),h(this,"_parse",(a,p,f)=>{let m=a(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 T={message:typeof r=="function"?r(p):r};return {success:false,error:T,errors:[T]}}),this}};return d.length>0?d.reduce((t,r)=>r(t,e,c)??t,s):s}var d=[];function x(e){d.push(e);}x((e,o,i)=>{if(i?.type==="array"){let u=e;u.min=function(c,{message:s}={}){return this.refine(t=>t.length>=c,{message:s||`Array must contain at least ${c} items.`})},u.max=function(c,{message:s}={}){return this.refine(t=>t.length<=c,{message:s||`Array must contain at most ${c} items.`})},u.length=function(c,{message:s}={}){return this.refine(t=>t.length===c,{message:s||`Array must contain exactly ${c} items.`})},u.nonEmpty=function({message:c}={}){return this.refine(s=>s.length>0,{message:c||"Array must not be empty."})},u.unique=function({message:c}={}){return this.refine(s=>{let t=new Set;try{return s.every(r=>{let n=JSON.stringify(r);return t.has(n)?!1:(t.add(n),!0)})}catch{return new Set(s).size===s.length}},{message:c||"Array items must be unique."})},u.sort=function(){return this.transform(c=>[...c].sort())},u.reverse=function(){return this.transform(c=>[...c].reverse())};}return e});exports.extend=x;exports.hookOriginal=h;exports.s=R;
|
package/dist/array.d.cts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, Infer, Invalid, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s } from './index.cjs';
|
|
2
|
+
|
|
3
|
+
declare module './index.ts' {
|
|
4
|
+
interface ArraySchemaInterface<T extends SchemaType> {
|
|
5
|
+
min(length: number, options?: SchemaInterfaceOptions): ArraySchemaInterface<T>;
|
|
6
|
+
max(length: number, options?: SchemaInterfaceOptions): ArraySchemaInterface<T>;
|
|
7
|
+
length(length: number, options?: SchemaInterfaceOptions): ArraySchemaInterface<T>;
|
|
8
|
+
nonEmpty(options?: SchemaInterfaceOptions): ArraySchemaInterface<T>;
|
|
9
|
+
unique(options?: SchemaInterfaceOptions): ArraySchemaInterface<T>;
|
|
10
|
+
sort(): ArraySchemaInterface<T>;
|
|
11
|
+
reverse(): ArraySchemaInterface<T>;
|
|
12
|
+
}
|
|
13
|
+
}
|
package/dist/array.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, Infer, Invalid, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s } from './index.js';
|
|
2
|
+
|
|
3
|
+
declare module './index.ts' {
|
|
4
|
+
interface ArraySchemaInterface<T extends SchemaType> {
|
|
5
|
+
min(length: number, options?: SchemaInterfaceOptions): ArraySchemaInterface<T>;
|
|
6
|
+
max(length: number, options?: SchemaInterfaceOptions): ArraySchemaInterface<T>;
|
|
7
|
+
length(length: number, options?: SchemaInterfaceOptions): ArraySchemaInterface<T>;
|
|
8
|
+
nonEmpty(options?: SchemaInterfaceOptions): ArraySchemaInterface<T>;
|
|
9
|
+
unique(options?: SchemaInterfaceOptions): ArraySchemaInterface<T>;
|
|
10
|
+
sort(): ArraySchemaInterface<T>;
|
|
11
|
+
reverse(): ArraySchemaInterface<T>;
|
|
12
|
+
}
|
|
13
|
+
}
|
package/dist/array.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import'./chunk-PHNKAD7Q.mjs';export{c as extend,b as hookOriginal,a as s}from'./chunk-NCHMO7B3.mjs';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import {c}from'./chunk-NCHMO7B3.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});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var P={abortEarly:true};function d(e,s){if(!s)return e;let o=e?.cause?.key?`${s}.${e.cause.key}`:`${s}`;return {message:`Error parsing key "${o}": ${e.message}`,cause:{key:o}}}function b(e,s,o){!e.errors||e.errors.length===0||e.errors.forEach(i=>{let f=d(i,o);s.push(f);});}function y(e){return {...P,...e}}var x=e=>typeof e=="string"||e instanceof String,k=e=>typeof e=="number"||e instanceof Number,w=e=>e===true||e===false,E=e=>e instanceof Date&&!Number.isNaN(e.getTime()),_=e=>Array.isArray(e),g=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),R={object(e,s){let o=l(g,{...s,type:"object"});return o._getDescription=()=>`object({ ${Object.entries(e).map(([f,c])=>`${f}: ${c._getDescription()}`).join(", ")} })`,h(o,"_parse",(i,f,c)=>{let t=i(f,c),{abortEarly:r}=y(c);if(t.success===false)return t;let a={},n=[];for(let u in e){let p=e[u]._parse(t.data[u],c);if(p.success)a[u]=p.data;else {if(p=p,r!==false){let m=d(p.error,u);return {success:false,error:m,errors:[m]}}b(p,n,u);}}return n.length>0?{success:false,error:n[0],errors:n}:{success:true,data:a}}),o},string(e){return l(x,{...e,type:"string"})},number(e){return l(k,{...e,type:"number"})},boolean(e){return l(w,{...e,type:"boolean"})},date(e){return l(E,{...e,type:"date"})},enum(e,s){let o=t=>e.includes(t),i=t=>`Invalid ${f} value. Expected ${e.map(r=>`"${r}"`).join(" | ")}, received "${t}".`,f="enum",c=l(o,{message:i,...s,type:f});return c._getDescription=()=>`enum(${e.map(t=>`"${t}"`).join(" | ")})`,c},array(e,s){let o=l(_,{...s,type:"array"});return o._getDescription=()=>`array(${e._getDescription()})`,h(o,"_parse",(i,f,c)=>{let t=i(f,c),{abortEarly:r}=y(c);if(t.success===false)return t;let a=[],n=[];for(let u=0;u<t.data.length;u++){let p=e._parse(t.data[u],c);if(p.success)a.push(p.data);else {if(p=p,r!==false){let m=d(p.error,u);return {success:false,error:m,errors:[m]}}b(p,n,u);}}return n.length>0?{success:false,error:n[0],errors:n}:{success:true,data:a}}),o},any(){return l(()=>true)},preprocess(e,s){return h(s,"_parse",(o,i)=>(i=e(i),o(i))),s},union(e,s){return l(c=>{for(let t=0;t<e.length;t++){let r=e[t]._parse(c);if(r.success)return r}return false},{message:c=>`Invalid union value. Expected the value to match one of the schemas:${e.map((t,r)=>` ${r+1}. ${t._getDescription()}`).join(",")} but received "${typeof c}" with value: ${g(c)?JSON.stringify(c):`"${c}"`}`,...s,type:"union"})}};function O(e){return s=>`The value "${s}" must be type of ${e} but is type of "${typeof s}".`}function h(e,s,o){let i=e[s];e[s]=(...f)=>o(i,...f);}function l(e,{type:s="any",name:o,message:i}={}){i=i||O(s);let f={name:o,message:i,type:s},c={_getName(){return o},_getType(){return s},_getDescription(){return this._getName()??this._getType()},_parse(t,r){let a=e(t);if(a===true)return {success:true,data:t};if(typeof a=="object"&&a?.success===true)return a;let n={message:typeof i=="function"?i(t):i};return {success:false,error:n,errors:[n]}},parse(t,r){let a=c._parse(t,r);if(!a.success)throw a=a,new Error(a.error.message,{cause:a.error.cause});return a.data},safeParse(t,r){return c._parse(t,r)},transform(t){return h(this,"_parse",(r,a,n)=>{let u=r(a,n);return u.success&&(u.data=t(u.data)),u}),this},optional(){return h(this,"_parse",(t,r,a)=>{let n=t(r,a);return !n.success&&r===void 0&&(n.data=void 0,n.success=true),n}),this},nullable(){return h(this,"_parse",(t,r,a)=>{let n=t(r,a);return !n.success&&r===null&&(n.data=null,n.success=true),n}),this},nullish(){return h(this,"_parse",(t,r,a)=>{let n=t(r,a);return !n.success&&r==null&&(n.success=true,n.data=r),n}),this},default(t){return h(this,"_parse",(r,a,n)=>(a===void 0&&(a=t,a=typeof t=="function"?t():t),r(a,n))),this},pipe(t){return h(this,"_parse",(r,a,n)=>{let u=r(a,n);return u.success?t._parse(u.data,n):u}),this},refine(t,{message:r,type:a}={}){return a&&(r=r||O(a),s=a),h(this,"_parse",(n,u,p)=>{let m=n(u,p);y(p);if(!m.success)return m;let I=t(m.data);if(I===true||typeof I=="object"&&I?.success===true)return m;let T={message:typeof r=="function"?r(u):r};return {success:false,error:T,errors:[T]}}),this}};return S.length>0?S.reduce((t,r)=>r(t,e,f)??t,c):c}var S=[];function $(e){S.push(e);}export{R as a,h as b,$ as c};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import {c}from'./chunk-NCHMO7B3.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});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import {c}from'./chunk-NCHMO7B3.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
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
'use strict';var P={abortEarly:true};function y(r,o){if(!o)return r;let u=r?.cause?.key?`${o}.${r.cause.key}`:`${o}`;return {message:`Error parsing key "${u}": ${r.message}`,cause:{key:u}}}function O(r,o,u){!r.errors||r.errors.length===0||r.errors.forEach(a=>{let t=y(a,u);o.push(t);});}function d(r){return {...P,...r}}var $=r=>typeof r=="string"||r instanceof String,N=r=>typeof r=="number"||r instanceof Number,k=r=>r===true||r===false,w=r=>r instanceof Date&&!Number.isNaN(r.getTime()),E=r=>Array.isArray(r),T=r=>typeof r=="object"&&r!==null&&!Array.isArray(r),D={object(r,o){let u=S(T,{...o,type:"object"});return u._getDescription=()=>`object({ ${Object.entries(r).map(([t,n])=>`${t}: ${n._getDescription()}`).join(", ")} })`,h(u,"_parse",(a,t,n)=>{let e=a(t,n),{abortEarly:s}=d(n);if(e.success===false)return e;let c={},i=[];for(let m in r){let p=r[m]._parse(e.data[m],n);if(p.success)c[m]=p.data;else {if(p=p,s!==false){let f=y(p.error,m);return {success:false,error:f,errors:[f]}}O(p,i,m);}}return i.length>0?{success:false,error:i[0],errors:i}:{success:true,data:c}}),u},string(r){return S($,{...r,type:"string"})},number(r){return S(N,{...r,type:"number"})},boolean(r){return S(k,{...r,type:"boolean"})},date(r){return S(w,{...r,type:"date"})},enum(r,o){let u=e=>r.includes(e),a=e=>`Invalid ${t} value. Expected ${r.map(s=>`"${s}"`).join(" | ")}, received "${e}".`,t="enum",n=S(u,{message:a,...o,type:t});return n._getDescription=()=>`enum(${r.map(e=>`"${e}"`).join(" | ")})`,n},array(r,o){let u=S(E,{...o,type:"array"});return u._getDescription=()=>`array(${r._getDescription()})`,h(u,"_parse",(a,t,n)=>{let e=a(t,n),{abortEarly:s}=d(n);if(e.success===false)return e;let c=[],i=[];for(let m=0;m<e.data.length;m++){let p=r._parse(e.data[m],n);if(p.success)c.push(p.data);else {if(p=p,s!==false){let f=y(p.error,m);return {success:false,error:f,errors:[f]}}O(p,i,m);}}return i.length>0?{success:false,error:i[0],errors:i}:{success:true,data:c}}),u},any(){return S(()=>true)},preprocess(r,o){return h(o,"_parse",(u,a)=>(a=r(a),u(a))),o},union(r,o){return S(n=>{for(let e=0;e<r.length;e++){let s=r[e]._parse(n);if(s.success)return s}return false},{message:n=>`Invalid union value. Expected the value to match one of the schemas:${r.map((e,s)=>` ${s+1}. ${e._getDescription()}`).join(",")} but received "${typeof n}" with value: ${T(n)?JSON.stringify(n):`"${n}"`}`,...o,type:"union"})}};function x(r){return o=>`The value "${o}" must be type of ${r} but is type of "${typeof o}".`}function h(r,o,u){let a=r[o];r[o]=(...t)=>u(a,...t);}function S(r,{type:o="any",name:u,message:a}={}){a=a||x(o);let t={name:u,message:a,type:o},n={_getName(){return u},_getType(){return o},_getDescription(){return this._getName()??this._getType()},_parse(e,s){let c=r(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=n._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 n._parse(e,s)},transform(e){return h(this,"_parse",(s,c,i)=>{let m=s(c,i);return m.success&&(m.data=e(m.data)),m}),this},optional(){return h(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 h(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 h(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 h(this,"_parse",(s,c,i)=>(c===void 0&&(c=e,c=typeof e=="function"?e():e),s(c,i))),this},pipe(e){return h(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),o=c),h(this,"_parse",(i,m,p)=>{let f=i(m,p);d(p);if(!f.success)return f;let l=e(f.data);if(l===true||typeof l=="object"&&l?.success===true)return f;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,r,t)??e,n):n}var g=[];function I(r){g.push(r);}I((r,o,u)=>{if(u?.type==="string"){let a=r;a.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}")`)})},a.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}")`)})},a.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}")`)})},a.nonEmpty=function({message:t}={}){return this.refine(n=>n.length>0,{message:t||"String must not be empty (received empty string)"})},a.startsWith=function(t,{message:n}={}){return this.refine(e=>e.startsWith(t),{message:n||(e=>`String must start with "${t}" (received: "${e}")`)})},a.endsWith=function(t,{message:n}={}){return this.refine(e=>e.endsWith(t),{message:n||(e=>`String must end with "${t}" (received: "${e}")`)})},a.includes=function(t,{message:n}={}){return this.refine(e=>e.includes(t),{message:n||(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,n=" "){return this.transform(e=>e.padStart(t,n))},a.padEnd=function(t,n=" "){return this.transform(e=>e.padEnd(t,n))},a.replace=function(t,n){return this.transform(e=>e.replace(t,n))};}return r});I((r,o,u)=>{if(u?.type==="number"){let a=r;a.min=function(t,{message:n}={}){return this.refine(e=>e>=t,{message:n||`Number must be greater than or equal to ${t}.`})},a.max=function(t,{message:n}={}){return this.refine(e=>e<=t,{message:n||`Number must be less than or equal to ${t}.`})},a.positive=function({message:t}={}){return this.refine(n=>n>0,{message:t||"Number must be positive."})},a.negative=function({message:t}={}){return this.refine(n=>n<0,{message:t||"Number must be negative."})},a.int=function({message:t}={}){return this.refine(n=>Number.isInteger(n),{message:t||"Number must be an integer."})},a.float=function({message:t}={}){return this.refine(n=>Number.isFinite(n)&&!Number.isInteger(n),{message:t||"Number must be a floating point (non-integer)."})},a.multipleOf=function(t,{message:n}={}){return this.refine(e=>e%t===0,{message:n||`Number must be a multiple of ${t}.`})},a.finite=function({message:t}={}){return this.refine(n=>Number.isFinite(n),{message:t||"Number must be finite."})};}return r});I((r,o,u)=>{if(u?.type==="array"){let a=r;a.min=function(t,{message:n}={}){return this.refine(e=>e.length>=t,{message:n||`Array must contain at least ${t} items.`})},a.max=function(t,{message:n}={}){return this.refine(e=>e.length<=t,{message:n||`Array must contain at most ${t} items.`})},a.length=function(t,{message:n}={}){return this.refine(e=>e.length===t,{message:n||`Array must contain exactly ${t} items.`})},a.nonEmpty=function({message:t}={}){return this.refine(n=>n.length>0,{message:t||"Array must not be empty."})},a.unique=function({message:t}={}){return this.refine(n=>{let e=new Set;try{return n.every(s=>{let c=JSON.stringify(s);return e.has(c)?!1:(e.add(c),!0)})}catch{return new Set(n).size===n.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 r});exports.extend=I;exports.hookOriginal=h;exports.s=D;
|
package/dist/full.d.cts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import './string.cjs';
|
|
2
|
+
import './number.cjs';
|
|
3
|
+
import './array.cjs';
|
|
4
|
+
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, Infer, Invalid, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s } from './index.cjs';
|
package/dist/full.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import './string.js';
|
|
2
|
+
import './number.js';
|
|
3
|
+
import './array.js';
|
|
4
|
+
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, Infer, Invalid, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s } from './index.js';
|
package/dist/full.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import'./chunk-PHNKAD7Q.mjs';import'./chunk-SHVCROKM.mjs';import'./chunk-BV6SWQSH.mjs';export{c as extend,b as hookOriginal,a as s}from'./chunk-NCHMO7B3.mjs';
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
'use strict';var P={abortEarly:true};function d(e,s){if(!s)return e;let o=e?.cause?.key?`${s}.${e.cause.key}`:`${s}`;return {message:`Error parsing key "${o}": ${e.message}`,cause:{key:o}}}function b(e,s,o){!e.errors||e.errors.length===0||e.errors.forEach(i=>{let f=d(i,o);s.push(f);});}function y(e){return {...P,...e}}var x=e=>typeof e=="string"||e instanceof String,k=e=>typeof e=="number"||e instanceof Number,w=e=>e===true||e===false,E=e=>e instanceof Date&&!Number.isNaN(e.getTime()),_=e=>Array.isArray(e),g=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),R={object(e,s){let o=l(g,{...s,type:"object"});return o._getDescription=()=>`object({ ${Object.entries(e).map(([f,c])=>`${f}: ${c._getDescription()}`).join(", ")} })`,h(o,"_parse",(i,f,c)=>{let t=i(f,c),{abortEarly:r}=y(c);if(t.success===false)return t;let a={},n=[];for(let u in e){let p=e[u]._parse(t.data[u],c);if(p.success)a[u]=p.data;else {if(p=p,r!==false){let m=d(p.error,u);return {success:false,error:m,errors:[m]}}b(p,n,u);}}return n.length>0?{success:false,error:n[0],errors:n}:{success:true,data:a}}),o},string(e){return l(x,{...e,type:"string"})},number(e){return l(k,{...e,type:"number"})},boolean(e){return l(w,{...e,type:"boolean"})},date(e){return l(E,{...e,type:"date"})},enum(e,s){let o=t=>e.includes(t),i=t=>`Invalid ${f} value. Expected ${e.map(r=>`"${r}"`).join(" | ")}, received "${t}".`,f="enum",c=l(o,{message:i,...s,type:f});return c._getDescription=()=>`enum(${e.map(t=>`"${t}"`).join(" | ")})`,c},array(e,s){let o=l(_,{...s,type:"array"});return o._getDescription=()=>`array(${e._getDescription()})`,h(o,"_parse",(i,f,c)=>{let t=i(f,c),{abortEarly:r}=y(c);if(t.success===false)return t;let a=[],n=[];for(let u=0;u<t.data.length;u++){let p=e._parse(t.data[u],c);if(p.success)a.push(p.data);else {if(p=p,r!==false){let m=d(p.error,u);return {success:false,error:m,errors:[m]}}b(p,n,u);}}return n.length>0?{success:false,error:n[0],errors:n}:{success:true,data:a}}),o},any(){return l(()=>true)},preprocess(e,s){return h(s,"_parse",(o,i)=>(i=e(i),o(i))),s},union(e,s){return l(c=>{for(let t=0;t<e.length;t++){let r=e[t]._parse(c);if(r.success)return r}return false},{message:c=>`Invalid union value. Expected the value to match one of the schemas:${e.map((t,r)=>` ${r+1}. ${t._getDescription()}`).join(",")} but received "${typeof c}" with value: ${g(c)?JSON.stringify(c):`"${c}"`}`,...s,type:"union"})}};function O(e){return s=>`The value "${s}" must be type of ${e} but is type of "${typeof s}".`}function h(e,s,o){let i=e[s];e[s]=(...f)=>o(i,...f);}function l(e,{type:s="any",name:o,message:i}={}){i=i||O(s);let f={name:o,message:i,type:s},c={_getName(){return o},_getType(){return s},_getDescription(){return this._getName()??this._getType()},_parse(t,r){let a=e(t);if(a===true)return {success:true,data:t};if(typeof a=="object"&&a?.success===true)return a;let n={message:typeof i=="function"?i(t):i};return {success:false,error:n,errors:[n]}},parse(t,r){let a=c._parse(t,r);if(!a.success)throw a=a,new Error(a.error.message,{cause:a.error.cause});return a.data},safeParse(t,r){return c._parse(t,r)},transform(t){return h(this,"_parse",(r,a,n)=>{let u=r(a,n);return u.success&&(u.data=t(u.data)),u}),this},optional(){return h(this,"_parse",(t,r,a)=>{let n=t(r,a);return !n.success&&r===void 0&&(n.data=void 0,n.success=true),n}),this},nullable(){return h(this,"_parse",(t,r,a)=>{let n=t(r,a);return !n.success&&r===null&&(n.data=null,n.success=true),n}),this},nullish(){return h(this,"_parse",(t,r,a)=>{let n=t(r,a);return !n.success&&r==null&&(n.success=true,n.data=r),n}),this},default(t){return h(this,"_parse",(r,a,n)=>(a===void 0&&(a=t,a=typeof t=="function"?t():t),r(a,n))),this},pipe(t){return h(this,"_parse",(r,a,n)=>{let u=r(a,n);return u.success?t._parse(u.data,n):u}),this},refine(t,{message:r,type:a}={}){return a&&(r=r||O(a),s=a),h(this,"_parse",(n,u,p)=>{let m=n(u,p);y(p);if(!m.success)return m;let I=t(m.data);if(I===true||typeof I=="object"&&I?.success===true)return m;let T={message:typeof r=="function"?r(u):r};return {success:false,error:T,errors:[T]}}),this}};return S.length>0?S.reduce((t,r)=>r(t,e,f)??t,c):c}var S=[];function $(e){S.push(e);}exports.extend=$;exports.hookOriginal=h;exports.s=R;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
type ErrorStructure = {
|
|
2
|
+
message: string;
|
|
3
|
+
cause?: {
|
|
4
|
+
key?: string;
|
|
5
|
+
};
|
|
6
|
+
};
|
|
7
|
+
type Valid<Output> = {
|
|
8
|
+
success: true;
|
|
9
|
+
data: Output;
|
|
10
|
+
};
|
|
11
|
+
type Invalid = {
|
|
12
|
+
success: false;
|
|
13
|
+
error: ErrorStructure;
|
|
14
|
+
errors?: ErrorStructure[];
|
|
15
|
+
};
|
|
16
|
+
type InternalParseOutput<Output> = Valid<Output> | Invalid;
|
|
17
|
+
type ValidationMethod<Input, Output> = (value: Input | Partial<Input>) => boolean | InternalParseOutput<Output>;
|
|
18
|
+
interface ParseOptions {
|
|
19
|
+
abortEarly?: boolean;
|
|
20
|
+
}
|
|
21
|
+
interface SchemaInterface<Input, Output> {
|
|
22
|
+
_getName(): string;
|
|
23
|
+
_getType(): string;
|
|
24
|
+
_getDescription(): string;
|
|
25
|
+
_parse(value: Input | Partial<Input>, options?: ParseOptions): InternalParseOutput<Output>;
|
|
26
|
+
parse(value: Input | Partial<Input>, options?: ParseOptions): Output;
|
|
27
|
+
safeParse(value: Input | Partial<Input>, options?: ParseOptions): InternalParseOutput<Output>;
|
|
28
|
+
optional(): SchemaInterface<Input, Partial<Output> | undefined>;
|
|
29
|
+
transform<NewOutput>(callback: (value: Input) => NewOutput): SchemaInterface<Input, NewOutput>;
|
|
30
|
+
nullable(): SchemaInterface<Input, Output | null>;
|
|
31
|
+
nullish(): SchemaInterface<Input, Output | undefined | null>;
|
|
32
|
+
default(defaultValue: Partial<Input> | (() => Partial<Input>) | Partial<Output>): SchemaInterface<Input, Output>;
|
|
33
|
+
pipe<NewOutput>(schema: SchemaInterface<Output, NewOutput>): SchemaInterface<Output, NewOutput>;
|
|
34
|
+
refine(validation: ValidationMethod<Input, Output>, options?: CreateSchemaInterfaceOptions): SchemaInterface<Input, Output>;
|
|
35
|
+
}
|
|
36
|
+
interface UnionSchemaInterface<T extends Array<SchemaInterface<unknown, unknown>>> extends SchemaInterface<ReturnType<T[number]['parse']>, ReturnType<T[number]['parse']>> {
|
|
37
|
+
}
|
|
38
|
+
interface EnumSchemaInterface<T extends string> extends SchemaInterface<string, T> {
|
|
39
|
+
}
|
|
40
|
+
interface StringSchemaInterface extends SchemaInterface<string, string> {
|
|
41
|
+
}
|
|
42
|
+
interface NumberSchemaInterface extends SchemaInterface<number, number> {
|
|
43
|
+
}
|
|
44
|
+
interface BooleanSchemaInterface extends SchemaInterface<boolean, boolean> {
|
|
45
|
+
}
|
|
46
|
+
interface DateSchemaInterface extends SchemaInterface<Date, Date> {
|
|
47
|
+
}
|
|
48
|
+
interface ArraySchemaInterface<T extends SchemaType> extends SchemaInterface<Array<ReturnType<T['parse']>>, Array<ReturnType<T['parse']>>> {
|
|
49
|
+
}
|
|
50
|
+
interface ObjectSchemaInterface<T extends Record<string, SchemaType>> extends SchemaInterface<{
|
|
51
|
+
[Property in keyof T]: ReturnType<T[Property]['parse']>;
|
|
52
|
+
}, {
|
|
53
|
+
[Property in keyof T]: ReturnType<T[Property]['parse']>;
|
|
54
|
+
}> {
|
|
55
|
+
}
|
|
56
|
+
type SchemaType = StringSchemaInterface | SchemaInterface<unknown, unknown> | SchemaInterface<string, string> | SchemaInterface<string, string | undefined> | SchemaInterface<string, string | null> | SchemaInterface<string, string | undefined | null> | ObjectSchemaInterface<Record<string, SchemaType>> | SchemaInterface<object, object> | SchemaInterface<object, object | undefined> | SchemaInterface<object, object | null> | SchemaInterface<object, object | undefined | null> | NumberSchemaInterface | SchemaInterface<number, number> | SchemaInterface<number, number | undefined> | SchemaInterface<number, number | null> | SchemaInterface<number, number | undefined | null> | BooleanSchemaInterface | SchemaInterface<boolean, boolean> | SchemaInterface<boolean, boolean | undefined> | SchemaInterface<boolean, boolean | null> | SchemaInterface<boolean, boolean | undefined | null> | DateSchemaInterface | SchemaInterface<Date, Date> | SchemaInterface<Date, Date | undefined> | SchemaInterface<Date, Date | null> | SchemaInterface<Date, Date | undefined | null> | EnumSchemaInterface<string> | UnionSchemaInterface<Array<SchemaInterface<unknown, unknown>>> | ArraySchemaInterface<StringSchemaInterface | ObjectSchemaInterface<Record<string, SchemaType>> | NumberSchemaInterface | BooleanSchemaInterface | DateSchemaInterface | EnumSchemaInterface<string>> | SchemaInterface<Array<unknown>, Array<unknown>> | SchemaInterface<Array<unknown>, Array<unknown> | undefined> | SchemaInterface<Array<unknown>, Array<unknown> | null> | SchemaInterface<Array<unknown>, Array<unknown> | undefined | null>;
|
|
57
|
+
type ErrorMessage = string | ((value: unknown) => string);
|
|
58
|
+
type ExtenderType = (inter: SchemaType, validation: Function, options?: {
|
|
59
|
+
message: ErrorMessage;
|
|
60
|
+
type: string;
|
|
61
|
+
}) => SchemaType;
|
|
62
|
+
interface CreateSchemaInterfaceOptions {
|
|
63
|
+
name?: string;
|
|
64
|
+
type?: string;
|
|
65
|
+
message?: ErrorMessage;
|
|
66
|
+
}
|
|
67
|
+
type SchemaInterfaceOptions = Omit<CreateSchemaInterfaceOptions, 'type'>;
|
|
68
|
+
declare const s: {
|
|
69
|
+
/**
|
|
70
|
+
* Creates an object schema with validated fields.
|
|
71
|
+
*
|
|
72
|
+
* @param definition - Object containing field schemas
|
|
73
|
+
* @param options - Optional configuration (name, message)
|
|
74
|
+
* @returns Object schema interface
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```typescript
|
|
78
|
+
* const userSchema = s.object({
|
|
79
|
+
* name: s.string(),
|
|
80
|
+
* age: s.number()
|
|
81
|
+
* });
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
object<T extends Record<string, SchemaType>>(definition: { [Property in keyof T]: T[Property]; }, options?: SchemaInterfaceOptions): ObjectSchemaInterface<T>;
|
|
85
|
+
/**
|
|
86
|
+
* Creates a string schema.
|
|
87
|
+
*
|
|
88
|
+
* @param options - Optional configuration (name, message)
|
|
89
|
+
* @returns String schema interface
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```typescript
|
|
93
|
+
* const nameSchema = s.string();
|
|
94
|
+
* const result = nameSchema.parse('John'); // 'John'
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
string(options?: SchemaInterfaceOptions): StringSchemaInterface;
|
|
98
|
+
/**
|
|
99
|
+
* Creates a number schema.
|
|
100
|
+
*
|
|
101
|
+
* @param options - Optional configuration (name, message)
|
|
102
|
+
* @returns Number schema interface
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```typescript
|
|
106
|
+
* const ageSchema = s.number();
|
|
107
|
+
* const result = ageSchema.parse(25); // 25
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
number(options?: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
111
|
+
/**
|
|
112
|
+
* Creates a boolean schema.
|
|
113
|
+
*
|
|
114
|
+
* @param options - Optional configuration (name, message)
|
|
115
|
+
* @returns Boolean schema interface
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* const isActiveSchema = s.boolean();
|
|
120
|
+
* const result = isActiveSchema.parse(true); // true
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
boolean(options?: SchemaInterfaceOptions): BooleanSchemaInterface;
|
|
124
|
+
/**
|
|
125
|
+
* Creates a date schema.
|
|
126
|
+
*
|
|
127
|
+
* @param options - Optional configuration (name, message)
|
|
128
|
+
* @returns Date schema interface
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* ```typescript
|
|
132
|
+
* const birthdateSchema = s.date();
|
|
133
|
+
* const result = birthdateSchema.parse(new Date()); // Date object
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
date(options?: SchemaInterfaceOptions): DateSchemaInterface;
|
|
137
|
+
/**
|
|
138
|
+
* Creates an enum schema with predefined values.
|
|
139
|
+
*
|
|
140
|
+
* @param definition - Array of allowed string values
|
|
141
|
+
* @param options - Optional configuration (name, message)
|
|
142
|
+
* @returns Enum schema interface
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```typescript
|
|
146
|
+
* const roleSchema = s.enum(['admin', 'user', 'guest']);
|
|
147
|
+
* const result = roleSchema.parse('admin'); // 'admin'
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
enum(definition: Readonly<Array<string>>, options?: SchemaInterfaceOptions): EnumSchemaInterface<(typeof definition)[number]>;
|
|
151
|
+
/**
|
|
152
|
+
* Creates an array schema with element validation.
|
|
153
|
+
*
|
|
154
|
+
* @param definition - Schema for array elements
|
|
155
|
+
* @param options - Optional configuration (name, message)
|
|
156
|
+
* @returns Array schema interface
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```typescript
|
|
160
|
+
* const tagsSchema = s.array(s.string());
|
|
161
|
+
* const result = tagsSchema.parse(['tag1', 'tag2']); // ['tag1', 'tag2']
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
array<T extends SchemaType>(definition: T, options?: SchemaInterfaceOptions): ArraySchemaInterface<T>;
|
|
165
|
+
/**
|
|
166
|
+
* Creates a schema that accepts any value without validation.
|
|
167
|
+
*
|
|
168
|
+
* @returns Schema interface that accepts any value
|
|
169
|
+
*
|
|
170
|
+
* @example
|
|
171
|
+
* ```typescript
|
|
172
|
+
* const anySchema = s.any();
|
|
173
|
+
* const result = anySchema.parse({ anything: true }); // { anything: true }
|
|
174
|
+
* ```
|
|
175
|
+
*/
|
|
176
|
+
any(): any;
|
|
177
|
+
/**
|
|
178
|
+
* Preprocesses a value before passing it to a schema for validation.
|
|
179
|
+
*
|
|
180
|
+
* @param callback - Function to transform the value before validation
|
|
181
|
+
* @param schema - Schema to validate the transformed value
|
|
182
|
+
* @returns Modified schema with preprocessing
|
|
183
|
+
*
|
|
184
|
+
* @example
|
|
185
|
+
* ```typescript
|
|
186
|
+
* const schema = s.preprocess(
|
|
187
|
+
* (val) => String(val).trim(),
|
|
188
|
+
* s.string().min(3)
|
|
189
|
+
* );
|
|
190
|
+
* const result = schema.parse(' hello '); // 'hello'
|
|
191
|
+
* ```
|
|
192
|
+
*/
|
|
193
|
+
preprocess<T extends SchemaType>(callback: Function, schema: T): T;
|
|
194
|
+
/**
|
|
195
|
+
* Creates a union schema that validates against multiple schemas.
|
|
196
|
+
* The value must match at least one of the provided schemas.
|
|
197
|
+
*
|
|
198
|
+
* @param definitions - Array of schemas to validate against
|
|
199
|
+
* @param options - Optional configuration (name, message)
|
|
200
|
+
* @returns Union schema interface
|
|
201
|
+
*
|
|
202
|
+
* @example
|
|
203
|
+
* ```typescript
|
|
204
|
+
* const idSchema = s.union([s.string(), s.number()]);
|
|
205
|
+
* const result1 = idSchema.parse('abc'); // 'abc'
|
|
206
|
+
* const result2 = idSchema.parse(123); // 123
|
|
207
|
+
* ```
|
|
208
|
+
*/
|
|
209
|
+
union<T extends Array<SchemaType>>(definitions: T, options?: SchemaInterfaceOptions): UnionSchemaInterface<T>;
|
|
210
|
+
};
|
|
211
|
+
declare function hookOriginal<Input, Output>(object: SchemaInterface<Input, Output> | SchemaType, method: string, action: (original: Function, ...args: unknown[]) => InternalParseOutput<Output>): void;
|
|
212
|
+
/**
|
|
213
|
+
* Extends the schema system with custom validation methods.
|
|
214
|
+
* Used to add new methods to schema interfaces like StringSchemaInterface, NumberSchemaInterface, etc.
|
|
215
|
+
*
|
|
216
|
+
* @param callback - Function that receives schema, validation, and options, and can add new methods
|
|
217
|
+
*
|
|
218
|
+
* @example
|
|
219
|
+
* ```typescript
|
|
220
|
+
* import { extend, type StringSchemaInterface } from '@esmj/schema';
|
|
221
|
+
*
|
|
222
|
+
* // Extend StringSchemaInterface with email validation
|
|
223
|
+
* declare module '@esmj/schema' {
|
|
224
|
+
* interface StringSchemaInterface {
|
|
225
|
+
* email(): StringSchemaInterface;
|
|
226
|
+
* }
|
|
227
|
+
* }
|
|
228
|
+
*
|
|
229
|
+
* extend((schema, _, options) => {
|
|
230
|
+
* if (options?.type === 'string') {
|
|
231
|
+
* schema.email = function() {
|
|
232
|
+
* return this.refine(
|
|
233
|
+
* (val) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(val),
|
|
234
|
+
* { message: 'Invalid email address' }
|
|
235
|
+
* );
|
|
236
|
+
* };
|
|
237
|
+
* }
|
|
238
|
+
* return schema;
|
|
239
|
+
* });
|
|
240
|
+
*
|
|
241
|
+
* // Now you can use the email() method
|
|
242
|
+
* const emailSchema = s.string().email();
|
|
243
|
+
* ```
|
|
244
|
+
*/
|
|
245
|
+
declare function extend(callback: ExtenderType): void;
|
|
246
|
+
/**
|
|
247
|
+
* Type helper to infer the output type of a schema.
|
|
248
|
+
* Extracts the TypeScript type that a schema will produce after parsing.
|
|
249
|
+
*
|
|
250
|
+
* @template T - The schema type to infer from
|
|
251
|
+
*
|
|
252
|
+
* @example
|
|
253
|
+
* ```typescript
|
|
254
|
+
* const userSchema = s.object({
|
|
255
|
+
* name: s.string(),
|
|
256
|
+
* age: s.number(),
|
|
257
|
+
* email: s.string().optional()
|
|
258
|
+
* });
|
|
259
|
+
*
|
|
260
|
+
* type User = Infer<typeof userSchema>;
|
|
261
|
+
* // type User = {
|
|
262
|
+
* // name: string;
|
|
263
|
+
* // age: number;
|
|
264
|
+
* // email?: string;
|
|
265
|
+
* // }
|
|
266
|
+
* ```
|
|
267
|
+
*/
|
|
268
|
+
type Infer<T> = T extends SchemaType ? ReturnType<T['parse']> : unknown;
|
|
269
|
+
|
|
270
|
+
export { type ArraySchemaInterface, type BooleanSchemaInterface, type DateSchemaInterface, type EnumSchemaInterface, type ErrorStructure, type ExtenderType, type Infer, type Invalid, type NumberSchemaInterface, type ObjectSchemaInterface, type SchemaInterface, type SchemaInterfaceOptions, type SchemaType, type StringSchemaInterface, type UnionSchemaInterface, type Valid, extend, hookOriginal, s };
|
package/dist/index.d.ts
CHANGED
|
@@ -14,10 +14,12 @@ type Invalid = {
|
|
|
14
14
|
errors?: ErrorStructure[];
|
|
15
15
|
};
|
|
16
16
|
type InternalParseOutput<Output> = Valid<Output> | Invalid;
|
|
17
|
+
type ValidationMethod<Input, Output> = (value: Input | Partial<Input>) => boolean | InternalParseOutput<Output>;
|
|
17
18
|
interface ParseOptions {
|
|
18
19
|
abortEarly?: boolean;
|
|
19
20
|
}
|
|
20
21
|
interface SchemaInterface<Input, Output> {
|
|
22
|
+
_getName(): string;
|
|
21
23
|
_getType(): string;
|
|
22
24
|
_getDescription(): string;
|
|
23
25
|
_parse(value: Input | Partial<Input>, options?: ParseOptions): InternalParseOutput<Output>;
|
|
@@ -29,7 +31,7 @@ interface SchemaInterface<Input, Output> {
|
|
|
29
31
|
nullish(): SchemaInterface<Input, Output | undefined | null>;
|
|
30
32
|
default(defaultValue: Partial<Input> | (() => Partial<Input>) | Partial<Output>): SchemaInterface<Input, Output>;
|
|
31
33
|
pipe<NewOutput>(schema: SchemaInterface<Output, NewOutput>): SchemaInterface<Output, NewOutput>;
|
|
32
|
-
refine(validation:
|
|
34
|
+
refine(validation: ValidationMethod<Input, Output>, options?: CreateSchemaInterfaceOptions): SchemaInterface<Input, Output>;
|
|
33
35
|
}
|
|
34
36
|
interface UnionSchemaInterface<T extends Array<SchemaInterface<unknown, unknown>>> extends SchemaInterface<ReturnType<T[number]['parse']>, ReturnType<T[number]['parse']>> {
|
|
35
37
|
}
|
|
@@ -43,7 +45,7 @@ interface BooleanSchemaInterface extends SchemaInterface<boolean, boolean> {
|
|
|
43
45
|
}
|
|
44
46
|
interface DateSchemaInterface extends SchemaInterface<Date, Date> {
|
|
45
47
|
}
|
|
46
|
-
interface ArraySchemaInterface<T extends SchemaType> extends SchemaInterface<Array<T
|
|
48
|
+
interface ArraySchemaInterface<T extends SchemaType> extends SchemaInterface<Array<ReturnType<T['parse']>>, Array<ReturnType<T['parse']>>> {
|
|
47
49
|
}
|
|
48
50
|
interface ObjectSchemaInterface<T extends Record<string, SchemaType>> extends SchemaInterface<{
|
|
49
51
|
[Property in keyof T]: ReturnType<T[Property]['parse']>;
|
|
@@ -58,23 +60,211 @@ type ExtenderType = (inter: SchemaType, validation: Function, options?: {
|
|
|
58
60
|
type: string;
|
|
59
61
|
}) => SchemaType;
|
|
60
62
|
interface CreateSchemaInterfaceOptions {
|
|
63
|
+
name?: string;
|
|
61
64
|
type?: string;
|
|
62
65
|
message?: ErrorMessage;
|
|
63
66
|
}
|
|
64
67
|
type SchemaInterfaceOptions = Omit<CreateSchemaInterfaceOptions, 'type'>;
|
|
65
68
|
declare const s: {
|
|
69
|
+
/**
|
|
70
|
+
* Creates an object schema with validated fields.
|
|
71
|
+
*
|
|
72
|
+
* @param definition - Object containing field schemas
|
|
73
|
+
* @param options - Optional configuration (name, message)
|
|
74
|
+
* @returns Object schema interface
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```typescript
|
|
78
|
+
* const userSchema = s.object({
|
|
79
|
+
* name: s.string(),
|
|
80
|
+
* age: s.number()
|
|
81
|
+
* });
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
66
84
|
object<T extends Record<string, SchemaType>>(definition: { [Property in keyof T]: T[Property]; }, options?: SchemaInterfaceOptions): ObjectSchemaInterface<T>;
|
|
85
|
+
/**
|
|
86
|
+
* Creates a string schema.
|
|
87
|
+
*
|
|
88
|
+
* @param options - Optional configuration (name, message)
|
|
89
|
+
* @returns String schema interface
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```typescript
|
|
93
|
+
* const nameSchema = s.string();
|
|
94
|
+
* const result = nameSchema.parse('John'); // 'John'
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
67
97
|
string(options?: SchemaInterfaceOptions): StringSchemaInterface;
|
|
98
|
+
/**
|
|
99
|
+
* Creates a number schema.
|
|
100
|
+
*
|
|
101
|
+
* @param options - Optional configuration (name, message)
|
|
102
|
+
* @returns Number schema interface
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```typescript
|
|
106
|
+
* const ageSchema = s.number();
|
|
107
|
+
* const result = ageSchema.parse(25); // 25
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
68
110
|
number(options?: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
111
|
+
/**
|
|
112
|
+
* Creates a boolean schema.
|
|
113
|
+
*
|
|
114
|
+
* @param options - Optional configuration (name, message)
|
|
115
|
+
* @returns Boolean schema interface
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* const isActiveSchema = s.boolean();
|
|
120
|
+
* const result = isActiveSchema.parse(true); // true
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
69
123
|
boolean(options?: SchemaInterfaceOptions): BooleanSchemaInterface;
|
|
124
|
+
/**
|
|
125
|
+
* Creates a date schema.
|
|
126
|
+
*
|
|
127
|
+
* @param options - Optional configuration (name, message)
|
|
128
|
+
* @returns Date schema interface
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* ```typescript
|
|
132
|
+
* const birthdateSchema = s.date();
|
|
133
|
+
* const result = birthdateSchema.parse(new Date()); // Date object
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
70
136
|
date(options?: SchemaInterfaceOptions): DateSchemaInterface;
|
|
137
|
+
/**
|
|
138
|
+
* Creates an enum schema with predefined values.
|
|
139
|
+
*
|
|
140
|
+
* @param definition - Array of allowed string values
|
|
141
|
+
* @param options - Optional configuration (name, message)
|
|
142
|
+
* @returns Enum schema interface
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```typescript
|
|
146
|
+
* const roleSchema = s.enum(['admin', 'user', 'guest']);
|
|
147
|
+
* const result = roleSchema.parse('admin'); // 'admin'
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
71
150
|
enum(definition: Readonly<Array<string>>, options?: SchemaInterfaceOptions): EnumSchemaInterface<(typeof definition)[number]>;
|
|
151
|
+
/**
|
|
152
|
+
* Creates an array schema with element validation.
|
|
153
|
+
*
|
|
154
|
+
* @param definition - Schema for array elements
|
|
155
|
+
* @param options - Optional configuration (name, message)
|
|
156
|
+
* @returns Array schema interface
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```typescript
|
|
160
|
+
* const tagsSchema = s.array(s.string());
|
|
161
|
+
* const result = tagsSchema.parse(['tag1', 'tag2']); // ['tag1', 'tag2']
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
72
164
|
array<T extends SchemaType>(definition: T, options?: SchemaInterfaceOptions): ArraySchemaInterface<T>;
|
|
165
|
+
/**
|
|
166
|
+
* Creates a schema that accepts any value without validation.
|
|
167
|
+
*
|
|
168
|
+
* @returns Schema interface that accepts any value
|
|
169
|
+
*
|
|
170
|
+
* @example
|
|
171
|
+
* ```typescript
|
|
172
|
+
* const anySchema = s.any();
|
|
173
|
+
* const result = anySchema.parse({ anything: true }); // { anything: true }
|
|
174
|
+
* ```
|
|
175
|
+
*/
|
|
73
176
|
any(): any;
|
|
177
|
+
/**
|
|
178
|
+
* Preprocesses a value before passing it to a schema for validation.
|
|
179
|
+
*
|
|
180
|
+
* @param callback - Function to transform the value before validation
|
|
181
|
+
* @param schema - Schema to validate the transformed value
|
|
182
|
+
* @returns Modified schema with preprocessing
|
|
183
|
+
*
|
|
184
|
+
* @example
|
|
185
|
+
* ```typescript
|
|
186
|
+
* const schema = s.preprocess(
|
|
187
|
+
* (val) => String(val).trim(),
|
|
188
|
+
* s.string().min(3)
|
|
189
|
+
* );
|
|
190
|
+
* const result = schema.parse(' hello '); // 'hello'
|
|
191
|
+
* ```
|
|
192
|
+
*/
|
|
74
193
|
preprocess<T extends SchemaType>(callback: Function, schema: T): T;
|
|
194
|
+
/**
|
|
195
|
+
* Creates a union schema that validates against multiple schemas.
|
|
196
|
+
* The value must match at least one of the provided schemas.
|
|
197
|
+
*
|
|
198
|
+
* @param definitions - Array of schemas to validate against
|
|
199
|
+
* @param options - Optional configuration (name, message)
|
|
200
|
+
* @returns Union schema interface
|
|
201
|
+
*
|
|
202
|
+
* @example
|
|
203
|
+
* ```typescript
|
|
204
|
+
* const idSchema = s.union([s.string(), s.number()]);
|
|
205
|
+
* const result1 = idSchema.parse('abc'); // 'abc'
|
|
206
|
+
* const result2 = idSchema.parse(123); // 123
|
|
207
|
+
* ```
|
|
208
|
+
*/
|
|
75
209
|
union<T extends Array<SchemaType>>(definitions: T, options?: SchemaInterfaceOptions): UnionSchemaInterface<T>;
|
|
76
210
|
};
|
|
211
|
+
declare function hookOriginal<Input, Output>(object: SchemaInterface<Input, Output> | SchemaType, method: string, action: (original: Function, ...args: unknown[]) => InternalParseOutput<Output>): void;
|
|
212
|
+
/**
|
|
213
|
+
* Extends the schema system with custom validation methods.
|
|
214
|
+
* Used to add new methods to schema interfaces like StringSchemaInterface, NumberSchemaInterface, etc.
|
|
215
|
+
*
|
|
216
|
+
* @param callback - Function that receives schema, validation, and options, and can add new methods
|
|
217
|
+
*
|
|
218
|
+
* @example
|
|
219
|
+
* ```typescript
|
|
220
|
+
* import { extend, type StringSchemaInterface } from '@esmj/schema';
|
|
221
|
+
*
|
|
222
|
+
* // Extend StringSchemaInterface with email validation
|
|
223
|
+
* declare module '@esmj/schema' {
|
|
224
|
+
* interface StringSchemaInterface {
|
|
225
|
+
* email(): StringSchemaInterface;
|
|
226
|
+
* }
|
|
227
|
+
* }
|
|
228
|
+
*
|
|
229
|
+
* extend((schema, _, options) => {
|
|
230
|
+
* if (options?.type === 'string') {
|
|
231
|
+
* schema.email = function() {
|
|
232
|
+
* return this.refine(
|
|
233
|
+
* (val) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(val),
|
|
234
|
+
* { message: 'Invalid email address' }
|
|
235
|
+
* );
|
|
236
|
+
* };
|
|
237
|
+
* }
|
|
238
|
+
* return schema;
|
|
239
|
+
* });
|
|
240
|
+
*
|
|
241
|
+
* // Now you can use the email() method
|
|
242
|
+
* const emailSchema = s.string().email();
|
|
243
|
+
* ```
|
|
244
|
+
*/
|
|
77
245
|
declare function extend(callback: ExtenderType): void;
|
|
246
|
+
/**
|
|
247
|
+
* Type helper to infer the output type of a schema.
|
|
248
|
+
* Extracts the TypeScript type that a schema will produce after parsing.
|
|
249
|
+
*
|
|
250
|
+
* @template T - The schema type to infer from
|
|
251
|
+
*
|
|
252
|
+
* @example
|
|
253
|
+
* ```typescript
|
|
254
|
+
* const userSchema = s.object({
|
|
255
|
+
* name: s.string(),
|
|
256
|
+
* age: s.number(),
|
|
257
|
+
* email: s.string().optional()
|
|
258
|
+
* });
|
|
259
|
+
*
|
|
260
|
+
* type User = Infer<typeof userSchema>;
|
|
261
|
+
* // type User = {
|
|
262
|
+
* // name: string;
|
|
263
|
+
* // age: number;
|
|
264
|
+
* // email?: string;
|
|
265
|
+
* // }
|
|
266
|
+
* ```
|
|
267
|
+
*/
|
|
78
268
|
type Infer<T> = T extends SchemaType ? ReturnType<T['parse']> : unknown;
|
|
79
269
|
|
|
80
|
-
export { type ArraySchemaInterface, type BooleanSchemaInterface, type DateSchemaInterface, type EnumSchemaInterface, type ExtenderType, type Infer, type NumberSchemaInterface, type ObjectSchemaInterface, type SchemaInterface, type SchemaInterfaceOptions, type SchemaType, type StringSchemaInterface, type UnionSchemaInterface, extend, s };
|
|
270
|
+
export { type ArraySchemaInterface, type BooleanSchemaInterface, type DateSchemaInterface, type EnumSchemaInterface, type ErrorStructure, type ExtenderType, type Infer, type Invalid, type NumberSchemaInterface, type ObjectSchemaInterface, type SchemaInterface, type SchemaInterfaceOptions, type SchemaType, type StringSchemaInterface, type UnionSchemaInterface, type Valid, extend, hookOriginal, s };
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
export{c as extend,b as hookOriginal,a as s}from'./chunk-NCHMO7B3.mjs';
|