@esmj/schema 0.7.2 → 0.7.3
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 +22 -0
- package/dist/array.cjs +1 -1
- package/dist/array.d.mts +1 -1
- package/dist/array.d.ts +1 -1
- package/dist/array.mjs +1 -1
- package/dist/chunk-CLEFU3BA.mjs +1 -0
- package/dist/chunk-QQOSS5DZ.mjs +1 -0
- package/dist/chunk-YUL3WJPU.mjs +1 -0
- package/dist/chunk-YYPP27RX.mjs +1 -0
- package/dist/full.cjs +1 -1
- package/dist/full.d.mts +1 -1
- package/dist/full.d.ts +1 -1
- package/dist/full.mjs +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.mts +20 -4
- package/dist/index.d.ts +20 -4
- package/dist/index.mjs +1 -1
- package/dist/number.cjs +1 -1
- package/dist/number.d.mts +1 -1
- package/dist/number.d.ts +1 -1
- package/dist/number.mjs +1 -1
- package/dist/string.cjs +1 -1
- package/dist/string.d.mts +1 -1
- package/dist/string.d.ts +1 -1
- package/dist/string.mjs +1 -1
- package/package.json +2 -2
- package/dist/chunk-3C2I2ZOZ.mjs +0 -1
- package/dist/chunk-G6KRMWVT.mjs +0 -1
- package/dist/chunk-GJNOESJT.mjs +0 -1
- package/dist/chunk-QIW3OXV4.mjs +0 -1
package/README.md
CHANGED
|
@@ -426,6 +426,7 @@ const schema = s.object({
|
|
|
426
426
|
- `s.number()` - Number validation
|
|
427
427
|
- `s.boolean()` - Boolean validation
|
|
428
428
|
- `s.date()` - Date validation
|
|
429
|
+
- `s.function()` - Function validation
|
|
429
430
|
- `s.object(def)` - Object validation
|
|
430
431
|
- `s.array(def)` - Array validation
|
|
431
432
|
- `s.literal(value)` - Literal value validation
|
|
@@ -590,6 +591,27 @@ const dateSchemaFunc = s.date({
|
|
|
590
591
|
});
|
|
591
592
|
```
|
|
592
593
|
|
|
594
|
+
#### `s.function(options?)`
|
|
595
|
+
|
|
596
|
+
Creates a function schema that validates the value is callable.
|
|
597
|
+
|
|
598
|
+
- **`message`**: Can be either a constant string or a function `(value) => string`.
|
|
599
|
+
|
|
600
|
+
```typescript
|
|
601
|
+
const callbackSchema = s.function();
|
|
602
|
+
callbackSchema.parse(() => {}); // () => {}
|
|
603
|
+
callbackSchema.parse(async () => {}); // async () => {}
|
|
604
|
+
callbackSchema.parse('hello'); // throws
|
|
605
|
+
|
|
606
|
+
const callbackSchemaMsg = s.function({
|
|
607
|
+
message: 'Expected a callback function.',
|
|
608
|
+
});
|
|
609
|
+
|
|
610
|
+
const callbackSchemaFunc = s.function({
|
|
611
|
+
message: (value) => `Custom error: "${value}" is not a function.`,
|
|
612
|
+
});
|
|
613
|
+
```
|
|
614
|
+
|
|
593
615
|
#### `s.object(definition, options?)`
|
|
594
616
|
|
|
595
617
|
Creates an object schema with the given definition. You can optionally pass `options` to customize error messages.
|
package/dist/array.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var E={abortEarly:true};function b(e,a){if(!a)return e;let n=e?.cause?.key?`${a}.${e.cause.key}`:`${a}`;return {message:`Error parsing key "${n}": ${e.message}`,cause:{key:n}}}function
|
|
1
|
+
'use strict';var E={abortEarly:true};function b(e,a){if(!a)return e;let n=e?.cause?.key?`${a}.${e.cause.key}`:`${a}`;return {message:`Error parsing key "${n}": ${e.message}`,cause:{key:n}}}function w(e,a,n){if(e.errors?.length)for(let r=0;r<e.errors.length;r++)a.push(b(e.errors[r],n));}function T(e){return {...E,...e}}var y=e=>typeof e=="string"||e instanceof String,d=e=>(typeof e=="number"||e instanceof Number)&&!Number.isNaN(e),g=e=>e===true||e===false,$=e=>e instanceof Date&&!Number.isNaN(e.getTime()),_=e=>typeof e=="function",D=e=>Array.isArray(e),x=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),m={object(e,a){let n=S(x,{...a,type:"object"});return n._getDescription=()=>`object({ ${Object.entries(e).map(([u,c])=>`${u}: ${c._getDescription()}`).join(", ")} })`,h(n,"_parse",(r,u,c)=>{let t=r(u,c),{abortEarly:s}=T(c);if(t.success===false)return t;let o={},i=[];for(let p in e){let f=e[p]._parse(t.data[p],c);if(f.success)o[p]=f.data;else {if(f=f,s!==false){let I=b(f.error,p);return {success:false,error:I,errors:[I]}}w(f,i,p);}}return i.length>0?{success:false,error:i[0],errors:i}:{success:true,data:o}}),n},string(e){return S(y,{...e,type:"string"})},number(e){return S(d,{...e,type:"number"})},boolean(e){return S(g,{...e,type:"boolean"})},date(e){return S($,{...e,type:"date"})},function(e){return S(_,{...e,type:"function"})},enum(e,a){let n=t=>e.includes(t),r=t=>`Invalid ${u} value. Expected ${e.map(s=>`"${s}"`).join(" | ")}, received "${t}".`,u="enum",c=S(n,{message:r,...a,type:u});return c._getDescription=()=>`enum(${e.map(t=>`"${t}"`).join(" | ")})`,c},array(e,a){let n=S(D,{...a,type:"array"});return n._getDescription=()=>`array(${e._getDescription()})`,h(n,"_parse",(r,u,c)=>{let t=r(u,c),{abortEarly:s}=T(c);if(t.success===false)return t;let o=[],i=[];for(let p=0;p<t.data.length;p++){let f=e._parse(t.data[p],c);if(f.success)o.push(f.data);else {if(f=f,s!==false){let I=b(f.error,p);return {success:false,error:I,errors:[I]}}w(f,i,p);}}return i.length>0?{success:false,error:i[0],errors:i}:{success:true,data:o}}),n},any(){return S(()=>true)},preprocess(e,a){return h(a,"_parse",(n,r)=>(r=e(r),n(r))),a},union(e,a){return S(c=>{for(let t=0;t<e.length;t++){let s=e[t]._parse(c);if(s.success)return s}return false},{message:c=>`Invalid union value. Expected the value to match one of the schemas:${e.map((t,s)=>` ${s+1}. ${t._getDescription()}`).join(",")} but received "${typeof c}" with value: ${x(c)?JSON.stringify(c):`"${c}"`}`,...a,type:"union"})},literal(e,a){let u=S(c=>c===e,{message:c=>a?.message?typeof a.message=="function"?a.message(c):a.message:`Expected literal value "${e}", received "${c}"`,name:a?.name,type:"literal"});return u._getDescription=()=>`literal("${e}")`,u},coerce:{string(e){return m.preprocess(a=>String(a),m.string(e))},number(e){let a=e?.message??(n=>`Cannot coerce "${n}" to a valid number.`);return m.preprocess(n=>Number(n),m.number({...e,message:a}))},boolean(e){return m.preprocess(a=>!!a,m.boolean(e))},date(e){let a=e?.message??(n=>`Cannot coerce "${n}" to a valid date.`);return m.preprocess(n=>new Date(n),m.date({...e,message:a}))}},cast:{boolean(e){let a=e?.message??(n=>`Cannot cast "${n}" to boolean. Accepted: true/false, 1/0, yes/no, on/off.`);return m.preprocess(n=>{let r;return y(n)&&(r=n.toLowerCase()),r==="true"||r==="yes"||r==="on"||r==="1"||n===1?true:r==="false"||r==="no"||r==="off"||r==="0"||n===0?false:n},m.boolean({...e,message:a}))},number(e){let a=e?.message??(n=>`Cannot cast "${n}" to a number. Expected a numeric string or number.`);return m.preprocess(n=>{if(g(n))return Number(n);if(y(n)){let r=n.trim();if(r==="")return n;let u=Number(r);if(Number.isFinite(u))return u}return n},m.number({...e,message:a}))},string(e){let a=e?.message??(n=>`Cannot cast "${n}" to string. Expected a string, number, or boolean.`);return m.preprocess(n=>g(n)||d(n)&&Number.isFinite(n)?String(n):n,m.string({...e,message:a}))},date(e){let a=e?.message??(n=>`Cannot cast "${n}" to a valid date.`);return m.preprocess(n=>{let r;return y(n)&&(r=n.trim()),d(n)&&Number.isFinite(n)||r?new Date(r??n):n},m.date({...e,message:a}))},json(e,a){let n=a?.message??(r=>`Cannot parse "${r}" as JSON.`);return h(e,"_parse",(r,u)=>{if(y(u))try{u=JSON.parse(u);}catch{let c={message:typeof n=="function"?n(u):n};return {success:false,error:c,errors:[c]}}return r(u)}),e}}};function A(e){return a=>`The value "${a}" must be type of ${e} but is type of "${typeof a}".`}function h(e,a,n){let r=e[a];e[a]=(...u)=>n(r,...u);}function S(e,{type:a="any",name:n,message:r}={}){r=r||A(a);let u={name:n,message:r,type:a},c={_getName(){return n},_getType(){return a},_getDescription(){return this._getName()??this._getType()},_parse(t,s){let o=e(t);if(o===true)return {success:true,data:t};if(typeof o=="object"&&o?.success===true)return o;let i={message:typeof r=="function"?r(t):r};return {success:false,error:i,errors:[i]}},parse(t,s){let o=c._parse(t,s);if(!o.success)throw o=o,new Error(o.error.message,{cause:o.error.cause});return o.data},safeParse(t,s){return c._parse(t,s)},transform(t){return h(this,"_parse",(s,o,i)=>{let p=s(o,i);return p.success&&(p.data=t(p.data)),p}),this},optional(){return h(this,"_parse",(t,s,o)=>{let i=t(s,o);return !i.success&&s===void 0&&(i.data=void 0,i.success=true),i}),this},nullable(){return h(this,"_parse",(t,s,o)=>{let i=t(s,o);return !i.success&&s===null&&(i.data=null,i.success=true),i}),this},nullish(){return h(this,"_parse",(t,s,o)=>{let i=t(s,o);return !i.success&&s==null&&(i.success=true,i.data=s),i}),this},default(t){return h(this,"_parse",(s,o,i)=>(o===void 0&&(o=typeof t=="function"?t():t),s(o,i))),this},catch(t){return h(this,"_parse",(s,o,i)=>{let p=s(o,i);return p.success?p:{success:true,data:typeof t=="function"?t({input:o,error:p.error}):t}}),this},pipe(t){return h(this,"_parse",(s,o,i)=>{let p=s(o,i);return p.success?t._parse(p.data,i):p}),this},refine(t,{message:s,type:o}={}){return o&&(s=s||A(o),a=o),h(this,"_parse",(i,p,f)=>{let I=i(p,f);T(f);if(!I.success)return I;let l=t(I.data);if(l===true||typeof l=="object"&&l?.success===true)return I;let k={message:typeof s=="function"?s(p):s};return {success:false,error:k,errors:[k]}}),this}};return O.length>0?O.reduce((t,s)=>s(t,e,u)??t,c):c}var O=[];function P(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");O.push(e);}P((e,a,n)=>{if(n?.type==="array"){let r=e;r.min=function(u,{message:c}={}){return this.refine(t=>t.length>=u,{message:c||`Array must contain at least ${u} items.`})},r.max=function(u,{message:c}={}){return this.refine(t=>t.length<=u,{message:c||`Array must contain at most ${u} items.`})},r.length=function(u,{message:c}={}){return this.refine(t=>t.length===u,{message:c||`Array must contain exactly ${u} items.`})},r.nonEmpty=function({message:u}={}){return this.refine(c=>c.length>0,{message:u||"Array must not be empty."})},r.unique=function({message:u}={}){return this.refine(c=>{let t=new Set;try{return c.every(s=>{let o=JSON.stringify(s);return t.has(o)?!1:(t.add(o),!0)})}catch{return new Set(c).size===c.length}},{message:u||"Array items must be unique."})},r.sort=function(){return this.transform(u=>[...u].sort())},r.reverse=function(){return this.transform(u=>[...u].reverse())};}return e});exports.extend=P;exports.hookOriginal=h;exports.s=m;exports.schema=m;
|
package/dist/array.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s, s as schema } from './index.mjs';
|
|
1
|
+
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s, s as schema } from './index.mjs';
|
|
2
2
|
|
|
3
3
|
declare module './index.ts' {
|
|
4
4
|
interface ArraySchemaInterface<T extends SchemaType> {
|
package/dist/array.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s, s as schema } from './index.js';
|
|
1
|
+
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s, s as schema } from './index.js';
|
|
2
2
|
|
|
3
3
|
declare module './index.ts' {
|
|
4
4
|
interface ArraySchemaInterface<T extends SchemaType> {
|
package/dist/array.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import'./chunk-
|
|
1
|
+
import'./chunk-YYPP27RX.mjs';export{c as extend,b as hookOriginal,a as s,a as schema}from'./chunk-QQOSS5DZ.mjs';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import {c}from'./chunk-QQOSS5DZ.mjs';c((r,c,i)=>{if(i?.type==="number"){let t=r;t.min=function(e,{message:n}={}){return this.refine(a=>a>=e,{message:n||`Number must be greater than or equal to ${e}.`})},t.max=function(e,{message:n}={}){return this.refine(a=>a<=e,{message:n||`Number must be less than or equal to ${e}.`})},t.positive=function({message:e}={}){return this.refine(n=>n>0,{message:e||"Number must be positive."})},t.negative=function({message:e}={}){return this.refine(n=>n<0,{message:e||"Number must be negative."})},t.int=function({message:e}={}){return this.refine(n=>Number.isInteger(n),{message:e||"Number must be an integer."})},t.float=function({message:e}={}){return this.refine(n=>Number.isFinite(n)&&!Number.isInteger(n),{message:e||"Number must be a floating point (non-integer)."})},t.multipleOf=function(e,{message:n}={}){return this.refine(a=>a%e===0,{message:n||`Number must be a multiple of ${e}.`})},t.finite=function({message:e}={}){return this.refine(n=>Number.isFinite(n),{message:e||"Number must be finite."})};}return r});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var E={abortEarly:true};function b(e,t){if(!t)return e;let n=e?.cause?.key?`${t}.${e.cause.key}`:`${t}`;return {message:`Error parsing key "${n}": ${e.message}`,cause:{key:n}}}function w(e,t,n){if(e.errors?.length)for(let a=0;a<e.errors.length;a++)t.push(b(e.errors[a],n));}function T(e){return {...E,...e}}var S=e=>typeof e=="string"||e instanceof String,y=e=>(typeof e=="number"||e instanceof Number)&&!Number.isNaN(e),g=e=>e===true||e===false,D=e=>e instanceof Date&&!Number.isNaN(e.getTime()),_=e=>typeof e=="function",$=e=>Array.isArray(e),x=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),m={object(e,t){let n=I(x,{...t,type:"object"});return n._getDescription=()=>`object({ ${Object.entries(e).map(([p,o])=>`${p}: ${o._getDescription()}`).join(", ")} })`,h(n,"_parse",(a,p,o)=>{let r=a(p,o),{abortEarly:s}=T(o);if(r.success===false)return r;let u={},c=[];for(let i in e){let f=e[i]._parse(r.data[i],o);if(f.success)u[i]=f.data;else {if(f=f,s!==false){let l=b(f.error,i);return {success:false,error:l,errors:[l]}}w(f,c,i);}}return c.length>0?{success:false,error:c[0],errors:c}:{success:true,data:u}}),n},string(e){return I(S,{...e,type:"string"})},number(e){return I(y,{...e,type:"number"})},boolean(e){return I(g,{...e,type:"boolean"})},date(e){return I(D,{...e,type:"date"})},function(e){return I(_,{...e,type:"function"})},enum(e,t){let n=r=>e.includes(r),a=r=>`Invalid ${p} value. Expected ${e.map(s=>`"${s}"`).join(" | ")}, received "${r}".`,p="enum",o=I(n,{message:a,...t,type:p});return o._getDescription=()=>`enum(${e.map(r=>`"${r}"`).join(" | ")})`,o},array(e,t){let n=I($,{...t,type:"array"});return n._getDescription=()=>`array(${e._getDescription()})`,h(n,"_parse",(a,p,o)=>{let r=a(p,o),{abortEarly:s}=T(o);if(r.success===false)return r;let u=[],c=[];for(let i=0;i<r.data.length;i++){let f=e._parse(r.data[i],o);if(f.success)u.push(f.data);else {if(f=f,s!==false){let l=b(f.error,i);return {success:false,error:l,errors:[l]}}w(f,c,i);}}return c.length>0?{success:false,error:c[0],errors:c}:{success:true,data:u}}),n},any(){return I(()=>true)},preprocess(e,t){return h(t,"_parse",(n,a)=>(a=e(a),n(a))),t},union(e,t){return I(o=>{for(let r=0;r<e.length;r++){let s=e[r]._parse(o);if(s.success)return s}return false},{message:o=>`Invalid union value. Expected the value to match one of the schemas:${e.map((r,s)=>` ${s+1}. ${r._getDescription()}`).join(",")} but received "${typeof o}" with value: ${x(o)?JSON.stringify(o):`"${o}"`}`,...t,type:"union"})},literal(e,t){let p=I(o=>o===e,{message:o=>t?.message?typeof t.message=="function"?t.message(o):t.message:`Expected literal value "${e}", received "${o}"`,name:t?.name,type:"literal"});return p._getDescription=()=>`literal("${e}")`,p},coerce:{string(e){return m.preprocess(t=>String(t),m.string(e))},number(e){let t=e?.message??(n=>`Cannot coerce "${n}" to a valid number.`);return m.preprocess(n=>Number(n),m.number({...e,message:t}))},boolean(e){return m.preprocess(t=>!!t,m.boolean(e))},date(e){let t=e?.message??(n=>`Cannot coerce "${n}" to a valid date.`);return m.preprocess(n=>new Date(n),m.date({...e,message:t}))}},cast:{boolean(e){let t=e?.message??(n=>`Cannot cast "${n}" to boolean. Accepted: true/false, 1/0, yes/no, on/off.`);return m.preprocess(n=>{let a;return S(n)&&(a=n.toLowerCase()),a==="true"||a==="yes"||a==="on"||a==="1"||n===1?true:a==="false"||a==="no"||a==="off"||a==="0"||n===0?false:n},m.boolean({...e,message:t}))},number(e){let t=e?.message??(n=>`Cannot cast "${n}" to a number. Expected a numeric string or number.`);return m.preprocess(n=>{if(g(n))return Number(n);if(S(n)){let a=n.trim();if(a==="")return n;let p=Number(a);if(Number.isFinite(p))return p}return n},m.number({...e,message:t}))},string(e){let t=e?.message??(n=>`Cannot cast "${n}" to string. Expected a string, number, or boolean.`);return m.preprocess(n=>g(n)||y(n)&&Number.isFinite(n)?String(n):n,m.string({...e,message:t}))},date(e){let t=e?.message??(n=>`Cannot cast "${n}" to a valid date.`);return m.preprocess(n=>{let a;return S(n)&&(a=n.trim()),y(n)&&Number.isFinite(n)||a?new Date(a??n):n},m.date({...e,message:t}))},json(e,t){let n=t?.message??(a=>`Cannot parse "${a}" as JSON.`);return h(e,"_parse",(a,p)=>{if(S(p))try{p=JSON.parse(p);}catch{let o={message:typeof n=="function"?n(p):n};return {success:false,error:o,errors:[o]}}return a(p)}),e}}};function P(e){return t=>`The value "${t}" must be type of ${e} but is type of "${typeof t}".`}function h(e,t,n){let a=e[t];e[t]=(...p)=>n(a,...p);}function I(e,{type:t="any",name:n,message:a}={}){a=a||P(t);let p={name:n,message:a,type:t},o={_getName(){return n},_getType(){return t},_getDescription(){return this._getName()??this._getType()},_parse(r,s){let u=e(r);if(u===true)return {success:true,data:r};if(typeof u=="object"&&u?.success===true)return u;let c={message:typeof a=="function"?a(r):a};return {success:false,error:c,errors:[c]}},parse(r,s){let u=o._parse(r,s);if(!u.success)throw u=u,new Error(u.error.message,{cause:u.error.cause});return u.data},safeParse(r,s){return o._parse(r,s)},transform(r){return h(this,"_parse",(s,u,c)=>{let i=s(u,c);return i.success&&(i.data=r(i.data)),i}),this},optional(){return h(this,"_parse",(r,s,u)=>{let c=r(s,u);return !c.success&&s===void 0&&(c.data=void 0,c.success=true),c}),this},nullable(){return h(this,"_parse",(r,s,u)=>{let c=r(s,u);return !c.success&&s===null&&(c.data=null,c.success=true),c}),this},nullish(){return h(this,"_parse",(r,s,u)=>{let c=r(s,u);return !c.success&&s==null&&(c.success=true,c.data=s),c}),this},default(r){return h(this,"_parse",(s,u,c)=>(u===void 0&&(u=typeof r=="function"?r():r),s(u,c))),this},catch(r){return h(this,"_parse",(s,u,c)=>{let i=s(u,c);return i.success?i:{success:true,data:typeof r=="function"?r({input:u,error:i.error}):r}}),this},pipe(r){return h(this,"_parse",(s,u,c)=>{let i=s(u,c);return i.success?r._parse(i.data,c):i}),this},refine(r,{message:s,type:u}={}){return u&&(s=s||P(u),t=u),h(this,"_parse",(c,i,f)=>{let l=c(i,f);T(f);if(!l.success)return l;let d=r(l.data);if(d===true||typeof d=="object"&&d?.success===true)return l;let k={message:typeof s=="function"?s(i):s};return {success:false,error:k,errors:[k]}}),this}};return O.length>0?O.reduce((r,s)=>s(r,e,p)??r,o):o}var O=[];function A(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");O.push(e);}export{m as a,h as b,A as c};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import {c}from'./chunk-QQOSS5DZ.mjs';c((a,s,i)=>{if(i?.type==="string"){let r=a;r.min=function(n,{message:t}={}){return this.refine(e=>e.length>=n,{message:t||(e=>`String must be at least ${n} characters long (received ${e.length} characters: "${e}")`)})},r.max=function(n,{message:t}={}){return this.refine(e=>e.length<=n,{message:t||(e=>`String must be at most ${n} characters long (received ${e.length} characters: "${e}")`)})},r.length=function(n,{message:t}={}){return this.refine(e=>e.length===n,{message:t||(e=>`String must be exactly ${n} characters long (received ${e.length} characters: "${e}")`)})},r.nonEmpty=function({message:n}={}){return this.refine(t=>t.length>0,{message:n||"String must not be empty (received empty string)"})},r.startsWith=function(n,{message:t}={}){return this.refine(e=>e.startsWith(n),{message:t||(e=>`String must start with "${n}" (received: "${e}")`)})},r.endsWith=function(n,{message:t}={}){return this.refine(e=>e.endsWith(n),{message:t||(e=>`String must end with "${n}" (received: "${e}")`)})},r.includes=function(n,{message:t}={}){return this.refine(e=>e.includes(n),{message:t||(e=>`String must include "${n}" (received: "${e}")`)})},r.toLowerCase=function(){return this.transform(n=>n.toLowerCase())},r.toUpperCase=function(){return this.transform(n=>n.toUpperCase())},r.trim=function(){return this.transform(n=>n.trim())},r.padStart=function(n,t=" "){return this.transform(e=>e.padStart(n,t))},r.padEnd=function(n,t=" "){return this.transform(e=>e.padEnd(n,t))},r.replace=function(n,t){return this.transform(e=>e.replace(n,t))};}return a});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import {c}from'./chunk-QQOSS5DZ.mjs';c((t,h,i)=>{if(i?.type==="array"){let r=t;r.min=function(e,{message:n}={}){return this.refine(a=>a.length>=e,{message:n||`Array must contain at least ${e} items.`})},r.max=function(e,{message:n}={}){return this.refine(a=>a.length<=e,{message:n||`Array must contain at most ${e} items.`})},r.length=function(e,{message:n}={}){return this.refine(a=>a.length===e,{message:n||`Array must contain exactly ${e} items.`})},r.nonEmpty=function({message:e}={}){return this.refine(n=>n.length>0,{message:e||"Array must not be empty."})},r.unique=function({message:e}={}){return this.refine(n=>{let a=new Set;try{return n.every(c=>{let s=JSON.stringify(c);return a.has(s)?!1:(a.add(s),!0)})}catch{return new Set(n).size===n.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 t});
|
package/dist/full.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var
|
|
1
|
+
'use strict';var A={abortEarly:true};function O(e,c){if(!c)return e;let s=e?.cause?.key?`${c}.${e.cause.key}`:`${c}`;return {message:`Error parsing key "${s}": ${e.message}`,cause:{key:s}}}function x(e,c,s){if(e.errors?.length)for(let a=0;a<e.errors.length;a++)c.push(O(e.errors[a],s));}function T(e){return {...A,...e}}var l=e=>typeof e=="string"||e instanceof String,d=e=>(typeof e=="number"||e instanceof Number)&&!Number.isNaN(e),b=e=>e===true||e===false,P=e=>e instanceof Date&&!Number.isNaN(e.getTime()),E=e=>typeof e=="function",_=e=>Array.isArray(e),N=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),p={object(e,c){let s=S(N,{...c,type:"object"});return s._getDescription=()=>`object({ ${Object.entries(e).map(([t,r])=>`${t}: ${r._getDescription()}`).join(", ")} })`,h(s,"_parse",(a,t,r)=>{let n=a(t,r),{abortEarly:o}=T(r);if(n.success===false)return n;let u={},i=[];for(let m in e){let f=e[m]._parse(n.data[m],r);if(f.success)u[m]=f.data;else {if(f=f,o!==false){let I=O(f.error,m);return {success:false,error:I,errors:[I]}}x(f,i,m);}}return i.length>0?{success:false,error:i[0],errors:i}:{success:true,data:u}}),s},string(e){return S(l,{...e,type:"string"})},number(e){return S(d,{...e,type:"number"})},boolean(e){return S(b,{...e,type:"boolean"})},date(e){return S(P,{...e,type:"date"})},function(e){return S(E,{...e,type:"function"})},enum(e,c){let s=n=>e.includes(n),a=n=>`Invalid ${t} value. Expected ${e.map(o=>`"${o}"`).join(" | ")}, received "${n}".`,t="enum",r=S(s,{message:a,...c,type:t});return r._getDescription=()=>`enum(${e.map(n=>`"${n}"`).join(" | ")})`,r},array(e,c){let s=S(_,{...c,type:"array"});return s._getDescription=()=>`array(${e._getDescription()})`,h(s,"_parse",(a,t,r)=>{let n=a(t,r),{abortEarly:o}=T(r);if(n.success===false)return n;let u=[],i=[];for(let m=0;m<n.data.length;m++){let f=e._parse(n.data[m],r);if(f.success)u.push(f.data);else {if(f=f,o!==false){let I=O(f.error,m);return {success:false,error:I,errors:[I]}}x(f,i,m);}}return i.length>0?{success:false,error:i[0],errors:i}:{success:true,data:u}}),s},any(){return S(()=>true)},preprocess(e,c){return h(c,"_parse",(s,a)=>(a=e(a),s(a))),c},union(e,c){return S(r=>{for(let n=0;n<e.length;n++){let o=e[n]._parse(r);if(o.success)return o}return false},{message:r=>`Invalid union value. Expected the value to match one of the schemas:${e.map((n,o)=>` ${o+1}. ${n._getDescription()}`).join(",")} but received "${typeof r}" with value: ${N(r)?JSON.stringify(r):`"${r}"`}`,...c,type:"union"})},literal(e,c){let t=S(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 t._getDescription=()=>`literal("${e}")`,t},coerce:{string(e){return p.preprocess(c=>String(c),p.string(e))},number(e){let c=e?.message??(s=>`Cannot coerce "${s}" to a valid number.`);return p.preprocess(s=>Number(s),p.number({...e,message:c}))},boolean(e){return p.preprocess(c=>!!c,p.boolean(e))},date(e){let c=e?.message??(s=>`Cannot coerce "${s}" to a valid date.`);return p.preprocess(s=>new Date(s),p.date({...e,message:c}))}},cast:{boolean(e){let c=e?.message??(s=>`Cannot cast "${s}" to boolean. Accepted: true/false, 1/0, yes/no, on/off.`);return p.preprocess(s=>{let a;return l(s)&&(a=s.toLowerCase()),a==="true"||a==="yes"||a==="on"||a==="1"||s===1?true:a==="false"||a==="no"||a==="off"||a==="0"||s===0?false:s},p.boolean({...e,message:c}))},number(e){let c=e?.message??(s=>`Cannot cast "${s}" to a number. Expected a numeric string or number.`);return p.preprocess(s=>{if(b(s))return Number(s);if(l(s)){let a=s.trim();if(a==="")return s;let t=Number(a);if(Number.isFinite(t))return t}return s},p.number({...e,message:c}))},string(e){let c=e?.message??(s=>`Cannot cast "${s}" to string. Expected a string, number, or boolean.`);return p.preprocess(s=>b(s)||d(s)&&Number.isFinite(s)?String(s):s,p.string({...e,message:c}))},date(e){let c=e?.message??(s=>`Cannot cast "${s}" to a valid date.`);return p.preprocess(s=>{let a;return l(s)&&(a=s.trim()),d(s)&&Number.isFinite(s)||a?new Date(a??s):s},p.date({...e,message:c}))},json(e,c){let s=c?.message??(a=>`Cannot parse "${a}" as JSON.`);return h(e,"_parse",(a,t)=>{if(l(t))try{t=JSON.parse(t);}catch{let r={message:typeof s=="function"?s(t):s};return {success:false,error:r,errors:[r]}}return a(t)}),e}}};function $(e){return c=>`The value "${c}" must be type of ${e} but is type of "${typeof c}".`}function h(e,c,s){let a=e[c];e[c]=(...t)=>s(a,...t);}function S(e,{type:c="any",name:s,message:a}={}){a=a||$(c);let t={name:s,message:a,type:c},r={_getName(){return s},_getType(){return c},_getDescription(){return this._getName()??this._getType()},_parse(n,o){let u=e(n);if(u===true)return {success:true,data:n};if(typeof u=="object"&&u?.success===true)return u;let i={message:typeof a=="function"?a(n):a};return {success:false,error:i,errors:[i]}},parse(n,o){let u=r._parse(n,o);if(!u.success)throw u=u,new Error(u.error.message,{cause:u.error.cause});return u.data},safeParse(n,o){return r._parse(n,o)},transform(n){return h(this,"_parse",(o,u,i)=>{let m=o(u,i);return m.success&&(m.data=n(m.data)),m}),this},optional(){return h(this,"_parse",(n,o,u)=>{let i=n(o,u);return !i.success&&o===void 0&&(i.data=void 0,i.success=true),i}),this},nullable(){return h(this,"_parse",(n,o,u)=>{let i=n(o,u);return !i.success&&o===null&&(i.data=null,i.success=true),i}),this},nullish(){return h(this,"_parse",(n,o,u)=>{let i=n(o,u);return !i.success&&o==null&&(i.success=true,i.data=o),i}),this},default(n){return h(this,"_parse",(o,u,i)=>(u===void 0&&(u=typeof n=="function"?n():n),o(u,i))),this},catch(n){return h(this,"_parse",(o,u,i)=>{let m=o(u,i);return m.success?m:{success:true,data:typeof n=="function"?n({input:u,error:m.error}):n}}),this},pipe(n){return h(this,"_parse",(o,u,i)=>{let m=o(u,i);return m.success?n._parse(m.data,i):m}),this},refine(n,{message:o,type:u}={}){return u&&(o=o||$(u),c=u),h(this,"_parse",(i,m,f)=>{let I=i(m,f);T(f);if(!I.success)return I;let y=n(I.data);if(y===true||typeof y=="object"&&y?.success===true)return I;let k={message:typeof o=="function"?o(m):o};return {success:false,error:k,errors:[k]}}),this}};return w.length>0?w.reduce((n,o)=>o(n,e,t)??n,r):r}var w=[];function g(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");w.push(e);}g((e,c,s)=>{if(s?.type==="string"){let a=e;a.min=function(t,{message:r}={}){return this.refine(n=>n.length>=t,{message:r||(n=>`String must be at least ${t} characters long (received ${n.length} characters: "${n}")`)})},a.max=function(t,{message:r}={}){return this.refine(n=>n.length<=t,{message:r||(n=>`String must be at most ${t} characters long (received ${n.length} characters: "${n}")`)})},a.length=function(t,{message:r}={}){return this.refine(n=>n.length===t,{message:r||(n=>`String must be exactly ${t} characters long (received ${n.length} characters: "${n}")`)})},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(n=>n.startsWith(t),{message:r||(n=>`String must start with "${t}" (received: "${n}")`)})},a.endsWith=function(t,{message:r}={}){return this.refine(n=>n.endsWith(t),{message:r||(n=>`String must end with "${t}" (received: "${n}")`)})},a.includes=function(t,{message:r}={}){return this.refine(n=>n.includes(t),{message:r||(n=>`String must include "${t}" (received: "${n}")`)})},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(n=>n.padStart(t,r))},a.padEnd=function(t,r=" "){return this.transform(n=>n.padEnd(t,r))},a.replace=function(t,r){return this.transform(n=>n.replace(t,r))};}return e});g((e,c,s)=>{if(s?.type==="number"){let a=e;a.min=function(t,{message:r}={}){return this.refine(n=>n>=t,{message:r||`Number must be greater than or equal to ${t}.`})},a.max=function(t,{message:r}={}){return this.refine(n=>n<=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(n=>n%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 e});g((e,c,s)=>{if(s?.type==="array"){let a=e;a.min=function(t,{message:r}={}){return this.refine(n=>n.length>=t,{message:r||`Array must contain at least ${t} items.`})},a.max=function(t,{message:r}={}){return this.refine(n=>n.length<=t,{message:r||`Array must contain at most ${t} items.`})},a.length=function(t,{message:r}={}){return this.refine(n=>n.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 n=new Set;try{return r.every(o=>{let u=JSON.stringify(o);return n.has(u)?!1:(n.add(u),!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 e});exports.extend=g;exports.hookOriginal=h;exports.s=p;exports.schema=p;
|
package/dist/full.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import './string.mjs';
|
|
2
2
|
import './number.mjs';
|
|
3
3
|
import './array.mjs';
|
|
4
|
-
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s, s as schema } from './index.mjs';
|
|
4
|
+
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s, s as schema } from './index.mjs';
|
package/dist/full.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import './string.js';
|
|
2
2
|
import './number.js';
|
|
3
3
|
import './array.js';
|
|
4
|
-
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s, s as schema } from './index.js';
|
|
4
|
+
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s, s as schema } from './index.js';
|
package/dist/full.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import'./chunk-
|
|
1
|
+
import'./chunk-YYPP27RX.mjs';import'./chunk-CLEFU3BA.mjs';import'./chunk-YUL3WJPU.mjs';export{c as extend,b as hookOriginal,a as s,a as schema}from'./chunk-QQOSS5DZ.mjs';
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var E={abortEarly:true};function b(e,t){if(!t)return e;let n=e?.cause?.key?`${t}.${e.cause.key}`:`${t}`;return {message:`Error parsing key "${n}": ${e.message}`,cause:{key:n}}}function w(e,t,n){if(e.errors?.length)for(let a=0;a<e.errors.length;a++)t.push(b(e.errors[a],n));}function T(e){return {...E,...e}}var
|
|
1
|
+
'use strict';var E={abortEarly:true};function b(e,t){if(!t)return e;let n=e?.cause?.key?`${t}.${e.cause.key}`:`${t}`;return {message:`Error parsing key "${n}": ${e.message}`,cause:{key:n}}}function w(e,t,n){if(e.errors?.length)for(let a=0;a<e.errors.length;a++)t.push(b(e.errors[a],n));}function T(e){return {...E,...e}}var S=e=>typeof e=="string"||e instanceof String,y=e=>(typeof e=="number"||e instanceof Number)&&!Number.isNaN(e),g=e=>e===true||e===false,D=e=>e instanceof Date&&!Number.isNaN(e.getTime()),_=e=>typeof e=="function",$=e=>Array.isArray(e),x=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),m={object(e,t){let n=I(x,{...t,type:"object"});return n._getDescription=()=>`object({ ${Object.entries(e).map(([p,o])=>`${p}: ${o._getDescription()}`).join(", ")} })`,h(n,"_parse",(a,p,o)=>{let r=a(p,o),{abortEarly:s}=T(o);if(r.success===false)return r;let u={},c=[];for(let i in e){let f=e[i]._parse(r.data[i],o);if(f.success)u[i]=f.data;else {if(f=f,s!==false){let l=b(f.error,i);return {success:false,error:l,errors:[l]}}w(f,c,i);}}return c.length>0?{success:false,error:c[0],errors:c}:{success:true,data:u}}),n},string(e){return I(S,{...e,type:"string"})},number(e){return I(y,{...e,type:"number"})},boolean(e){return I(g,{...e,type:"boolean"})},date(e){return I(D,{...e,type:"date"})},function(e){return I(_,{...e,type:"function"})},enum(e,t){let n=r=>e.includes(r),a=r=>`Invalid ${p} value. Expected ${e.map(s=>`"${s}"`).join(" | ")}, received "${r}".`,p="enum",o=I(n,{message:a,...t,type:p});return o._getDescription=()=>`enum(${e.map(r=>`"${r}"`).join(" | ")})`,o},array(e,t){let n=I($,{...t,type:"array"});return n._getDescription=()=>`array(${e._getDescription()})`,h(n,"_parse",(a,p,o)=>{let r=a(p,o),{abortEarly:s}=T(o);if(r.success===false)return r;let u=[],c=[];for(let i=0;i<r.data.length;i++){let f=e._parse(r.data[i],o);if(f.success)u.push(f.data);else {if(f=f,s!==false){let l=b(f.error,i);return {success:false,error:l,errors:[l]}}w(f,c,i);}}return c.length>0?{success:false,error:c[0],errors:c}:{success:true,data:u}}),n},any(){return I(()=>true)},preprocess(e,t){return h(t,"_parse",(n,a)=>(a=e(a),n(a))),t},union(e,t){return I(o=>{for(let r=0;r<e.length;r++){let s=e[r]._parse(o);if(s.success)return s}return false},{message:o=>`Invalid union value. Expected the value to match one of the schemas:${e.map((r,s)=>` ${s+1}. ${r._getDescription()}`).join(",")} but received "${typeof o}" with value: ${x(o)?JSON.stringify(o):`"${o}"`}`,...t,type:"union"})},literal(e,t){let p=I(o=>o===e,{message:o=>t?.message?typeof t.message=="function"?t.message(o):t.message:`Expected literal value "${e}", received "${o}"`,name:t?.name,type:"literal"});return p._getDescription=()=>`literal("${e}")`,p},coerce:{string(e){return m.preprocess(t=>String(t),m.string(e))},number(e){let t=e?.message??(n=>`Cannot coerce "${n}" to a valid number.`);return m.preprocess(n=>Number(n),m.number({...e,message:t}))},boolean(e){return m.preprocess(t=>!!t,m.boolean(e))},date(e){let t=e?.message??(n=>`Cannot coerce "${n}" to a valid date.`);return m.preprocess(n=>new Date(n),m.date({...e,message:t}))}},cast:{boolean(e){let t=e?.message??(n=>`Cannot cast "${n}" to boolean. Accepted: true/false, 1/0, yes/no, on/off.`);return m.preprocess(n=>{let a;return S(n)&&(a=n.toLowerCase()),a==="true"||a==="yes"||a==="on"||a==="1"||n===1?true:a==="false"||a==="no"||a==="off"||a==="0"||n===0?false:n},m.boolean({...e,message:t}))},number(e){let t=e?.message??(n=>`Cannot cast "${n}" to a number. Expected a numeric string or number.`);return m.preprocess(n=>{if(g(n))return Number(n);if(S(n)){let a=n.trim();if(a==="")return n;let p=Number(a);if(Number.isFinite(p))return p}return n},m.number({...e,message:t}))},string(e){let t=e?.message??(n=>`Cannot cast "${n}" to string. Expected a string, number, or boolean.`);return m.preprocess(n=>g(n)||y(n)&&Number.isFinite(n)?String(n):n,m.string({...e,message:t}))},date(e){let t=e?.message??(n=>`Cannot cast "${n}" to a valid date.`);return m.preprocess(n=>{let a;return S(n)&&(a=n.trim()),y(n)&&Number.isFinite(n)||a?new Date(a??n):n},m.date({...e,message:t}))},json(e,t){let n=t?.message??(a=>`Cannot parse "${a}" as JSON.`);return h(e,"_parse",(a,p)=>{if(S(p))try{p=JSON.parse(p);}catch{let o={message:typeof n=="function"?n(p):n};return {success:false,error:o,errors:[o]}}return a(p)}),e}}};function P(e){return t=>`The value "${t}" must be type of ${e} but is type of "${typeof t}".`}function h(e,t,n){let a=e[t];e[t]=(...p)=>n(a,...p);}function I(e,{type:t="any",name:n,message:a}={}){a=a||P(t);let p={name:n,message:a,type:t},o={_getName(){return n},_getType(){return t},_getDescription(){return this._getName()??this._getType()},_parse(r,s){let u=e(r);if(u===true)return {success:true,data:r};if(typeof u=="object"&&u?.success===true)return u;let c={message:typeof a=="function"?a(r):a};return {success:false,error:c,errors:[c]}},parse(r,s){let u=o._parse(r,s);if(!u.success)throw u=u,new Error(u.error.message,{cause:u.error.cause});return u.data},safeParse(r,s){return o._parse(r,s)},transform(r){return h(this,"_parse",(s,u,c)=>{let i=s(u,c);return i.success&&(i.data=r(i.data)),i}),this},optional(){return h(this,"_parse",(r,s,u)=>{let c=r(s,u);return !c.success&&s===void 0&&(c.data=void 0,c.success=true),c}),this},nullable(){return h(this,"_parse",(r,s,u)=>{let c=r(s,u);return !c.success&&s===null&&(c.data=null,c.success=true),c}),this},nullish(){return h(this,"_parse",(r,s,u)=>{let c=r(s,u);return !c.success&&s==null&&(c.success=true,c.data=s),c}),this},default(r){return h(this,"_parse",(s,u,c)=>(u===void 0&&(u=typeof r=="function"?r():r),s(u,c))),this},catch(r){return h(this,"_parse",(s,u,c)=>{let i=s(u,c);return i.success?i:{success:true,data:typeof r=="function"?r({input:u,error:i.error}):r}}),this},pipe(r){return h(this,"_parse",(s,u,c)=>{let i=s(u,c);return i.success?r._parse(i.data,c):i}),this},refine(r,{message:s,type:u}={}){return u&&(s=s||P(u),t=u),h(this,"_parse",(c,i,f)=>{let l=c(i,f);T(f);if(!l.success)return l;let d=r(l.data);if(d===true||typeof d=="object"&&d?.success===true)return l;let k={message:typeof s=="function"?s(i):s};return {success:false,error:k,errors:[k]}}),this}};return O.length>0?O.reduce((r,s)=>s(r,e,p)??r,o):o}var O=[];function A(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");O.push(e);}exports.extend=A;exports.hookOriginal=h;exports.s=m;exports.schema=m;
|
package/dist/index.d.mts
CHANGED
|
@@ -19,7 +19,7 @@ interface ParseOptions {
|
|
|
19
19
|
abortEarly?: boolean;
|
|
20
20
|
}
|
|
21
21
|
interface SchemaInterface<Input, Output> {
|
|
22
|
-
_getName(): string;
|
|
22
|
+
_getName(): undefined | string;
|
|
23
23
|
_getType(): string;
|
|
24
24
|
_getDescription(): string;
|
|
25
25
|
_parse(value: Input | Partial<Input>, options?: ParseOptions): InternalParseOutput<Output>;
|
|
@@ -51,6 +51,8 @@ interface BooleanSchemaInterface extends SchemaInterface<boolean, boolean> {
|
|
|
51
51
|
}
|
|
52
52
|
interface DateSchemaInterface extends SchemaInterface<Date, Date> {
|
|
53
53
|
}
|
|
54
|
+
interface FunctionSchemaInterface extends SchemaInterface<Function, Function> {
|
|
55
|
+
}
|
|
54
56
|
interface ArraySchemaInterface<T extends SchemaType> extends SchemaInterface<Array<ReturnType<T['parse']>>, Array<ReturnType<T['parse']>>> {
|
|
55
57
|
}
|
|
56
58
|
interface ObjectSchemaInterface<T extends Record<string, SchemaType>> extends SchemaInterface<{
|
|
@@ -59,7 +61,7 @@ interface ObjectSchemaInterface<T extends Record<string, SchemaType>> extends Sc
|
|
|
59
61
|
[Property in keyof T]: ReturnType<T[Property]['parse']>;
|
|
60
62
|
}> {
|
|
61
63
|
}
|
|
62
|
-
type SchemaType = LiteralSchemaInterface<string> | LiteralSchemaInterface<number> | LiteralSchemaInterface<boolean> | 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>;
|
|
64
|
+
type SchemaType = LiteralSchemaInterface<string> | LiteralSchemaInterface<number> | LiteralSchemaInterface<boolean> | 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> | FunctionSchemaInterface | 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>;
|
|
63
65
|
type ErrorMessage = string | ((value: unknown) => string);
|
|
64
66
|
type ExtenderType = (inter: SchemaType, validation: Function, options?: {
|
|
65
67
|
message: ErrorMessage;
|
|
@@ -140,6 +142,20 @@ declare const s: {
|
|
|
140
142
|
* ```
|
|
141
143
|
*/
|
|
142
144
|
date(options?: SchemaInterfaceOptions): DateSchemaInterface;
|
|
145
|
+
/**
|
|
146
|
+
* Creates a function schema that validates the value is callable.
|
|
147
|
+
*
|
|
148
|
+
* @param options - Optional configuration (name, message)
|
|
149
|
+
* @returns Function schema interface
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```typescript
|
|
153
|
+
* const callbackSchema = s.function();
|
|
154
|
+
* callbackSchema.parse(() => {}); // () => {}
|
|
155
|
+
* callbackSchema.parse('hello'); // throws
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
158
|
+
function(options?: SchemaInterfaceOptions): FunctionSchemaInterface;
|
|
143
159
|
/**
|
|
144
160
|
* Creates an enum schema with predefined values.
|
|
145
161
|
*
|
|
@@ -179,7 +195,7 @@ declare const s: {
|
|
|
179
195
|
* const result = anySchema.parse({ anything: true }); // { anything: true }
|
|
180
196
|
* ```
|
|
181
197
|
*/
|
|
182
|
-
any():
|
|
198
|
+
any(): SchemaInterface<unknown, unknown>;
|
|
183
199
|
/**
|
|
184
200
|
* Preprocesses a value before passing it to a schema for validation.
|
|
185
201
|
*
|
|
@@ -395,4 +411,4 @@ declare function extend(callback: ExtenderType): void;
|
|
|
395
411
|
*/
|
|
396
412
|
type Infer<T> = T extends SchemaType ? ReturnType<T['parse']> : unknown;
|
|
397
413
|
|
|
398
|
-
export { type ArraySchemaInterface, type BooleanSchemaInterface, type DateSchemaInterface, type EnumSchemaInterface, type ErrorStructure, type ExtenderType, type Infer, type Invalid, type LiteralSchemaInterface, type NumberSchemaInterface, type ObjectSchemaInterface, type SchemaInterface, type SchemaInterfaceOptions, type SchemaType, type StringSchemaInterface, type UnionSchemaInterface, type Valid, extend, hookOriginal, s, s as schema };
|
|
414
|
+
export { type ArraySchemaInterface, type BooleanSchemaInterface, type DateSchemaInterface, type EnumSchemaInterface, type ErrorStructure, type ExtenderType, type FunctionSchemaInterface, type Infer, type Invalid, type LiteralSchemaInterface, type NumberSchemaInterface, type ObjectSchemaInterface, type SchemaInterface, type SchemaInterfaceOptions, type SchemaType, type StringSchemaInterface, type UnionSchemaInterface, type Valid, extend, hookOriginal, s, s as schema };
|
package/dist/index.d.ts
CHANGED
|
@@ -19,7 +19,7 @@ interface ParseOptions {
|
|
|
19
19
|
abortEarly?: boolean;
|
|
20
20
|
}
|
|
21
21
|
interface SchemaInterface<Input, Output> {
|
|
22
|
-
_getName(): string;
|
|
22
|
+
_getName(): undefined | string;
|
|
23
23
|
_getType(): string;
|
|
24
24
|
_getDescription(): string;
|
|
25
25
|
_parse(value: Input | Partial<Input>, options?: ParseOptions): InternalParseOutput<Output>;
|
|
@@ -51,6 +51,8 @@ interface BooleanSchemaInterface extends SchemaInterface<boolean, boolean> {
|
|
|
51
51
|
}
|
|
52
52
|
interface DateSchemaInterface extends SchemaInterface<Date, Date> {
|
|
53
53
|
}
|
|
54
|
+
interface FunctionSchemaInterface extends SchemaInterface<Function, Function> {
|
|
55
|
+
}
|
|
54
56
|
interface ArraySchemaInterface<T extends SchemaType> extends SchemaInterface<Array<ReturnType<T['parse']>>, Array<ReturnType<T['parse']>>> {
|
|
55
57
|
}
|
|
56
58
|
interface ObjectSchemaInterface<T extends Record<string, SchemaType>> extends SchemaInterface<{
|
|
@@ -59,7 +61,7 @@ interface ObjectSchemaInterface<T extends Record<string, SchemaType>> extends Sc
|
|
|
59
61
|
[Property in keyof T]: ReturnType<T[Property]['parse']>;
|
|
60
62
|
}> {
|
|
61
63
|
}
|
|
62
|
-
type SchemaType = LiteralSchemaInterface<string> | LiteralSchemaInterface<number> | LiteralSchemaInterface<boolean> | 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>;
|
|
64
|
+
type SchemaType = LiteralSchemaInterface<string> | LiteralSchemaInterface<number> | LiteralSchemaInterface<boolean> | 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> | FunctionSchemaInterface | 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>;
|
|
63
65
|
type ErrorMessage = string | ((value: unknown) => string);
|
|
64
66
|
type ExtenderType = (inter: SchemaType, validation: Function, options?: {
|
|
65
67
|
message: ErrorMessage;
|
|
@@ -140,6 +142,20 @@ declare const s: {
|
|
|
140
142
|
* ```
|
|
141
143
|
*/
|
|
142
144
|
date(options?: SchemaInterfaceOptions): DateSchemaInterface;
|
|
145
|
+
/**
|
|
146
|
+
* Creates a function schema that validates the value is callable.
|
|
147
|
+
*
|
|
148
|
+
* @param options - Optional configuration (name, message)
|
|
149
|
+
* @returns Function schema interface
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```typescript
|
|
153
|
+
* const callbackSchema = s.function();
|
|
154
|
+
* callbackSchema.parse(() => {}); // () => {}
|
|
155
|
+
* callbackSchema.parse('hello'); // throws
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
158
|
+
function(options?: SchemaInterfaceOptions): FunctionSchemaInterface;
|
|
143
159
|
/**
|
|
144
160
|
* Creates an enum schema with predefined values.
|
|
145
161
|
*
|
|
@@ -179,7 +195,7 @@ declare const s: {
|
|
|
179
195
|
* const result = anySchema.parse({ anything: true }); // { anything: true }
|
|
180
196
|
* ```
|
|
181
197
|
*/
|
|
182
|
-
any():
|
|
198
|
+
any(): SchemaInterface<unknown, unknown>;
|
|
183
199
|
/**
|
|
184
200
|
* Preprocesses a value before passing it to a schema for validation.
|
|
185
201
|
*
|
|
@@ -395,4 +411,4 @@ declare function extend(callback: ExtenderType): void;
|
|
|
395
411
|
*/
|
|
396
412
|
type Infer<T> = T extends SchemaType ? ReturnType<T['parse']> : unknown;
|
|
397
413
|
|
|
398
|
-
export { type ArraySchemaInterface, type BooleanSchemaInterface, type DateSchemaInterface, type EnumSchemaInterface, type ErrorStructure, type ExtenderType, type Infer, type Invalid, type LiteralSchemaInterface, type NumberSchemaInterface, type ObjectSchemaInterface, type SchemaInterface, type SchemaInterfaceOptions, type SchemaType, type StringSchemaInterface, type UnionSchemaInterface, type Valid, extend, hookOriginal, s, s as schema };
|
|
414
|
+
export { type ArraySchemaInterface, type BooleanSchemaInterface, type DateSchemaInterface, type EnumSchemaInterface, type ErrorStructure, type ExtenderType, type FunctionSchemaInterface, type Infer, type Invalid, type LiteralSchemaInterface, type NumberSchemaInterface, type ObjectSchemaInterface, type SchemaInterface, type SchemaInterfaceOptions, type SchemaType, type StringSchemaInterface, type UnionSchemaInterface, type Valid, extend, hookOriginal, s, s as schema };
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export{c as extend,b as hookOriginal,a as s,a as schema}from'./chunk-
|
|
1
|
+
export{c as extend,b as hookOriginal,a as s,a as schema}from'./chunk-QQOSS5DZ.mjs';
|
package/dist/number.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var E={abortEarly:true};function g(e,a){if(!a)return e;let n=e?.cause?.key?`${a}.${e.cause.key}`:`${a}`;return {message:`Error parsing key "${n}": ${e.message}`,cause:{key:n}}}function w(e,a,n){if(e.errors?.length)for(let r=0;r<e.errors.length;r++)a.push(g(e.errors[r],n));}function O(e){return {...E,...e}}var l=e=>typeof e=="string"||e instanceof String,
|
|
1
|
+
'use strict';var E={abortEarly:true};function g(e,a){if(!a)return e;let n=e?.cause?.key?`${a}.${e.cause.key}`:`${a}`;return {message:`Error parsing key "${n}": ${e.message}`,cause:{key:n}}}function w(e,a,n){if(e.errors?.length)for(let r=0;r<e.errors.length;r++)a.push(g(e.errors[r],n));}function O(e){return {...E,...e}}var l=e=>typeof e=="string"||e instanceof String,b=e=>(typeof e=="number"||e instanceof Number)&&!Number.isNaN(e),y=e=>e===true||e===false,$=e=>e instanceof Date&&!Number.isNaN(e.getTime()),_=e=>typeof e=="function",D=e=>Array.isArray(e),x=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),p={object(e,a){let n=I(x,{...a,type:"object"});return n._getDescription=()=>`object({ ${Object.entries(e).map(([c,s])=>`${c}: ${s._getDescription()}`).join(", ")} })`,h(n,"_parse",(r,c,s)=>{let t=r(c,s),{abortEarly:u}=O(s);if(t.success===false)return t;let i={},o=[];for(let m in e){let f=e[m]._parse(t.data[m],s);if(f.success)i[m]=f.data;else {if(f=f,u!==false){let S=g(f.error,m);return {success:false,error:S,errors:[S]}}w(f,o,m);}}return o.length>0?{success:false,error:o[0],errors:o}:{success:true,data:i}}),n},string(e){return I(l,{...e,type:"string"})},number(e){return I(b,{...e,type:"number"})},boolean(e){return I(y,{...e,type:"boolean"})},date(e){return I($,{...e,type:"date"})},function(e){return I(_,{...e,type:"function"})},enum(e,a){let n=t=>e.includes(t),r=t=>`Invalid ${c} value. Expected ${e.map(u=>`"${u}"`).join(" | ")}, received "${t}".`,c="enum",s=I(n,{message:r,...a,type:c});return s._getDescription=()=>`enum(${e.map(t=>`"${t}"`).join(" | ")})`,s},array(e,a){let n=I(D,{...a,type:"array"});return n._getDescription=()=>`array(${e._getDescription()})`,h(n,"_parse",(r,c,s)=>{let t=r(c,s),{abortEarly:u}=O(s);if(t.success===false)return t;let i=[],o=[];for(let m=0;m<t.data.length;m++){let f=e._parse(t.data[m],s);if(f.success)i.push(f.data);else {if(f=f,u!==false){let S=g(f.error,m);return {success:false,error:S,errors:[S]}}w(f,o,m);}}return o.length>0?{success:false,error:o[0],errors:o}:{success:true,data:i}}),n},any(){return I(()=>true)},preprocess(e,a){return h(a,"_parse",(n,r)=>(r=e(r),n(r))),a},union(e,a){return I(s=>{for(let t=0;t<e.length;t++){let u=e[t]._parse(s);if(u.success)return u}return false},{message:s=>`Invalid union value. Expected the value to match one of the schemas:${e.map((t,u)=>` ${u+1}. ${t._getDescription()}`).join(",")} but received "${typeof s}" with value: ${x(s)?JSON.stringify(s):`"${s}"`}`,...a,type:"union"})},literal(e,a){let c=I(s=>s===e,{message:s=>a?.message?typeof a.message=="function"?a.message(s):a.message:`Expected literal value "${e}", received "${s}"`,name:a?.name,type:"literal"});return c._getDescription=()=>`literal("${e}")`,c},coerce:{string(e){return p.preprocess(a=>String(a),p.string(e))},number(e){let a=e?.message??(n=>`Cannot coerce "${n}" to a valid number.`);return p.preprocess(n=>Number(n),p.number({...e,message:a}))},boolean(e){return p.preprocess(a=>!!a,p.boolean(e))},date(e){let a=e?.message??(n=>`Cannot coerce "${n}" to a valid date.`);return p.preprocess(n=>new Date(n),p.date({...e,message:a}))}},cast:{boolean(e){let a=e?.message??(n=>`Cannot cast "${n}" to boolean. Accepted: true/false, 1/0, yes/no, on/off.`);return p.preprocess(n=>{let r;return l(n)&&(r=n.toLowerCase()),r==="true"||r==="yes"||r==="on"||r==="1"||n===1?true:r==="false"||r==="no"||r==="off"||r==="0"||n===0?false:n},p.boolean({...e,message:a}))},number(e){let a=e?.message??(n=>`Cannot cast "${n}" to a number. Expected a numeric string or number.`);return p.preprocess(n=>{if(y(n))return Number(n);if(l(n)){let r=n.trim();if(r==="")return n;let c=Number(r);if(Number.isFinite(c))return c}return n},p.number({...e,message:a}))},string(e){let a=e?.message??(n=>`Cannot cast "${n}" to string. Expected a string, number, or boolean.`);return p.preprocess(n=>y(n)||b(n)&&Number.isFinite(n)?String(n):n,p.string({...e,message:a}))},date(e){let a=e?.message??(n=>`Cannot cast "${n}" to a valid date.`);return p.preprocess(n=>{let r;return l(n)&&(r=n.trim()),b(n)&&Number.isFinite(n)||r?new Date(r??n):n},p.date({...e,message:a}))},json(e,a){let n=a?.message??(r=>`Cannot parse "${r}" as JSON.`);return h(e,"_parse",(r,c)=>{if(l(c))try{c=JSON.parse(c);}catch{let s={message:typeof n=="function"?n(c):n};return {success:false,error:s,errors:[s]}}return r(c)}),e}}};function N(e){return a=>`The value "${a}" must be type of ${e} but is type of "${typeof a}".`}function h(e,a,n){let r=e[a];e[a]=(...c)=>n(r,...c);}function I(e,{type:a="any",name:n,message:r}={}){r=r||N(a);let c={name:n,message:r,type:a},s={_getName(){return n},_getType(){return a},_getDescription(){return this._getName()??this._getType()},_parse(t,u){let i=e(t);if(i===true)return {success:true,data:t};if(typeof i=="object"&&i?.success===true)return i;let o={message:typeof r=="function"?r(t):r};return {success:false,error:o,errors:[o]}},parse(t,u){let i=s._parse(t,u);if(!i.success)throw i=i,new Error(i.error.message,{cause:i.error.cause});return i.data},safeParse(t,u){return s._parse(t,u)},transform(t){return h(this,"_parse",(u,i,o)=>{let m=u(i,o);return m.success&&(m.data=t(m.data)),m}),this},optional(){return h(this,"_parse",(t,u,i)=>{let o=t(u,i);return !o.success&&u===void 0&&(o.data=void 0,o.success=true),o}),this},nullable(){return h(this,"_parse",(t,u,i)=>{let o=t(u,i);return !o.success&&u===null&&(o.data=null,o.success=true),o}),this},nullish(){return h(this,"_parse",(t,u,i)=>{let o=t(u,i);return !o.success&&u==null&&(o.success=true,o.data=u),o}),this},default(t){return h(this,"_parse",(u,i,o)=>(i===void 0&&(i=typeof t=="function"?t():t),u(i,o))),this},catch(t){return h(this,"_parse",(u,i,o)=>{let m=u(i,o);return m.success?m:{success:true,data:typeof t=="function"?t({input:i,error:m.error}):t}}),this},pipe(t){return h(this,"_parse",(u,i,o)=>{let m=u(i,o);return m.success?t._parse(m.data,o):m}),this},refine(t,{message:u,type:i}={}){return i&&(u=u||N(i),a=i),h(this,"_parse",(o,m,f)=>{let S=o(m,f);O(f);if(!S.success)return S;let d=t(S.data);if(d===true||typeof d=="object"&&d?.success===true)return S;let k={message:typeof u=="function"?u(m):u};return {success:false,error:k,errors:[k]}}),this}};return T.length>0?T.reduce((t,u)=>u(t,e,c)??t,s):s}var T=[];function P(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");T.push(e);}P((e,a,n)=>{if(n?.type==="number"){let r=e;r.min=function(c,{message:s}={}){return this.refine(t=>t>=c,{message:s||`Number must be greater than or equal to ${c}.`})},r.max=function(c,{message:s}={}){return this.refine(t=>t<=c,{message:s||`Number must be less than or equal to ${c}.`})},r.positive=function({message:c}={}){return this.refine(s=>s>0,{message:c||"Number must be positive."})},r.negative=function({message:c}={}){return this.refine(s=>s<0,{message:c||"Number must be negative."})},r.int=function({message:c}={}){return this.refine(s=>Number.isInteger(s),{message:c||"Number must be an integer."})},r.float=function({message:c}={}){return this.refine(s=>Number.isFinite(s)&&!Number.isInteger(s),{message:c||"Number must be a floating point (non-integer)."})},r.multipleOf=function(c,{message:s}={}){return this.refine(t=>t%c===0,{message:s||`Number must be a multiple of ${c}.`})},r.finite=function({message:c}={}){return this.refine(s=>Number.isFinite(s),{message:c||"Number must be finite."})};}return e});exports.extend=P;exports.hookOriginal=h;exports.s=p;exports.schema=p;
|
package/dist/number.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s, s as schema } from './index.mjs';
|
|
1
|
+
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s, s as schema } from './index.mjs';
|
|
2
2
|
|
|
3
3
|
declare module './index.ts' {
|
|
4
4
|
interface NumberSchemaInterface {
|
package/dist/number.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s, s as schema } from './index.js';
|
|
1
|
+
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s, s as schema } from './index.js';
|
|
2
2
|
|
|
3
3
|
declare module './index.ts' {
|
|
4
4
|
interface NumberSchemaInterface {
|
package/dist/number.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import'./chunk-
|
|
1
|
+
import'./chunk-CLEFU3BA.mjs';export{c as extend,b as hookOriginal,a as s,a as schema}from'./chunk-QQOSS5DZ.mjs';
|
package/dist/string.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var E={abortEarly:true};function b(e,s){if(!s)return e;let
|
|
1
|
+
'use strict';var E={abortEarly:true};function b(e,s){if(!s)return e;let t=e?.cause?.key?`${s}.${e.cause.key}`:`${s}`;return {message:`Error parsing key "${t}": ${e.message}`,cause:{key:t}}}function k(e,s,t){if(e.errors?.length)for(let r=0;r<e.errors.length;r++)s.push(b(e.errors[r],t));}function O(e){return {...E,...e}}var l=e=>typeof e=="string"||e instanceof String,d=e=>(typeof e=="number"||e instanceof Number)&&!Number.isNaN(e),y=e=>e===true||e===false,_=e=>e instanceof Date&&!Number.isNaN(e.getTime()),D=e=>typeof e=="function",N=e=>Array.isArray(e),x=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),m={object(e,s){let t=S(x,{...s,type:"object"});return t._getDescription=()=>`object({ ${Object.entries(e).map(([a,c])=>`${a}: ${c._getDescription()}`).join(", ")} })`,h(t,"_parse",(r,a,c)=>{let n=r(a,c),{abortEarly:o}=O(c);if(n.success===false)return n;let i={},u=[];for(let f in e){let p=e[f]._parse(n.data[f],c);if(p.success)i[f]=p.data;else {if(p=p,o!==false){let I=b(p.error,f);return {success:false,error:I,errors:[I]}}k(p,u,f);}}return u.length>0?{success:false,error:u[0],errors:u}:{success:true,data:i}}),t},string(e){return S(l,{...e,type:"string"})},number(e){return S(d,{...e,type:"number"})},boolean(e){return S(y,{...e,type:"boolean"})},date(e){return S(_,{...e,type:"date"})},function(e){return S(D,{...e,type:"function"})},enum(e,s){let t=n=>e.includes(n),r=n=>`Invalid ${a} value. Expected ${e.map(o=>`"${o}"`).join(" | ")}, received "${n}".`,a="enum",c=S(t,{message:r,...s,type:a});return c._getDescription=()=>`enum(${e.map(n=>`"${n}"`).join(" | ")})`,c},array(e,s){let t=S(N,{...s,type:"array"});return t._getDescription=()=>`array(${e._getDescription()})`,h(t,"_parse",(r,a,c)=>{let n=r(a,c),{abortEarly:o}=O(c);if(n.success===false)return n;let i=[],u=[];for(let f=0;f<n.data.length;f++){let p=e._parse(n.data[f],c);if(p.success)i.push(p.data);else {if(p=p,o!==false){let I=b(p.error,f);return {success:false,error:I,errors:[I]}}k(p,u,f);}}return u.length>0?{success:false,error:u[0],errors:u}:{success:true,data:i}}),t},any(){return S(()=>true)},preprocess(e,s){return h(s,"_parse",(t,r)=>(r=e(r),t(r))),s},union(e,s){return S(c=>{for(let n=0;n<e.length;n++){let o=e[n]._parse(c);if(o.success)return o}return false},{message:c=>`Invalid union value. Expected the value to match one of the schemas:${e.map((n,o)=>` ${o+1}. ${n._getDescription()}`).join(",")} but received "${typeof c}" with value: ${x(c)?JSON.stringify(c):`"${c}"`}`,...s,type:"union"})},literal(e,s){let a=S(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 a._getDescription=()=>`literal("${e}")`,a},coerce:{string(e){return m.preprocess(s=>String(s),m.string(e))},number(e){let s=e?.message??(t=>`Cannot coerce "${t}" to a valid number.`);return m.preprocess(t=>Number(t),m.number({...e,message:s}))},boolean(e){return m.preprocess(s=>!!s,m.boolean(e))},date(e){let s=e?.message??(t=>`Cannot coerce "${t}" to a valid date.`);return m.preprocess(t=>new Date(t),m.date({...e,message:s}))}},cast:{boolean(e){let s=e?.message??(t=>`Cannot cast "${t}" to boolean. Accepted: true/false, 1/0, yes/no, on/off.`);return m.preprocess(t=>{let r;return l(t)&&(r=t.toLowerCase()),r==="true"||r==="yes"||r==="on"||r==="1"||t===1?true:r==="false"||r==="no"||r==="off"||r==="0"||t===0?false:t},m.boolean({...e,message:s}))},number(e){let s=e?.message??(t=>`Cannot cast "${t}" to a number. Expected a numeric string or number.`);return m.preprocess(t=>{if(y(t))return Number(t);if(l(t)){let r=t.trim();if(r==="")return t;let a=Number(r);if(Number.isFinite(a))return a}return t},m.number({...e,message:s}))},string(e){let s=e?.message??(t=>`Cannot cast "${t}" to string. Expected a string, number, or boolean.`);return m.preprocess(t=>y(t)||d(t)&&Number.isFinite(t)?String(t):t,m.string({...e,message:s}))},date(e){let s=e?.message??(t=>`Cannot cast "${t}" to a valid date.`);return m.preprocess(t=>{let r;return l(t)&&(r=t.trim()),d(t)&&Number.isFinite(t)||r?new Date(r??t):t},m.date({...e,message:s}))},json(e,s){let t=s?.message??(r=>`Cannot parse "${r}" as JSON.`);return h(e,"_parse",(r,a)=>{if(l(a))try{a=JSON.parse(a);}catch{let c={message:typeof t=="function"?t(a):t};return {success:false,error:c,errors:[c]}}return r(a)}),e}}};function P(e){return s=>`The value "${s}" must be type of ${e} but is type of "${typeof s}".`}function h(e,s,t){let r=e[s];e[s]=(...a)=>t(r,...a);}function S(e,{type:s="any",name:t,message:r}={}){r=r||P(s);let a={name:t,message:r,type:s},c={_getName(){return t},_getType(){return s},_getDescription(){return this._getName()??this._getType()},_parse(n,o){let i=e(n);if(i===true)return {success:true,data:n};if(typeof i=="object"&&i?.success===true)return i;let u={message:typeof r=="function"?r(n):r};return {success:false,error:u,errors:[u]}},parse(n,o){let i=c._parse(n,o);if(!i.success)throw i=i,new Error(i.error.message,{cause:i.error.cause});return i.data},safeParse(n,o){return c._parse(n,o)},transform(n){return h(this,"_parse",(o,i,u)=>{let f=o(i,u);return f.success&&(f.data=n(f.data)),f}),this},optional(){return h(this,"_parse",(n,o,i)=>{let u=n(o,i);return !u.success&&o===void 0&&(u.data=void 0,u.success=true),u}),this},nullable(){return h(this,"_parse",(n,o,i)=>{let u=n(o,i);return !u.success&&o===null&&(u.data=null,u.success=true),u}),this},nullish(){return h(this,"_parse",(n,o,i)=>{let u=n(o,i);return !u.success&&o==null&&(u.success=true,u.data=o),u}),this},default(n){return h(this,"_parse",(o,i,u)=>(i===void 0&&(i=typeof n=="function"?n():n),o(i,u))),this},catch(n){return h(this,"_parse",(o,i,u)=>{let f=o(i,u);return f.success?f:{success:true,data:typeof n=="function"?n({input:i,error:f.error}):n}}),this},pipe(n){return h(this,"_parse",(o,i,u)=>{let f=o(i,u);return f.success?n._parse(f.data,u):f}),this},refine(n,{message:o,type:i}={}){return i&&(o=o||P(i),s=i),h(this,"_parse",(u,f,p)=>{let I=u(f,p);O(p);if(!I.success)return I;let g=n(I.data);if(g===true||typeof g=="object"&&g?.success===true)return I;let T={message:typeof o=="function"?o(f):o};return {success:false,error:T,errors:[T]}}),this}};return w.length>0?w.reduce((n,o)=>o(n,e,a)??n,c):c}var w=[];function $(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");w.push(e);}$((e,s,t)=>{if(t?.type==="string"){let r=e;r.min=function(a,{message:c}={}){return this.refine(n=>n.length>=a,{message:c||(n=>`String must be at least ${a} characters long (received ${n.length} characters: "${n}")`)})},r.max=function(a,{message:c}={}){return this.refine(n=>n.length<=a,{message:c||(n=>`String must be at most ${a} characters long (received ${n.length} characters: "${n}")`)})},r.length=function(a,{message:c}={}){return this.refine(n=>n.length===a,{message:c||(n=>`String must be exactly ${a} characters long (received ${n.length} characters: "${n}")`)})},r.nonEmpty=function({message:a}={}){return this.refine(c=>c.length>0,{message:a||"String must not be empty (received empty string)"})},r.startsWith=function(a,{message:c}={}){return this.refine(n=>n.startsWith(a),{message:c||(n=>`String must start with "${a}" (received: "${n}")`)})},r.endsWith=function(a,{message:c}={}){return this.refine(n=>n.endsWith(a),{message:c||(n=>`String must end with "${a}" (received: "${n}")`)})},r.includes=function(a,{message:c}={}){return this.refine(n=>n.includes(a),{message:c||(n=>`String must include "${a}" (received: "${n}")`)})},r.toLowerCase=function(){return this.transform(a=>a.toLowerCase())},r.toUpperCase=function(){return this.transform(a=>a.toUpperCase())},r.trim=function(){return this.transform(a=>a.trim())},r.padStart=function(a,c=" "){return this.transform(n=>n.padStart(a,c))},r.padEnd=function(a,c=" "){return this.transform(n=>n.padEnd(a,c))},r.replace=function(a,c){return this.transform(n=>n.replace(a,c))};}return e});exports.extend=$;exports.hookOriginal=h;exports.s=m;exports.schema=m;
|
package/dist/string.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s, s as schema } from './index.mjs';
|
|
1
|
+
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s, s as schema } from './index.mjs';
|
|
2
2
|
|
|
3
3
|
declare module './index.ts' {
|
|
4
4
|
interface StringSchemaInterface {
|
package/dist/string.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s, s as schema } from './index.js';
|
|
1
|
+
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s, s as schema } from './index.js';
|
|
2
2
|
|
|
3
3
|
declare module './index.ts' {
|
|
4
4
|
interface StringSchemaInterface {
|
package/dist/string.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import'./chunk-
|
|
1
|
+
import'./chunk-YUL3WJPU.mjs';export{c as extend,b as hookOriginal,a as s,a as schema}from'./chunk-QQOSS5DZ.mjs';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@esmj/schema",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.3",
|
|
4
4
|
"description": "Tiny extendable package for schema validation.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"schema",
|
|
@@ -113,7 +113,7 @@
|
|
|
113
113
|
"joi": "^17.13.3",
|
|
114
114
|
"lint-staged": "^16.1.6",
|
|
115
115
|
"superstruct": "^2.0.2",
|
|
116
|
-
"tsup": "^8.5.
|
|
116
|
+
"tsup": "^8.5.1",
|
|
117
117
|
"yup": "^1.7.0",
|
|
118
118
|
"zod": "^3.25.42"
|
|
119
119
|
}
|
package/dist/chunk-3C2I2ZOZ.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import {c}from'./chunk-QIW3OXV4.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/chunk-G6KRMWVT.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import {c}from'./chunk-QIW3OXV4.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});
|
package/dist/chunk-GJNOESJT.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import {c}from'./chunk-QIW3OXV4.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});
|
package/dist/chunk-QIW3OXV4.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
var E={abortEarly:true};function b(e,t){if(!t)return e;let n=e?.cause?.key?`${t}.${e.cause.key}`:`${t}`;return {message:`Error parsing key "${n}": ${e.message}`,cause:{key:n}}}function w(e,t,n){if(e.errors?.length)for(let a=0;a<e.errors.length;a++)t.push(b(e.errors[a],n));}function T(e){return {...E,...e}}var d=e=>typeof e=="string"||e instanceof String,y=e=>(typeof e=="number"||e instanceof Number)&&!Number.isNaN(e),g=e=>e===true||e===false,D=e=>e instanceof Date&&!Number.isNaN(e.getTime()),_=e=>Array.isArray(e),x=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),m={object(e,t){let n=l(x,{...t,type:"object"});return n._getDescription=()=>`object({ ${Object.entries(e).map(([p,o])=>`${p}: ${o._getDescription()}`).join(", ")} })`,h(n,"_parse",(a,p,o)=>{let r=a(p,o),{abortEarly:s}=T(o);if(r.success===false)return r;let u={},c=[];for(let i in e){let f=e[i]._parse(r.data[i],o);if(f.success)u[i]=f.data;else {if(f=f,s!==false){let I=b(f.error,i);return {success:false,error:I,errors:[I]}}w(f,c,i);}}return c.length>0?{success:false,error:c[0],errors:c}:{success:true,data:u}}),n},string(e){return l(d,{...e,type:"string"})},number(e){return l(y,{...e,type:"number"})},boolean(e){return l(g,{...e,type:"boolean"})},date(e){return l(D,{...e,type:"date"})},enum(e,t){let n=r=>e.includes(r),a=r=>`Invalid ${p} value. Expected ${e.map(s=>`"${s}"`).join(" | ")}, received "${r}".`,p="enum",o=l(n,{message:a,...t,type:p});return o._getDescription=()=>`enum(${e.map(r=>`"${r}"`).join(" | ")})`,o},array(e,t){let n=l(_,{...t,type:"array"});return n._getDescription=()=>`array(${e._getDescription()})`,h(n,"_parse",(a,p,o)=>{let r=a(p,o),{abortEarly:s}=T(o);if(r.success===false)return r;let u=[],c=[];for(let i=0;i<r.data.length;i++){let f=e._parse(r.data[i],o);if(f.success)u.push(f.data);else {if(f=f,s!==false){let I=b(f.error,i);return {success:false,error:I,errors:[I]}}w(f,c,i);}}return c.length>0?{success:false,error:c[0],errors:c}:{success:true,data:u}}),n},any(){return l(()=>true)},preprocess(e,t){return h(t,"_parse",(n,a)=>(a=e(a),n(a))),t},union(e,t){return l(o=>{for(let r=0;r<e.length;r++){let s=e[r]._parse(o);if(s.success)return s}return false},{message:o=>`Invalid union value. Expected the value to match one of the schemas:${e.map((r,s)=>` ${s+1}. ${r._getDescription()}`).join(",")} but received "${typeof o}" with value: ${x(o)?JSON.stringify(o):`"${o}"`}`,...t,type:"union"})},literal(e,t){let p=l(o=>o===e,{message:o=>t?.message?typeof t.message=="function"?t.message(o):t.message:`Expected literal value "${e}", received "${o}"`,name:t?.name,type:"literal"});return p._getDescription=()=>`literal("${e}")`,p},coerce:{string(e){return m.preprocess(t=>String(t),m.string(e))},number(e){let t=e?.message??(n=>`Cannot coerce "${n}" to a valid number.`);return m.preprocess(n=>Number(n),m.number({...e,message:t}))},boolean(e){return m.preprocess(t=>!!t,m.boolean(e))},date(e){let t=e?.message??(n=>`Cannot coerce "${n}" to a valid date.`);return m.preprocess(n=>new Date(n),m.date({...e,message:t}))}},cast:{boolean(e){let t=e?.message??(n=>`Cannot cast "${n}" to boolean. Accepted: true/false, 1/0, yes/no, on/off.`);return m.preprocess(n=>{let a;return d(n)&&(a=n.toLowerCase()),a==="true"||a==="yes"||a==="on"||a==="1"||n===1?true:a==="false"||a==="no"||a==="off"||a==="0"||n===0?false:n},m.boolean({...e,message:t}))},number(e){let t=e?.message??(n=>`Cannot cast "${n}" to a number. Expected a numeric string or number.`);return m.preprocess(n=>{if(g(n))return Number(n);if(d(n)){let a=n.trim();if(a==="")return n;let p=Number(a);if(Number.isFinite(p))return p}return n},m.number({...e,message:t}))},string(e){let t=e?.message??(n=>`Cannot cast "${n}" to string. Expected a string, number, or boolean.`);return m.preprocess(n=>g(n)||y(n)&&Number.isFinite(n)?String(n):n,m.string({...e,message:t}))},date(e){let t=e?.message??(n=>`Cannot cast "${n}" to a valid date.`);return m.preprocess(n=>{let a;return d(n)&&(a=n.trim()),y(n)&&Number.isFinite(n)||a?new Date(a??n):n},m.date({...e,message:t}))},json(e,t){let n=t?.message??(a=>`Cannot parse "${a}" as JSON.`);return h(e,"_parse",(a,p)=>{if(d(p))try{p=JSON.parse(p);}catch{let o={message:typeof n=="function"?n(p):n};return {success:false,error:o,errors:[o]}}return a(p)}),e}}};function P(e){return t=>`The value "${t}" must be type of ${e} but is type of "${typeof t}".`}function h(e,t,n){let a=e[t];e[t]=(...p)=>n(a,...p);}function l(e,{type:t="any",name:n,message:a}={}){a=a||P(t);let p={name:n,message:a,type:t},o={_getName(){return n},_getType(){return t},_getDescription(){return this._getName()??this._getType()},_parse(r,s){let u=e(r);if(u===true)return {success:true,data:r};if(typeof u=="object"&&u?.success===true)return u;let c={message:typeof a=="function"?a(r):a};return {success:false,error:c,errors:[c]}},parse(r,s){let u=o._parse(r,s);if(!u.success)throw u=u,new Error(u.error.message,{cause:u.error.cause});return u.data},safeParse(r,s){return o._parse(r,s)},transform(r){return h(this,"_parse",(s,u,c)=>{let i=s(u,c);return i.success&&(i.data=r(i.data)),i}),this},optional(){return h(this,"_parse",(r,s,u)=>{let c=r(s,u);return !c.success&&s===void 0&&(c.data=void 0,c.success=true),c}),this},nullable(){return h(this,"_parse",(r,s,u)=>{let c=r(s,u);return !c.success&&s===null&&(c.data=null,c.success=true),c}),this},nullish(){return h(this,"_parse",(r,s,u)=>{let c=r(s,u);return !c.success&&s==null&&(c.success=true,c.data=s),c}),this},default(r){return h(this,"_parse",(s,u,c)=>(u===void 0&&(u=typeof r=="function"?r():r),s(u,c))),this},catch(r){return h(this,"_parse",(s,u,c)=>{let i=s(u,c);return i.success?i:{success:true,data:typeof r=="function"?r({input:u,error:i.error}):r}}),this},pipe(r){return h(this,"_parse",(s,u,c)=>{let i=s(u,c);return i.success?r._parse(i.data,c):i}),this},refine(r,{message:s,type:u}={}){return u&&(s=s||P(u),t=u),h(this,"_parse",(c,i,f)=>{let I=c(i,f);T(f);if(!I.success)return I;let S=r(I.data);if(S===true||typeof S=="object"&&S?.success===true)return I;let k={message:typeof s=="function"?s(i):s};return {success:false,error:k,errors:[k]}}),this}};return O.length>0?O.reduce((r,s)=>s(r,e,p)??r,o):o}var O=[];function A(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");O.push(e);}export{m as a,h as b,A as c};
|