@esmj/schema 0.7.3 → 0.8.0
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 +127 -21
- 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-YUL3WJPU.mjs → chunk-7VTVE334.mjs} +1 -1
- package/dist/chunk-AKXLQP64.mjs +1 -0
- package/dist/{chunk-YYPP27RX.mjs → chunk-AQFELD23.mjs} +1 -1
- package/dist/{chunk-CLEFU3BA.mjs → chunk-HVQY2UKM.mjs} +1 -1
- package/dist/chunk-SR7R5QYC.mjs +1 -0
- package/dist/coerce.cjs +1 -0
- package/dist/coerce.d.mts +27 -0
- package/dist/coerce.d.ts +27 -0
- package/dist/coerce.mjs +1 -0
- package/dist/full.cjs +1 -1
- package/dist/full.d.mts +2 -1
- package/dist/full.d.ts +2 -1
- package/dist/full.mjs +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.mts +36 -270
- package/dist/index.d.ts +36 -270
- 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 +23 -5
- package/dist/chunk-QQOSS5DZ.mjs +0 -1
package/dist/number.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var
|
|
1
|
+
'use strict';var R={abortEarly:true};function O(e,c){if(!c)return e;let n=e?.cause?.key?`${c}.${e.cause.key}`:`${c}`;return {message:`Error parsing key "${n}": ${e.message}`,cause:{key:n}}}function x(e,c,n){if(e.errors?.length)for(let r=0;r<e.errors.length;r++)c.push(O(e.errors[r],n));}function T(e){return e?.abortEarly!==void 0?e:R}var S=e=>typeof e=="string"||e instanceof String,y=e=>(typeof e=="number"||e instanceof Number)&&!Number.isNaN(e),b=e=>e===true||e===false,A=e=>e instanceof Date&&!Number.isNaN(e.getTime()),j=e=>typeof e=="function",F=e=>Array.isArray(e),N=e=>typeof e=="object"&&e!==null&&!Array.isArray(e);function C(e,c){let n=h(N,{...c,type:"object"});return n._getDescription=()=>`object({ ${Object.entries(e).map(([s,a])=>`${s}: ${a._getDescription()}`).join(", ")} })`,p(n,"_parse",(r,s,a)=>{let t=r(s,a),{abortEarly:u}=T(a);if(t.success===false)return t;let i={},o=[];for(let f in e){let m=e[f]._parse(t.data[f],a);if(m.success)i[f]=m.data;else {if(m=m,u!==false){let I=O(m.error,f);return {success:false,error:I,errors:[I]}}x(m,o,f);}}return o.length>0?{success:false,error:o[0],errors:o}:{success:true,data:i}}),n}function P(e){return h(S,{...e,type:"string"})}function E(e){return h(y,{...e,type:"number"})}function _(e){return h(b,{...e,type:"boolean"})}function $(e){return h(A,{...e,type:"date"})}function V(e){return h(j,{...e,type:"function"})}function L(e,c){let n=t=>e.includes(t),r=t=>`Invalid ${s} value. Expected ${e.map(u=>`"${u}"`).join(" | ")}, received "${t}".`,s="enum",a=h(n,{message:r,...c,type:s});return a._getDescription=()=>`enum(${e.map(t=>`"${t}"`).join(" | ")})`,a}function M(e,c){let n=h(F,{...c,type:"array"});return n._getDescription=()=>`array(${e._getDescription()})`,p(n,"_parse",(r,s,a)=>{let t=r(s,a),{abortEarly:u}=T(a);if(t.success===false)return t;let i=[],o=[];for(let f=0;f<t.data.length;f++){let m=e._parse(t.data[f],a);if(m.success)i.push(m.data);else {if(m=m,u!==false){let I=O(m.error,f);return {success:false,error:I,errors:[I]}}x(m,o,f);}}return o.length>0?{success:false,error:o[0],errors:o}:{success:true,data:i}}),n}function B(){return h(()=>true)}function l(e,c){return p(c,"_parse",(n,r)=>(r=e(r),n(r))),c}function U(e,c){return h(a=>{for(let t=0;t<e.length;t++){let u=e[t]._parse(a);if(u.success)return u}return false},{message:a=>`Invalid union value. Expected the value to match one of the schemas:${e.map((t,u)=>` ${u+1}. ${t._getDescription()}`).join(",")} but received "${typeof a}" with value: ${N(a)?JSON.stringify(a):`"${a}"`}`,...c,type:"union"})}function q(e,c){let s=h(a=>a===e,{message:a=>c?.message?typeof c.message=="function"?c.message(a):c.message:`Expected literal value "${e}", received "${a}"`,name:c?.name,type:"literal"});return s._getDescription=()=>`literal("${e}")`,s}var v={boolean(e){let c=e?.message??(n=>`Cannot cast "${n}" to boolean. Accepted: true/false, 1/0, yes/no, on/off.`);return l(n=>{let r;return S(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},_({...e,message:c}))},number(e){let c=e?.message??(n=>`Cannot cast "${n}" to a number. Expected a numeric string or number.`);return l(n=>{if(b(n))return Number(n);if(S(n)){let r=n.trim();if(r==="")return n;let s=Number(r);if(Number.isFinite(s))return s}return n},E({...e,message:c}))},string(e){let c=e?.message??(n=>`Cannot cast "${n}" to string. Expected a string, number, or boolean.`);return l(n=>b(n)||y(n)&&Number.isFinite(n)?String(n):n,P({...e,message:c}))},date(e){let c=e?.message??(n=>`Cannot cast "${n}" to a valid date.`);return l(n=>{let r;return S(n)&&(r=n.trim()),y(n)&&Number.isFinite(n)||r?new Date(r??n):n},$({...e,message:c}))},json(e,c){let n=c?.message??(r=>`Cannot parse "${r}" as JSON.`);return p(e,"_parse",(r,s)=>{if(S(s))try{s=JSON.parse(s);}catch{let a={message:typeof n=="function"?n(s):n};return {success:false,error:a,errors:[a]}}return r(s)}),e}},G={object:C,string:P,number:E,boolean:_,date:$,function:V,enum:L,array:M,any:B,preprocess:l,union:U,literal:q,coerce:{},cast:v};function w(e){return c=>`The value "${c}" must be type of ${e} but is type of "${typeof c}".`}function p(e,c,n){let r=e[c];e[c]=(...s)=>n(r,...s);}function h(e,{type:c="any",name:n,message:r}={}){r=r||w(c);let s={name:n,message:r,type:c},a={_getName(){return n},_getType(){return c},_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=a._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 a._parse(t,u)},transform(t){return p(this,"_parse",(u,i,o)=>{let f=u(i,o);return f.success&&(f.data=t(f.data)),f}),this},optional(){return p(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 p(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 p(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 p(this,"_parse",(u,i,o)=>(i===void 0&&(i=typeof t=="function"?t():t),u(i,o))),this},catch(t){return p(this,"_parse",(u,i,o)=>{let f=u(i,o);return f.success?f:{success:true,data:typeof t=="function"?t({input:i,error:f.error}):t}}),this},pipe(t){return p(this,"_parse",(u,i,o)=>{let f=u(i,o);return f.success?t._parse(f.data,o):f}),this},refine(t,{message:u,type:i}={}){return i&&(u=u||w(i),c=i),p(this,"_parse",(o,f,m)=>{let I=o(f,m),{abortEarly:J}=T(m);if(!I.success)return I;let d=t(I.data);if(d===true||typeof d=="object"&&d?.success===true)return I;let k={message:typeof u=="function"?u(f):u};return {success:false,error:k,errors:[k]}}),this}};return g.length>0?g.reduce((t,u)=>u(t,e,s)??t,a):a}var g=[];function D(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");g.push(e);}D((e,c,n)=>{if(n?.type==="number"){let r=e;r.min=function(s,{message:a}={}){return this.refine(t=>t>=s,{message:a||`Number must be greater than or equal to ${s}.`})},r.max=function(s,{message:a}={}){return this.refine(t=>t<=s,{message:a||`Number must be less than or equal to ${s}.`})},r.positive=function({message:s}={}){return this.refine(a=>a>0,{message:s||"Number must be positive."})},r.negative=function({message:s}={}){return this.refine(a=>a<0,{message:s||"Number must be negative."})},r.int=function({message:s}={}){return this.refine(a=>Number.isInteger(a),{message:s||"Number must be an integer."})},r.float=function({message:s}={}){return this.refine(a=>Number.isFinite(a)&&!Number.isInteger(a),{message:s||"Number must be a floating point (non-integer)."})},r.multipleOf=function(s,{message:a}={}){return this.refine(t=>t%s===0,{message:a||`Number must be a multiple of ${s}.`})},r.finite=function({message:s}={}){return this.refine(a=>Number.isFinite(a),{message:s||"Number must be finite."})};}return e});exports.any=B;exports.array=M;exports.boolean=_;exports.cast=v;exports.date=$;exports.enumSchema=L;exports.extend=D;exports.functionSchema=V;exports.hookOriginal=p;exports.literal=q;exports.number=E;exports.object=C;exports.preprocess=l;exports.s=G;exports.schema=G;exports.string=P;exports.union=U;
|
package/dist/number.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
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';
|
|
1
|
+
export { ArraySchemaInterface, BooleanSchemaInterface, CoerceInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, any, array, boolean, cast, date, enumSchema, extend, functionSchema, hookOriginal, literal, number, object, preprocess, s, s as schema, string, union } 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, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s, s as schema } from './index.js';
|
|
1
|
+
export { ArraySchemaInterface, BooleanSchemaInterface, CoerceInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, any, array, boolean, cast, date, enumSchema, extend, functionSchema, hookOriginal, literal, number, object, preprocess, s, s as schema, string, union } 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-HVQY2UKM.mjs';export{i as any,h as array,d as boolean,m as cast,e as date,g as enumSchema,p as extend,f as functionSchema,o as hookOriginal,l as literal,c as number,a as object,j as preprocess,n as s,n as schema,b as string,k as union}from'./chunk-AKXLQP64.mjs';
|
package/dist/string.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var
|
|
1
|
+
'use strict';var R={abortEarly:true};function O(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 x(e,s,t){if(e.errors?.length)for(let r=0;r<e.errors.length;r++)s.push(O(e.errors[r],t));}function T(e){return e?.abortEarly!==void 0?e:R}var I=e=>typeof e=="string"||e instanceof String,g=e=>(typeof e=="number"||e instanceof Number)&&!Number.isNaN(e),y=e=>e===true||e===false,A=e=>e instanceof Date&&!Number.isNaN(e.getTime()),j=e=>typeof e=="function",C=e=>Array.isArray(e),P=e=>typeof e=="object"&&e!==null&&!Array.isArray(e);function F(e,s){let t=h(P,{...s,type:"object"});return t._getDescription=()=>`object({ ${Object.entries(e).map(([a,c])=>`${a}: ${c._getDescription()}`).join(", ")} })`,m(t,"_parse",(r,a,c)=>{let n=r(a,c),{abortEarly:o}=T(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 S=O(p.error,f);return {success:false,error:S,errors:[S]}}x(p,u,f);}}return u.length>0?{success:false,error:u[0],errors:u}:{success:true,data:i}}),t}function $(e){return h(I,{...e,type:"string"})}function E(e){return h(g,{...e,type:"number"})}function _(e){return h(y,{...e,type:"boolean"})}function D(e){return h(A,{...e,type:"date"})}function V(e){return h(j,{...e,type:"function"})}function L(e,s){let t=n=>e.includes(n),r=n=>`Invalid ${a} value. Expected ${e.map(o=>`"${o}"`).join(" | ")}, received "${n}".`,a="enum",c=h(t,{message:r,...s,type:a});return c._getDescription=()=>`enum(${e.map(n=>`"${n}"`).join(" | ")})`,c}function M(e,s){let t=h(C,{...s,type:"array"});return t._getDescription=()=>`array(${e._getDescription()})`,m(t,"_parse",(r,a,c)=>{let n=r(a,c),{abortEarly:o}=T(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 S=O(p.error,f);return {success:false,error:S,errors:[S]}}x(p,u,f);}}return u.length>0?{success:false,error:u[0],errors:u}:{success:true,data:i}}),t}function U(){return h(()=>true)}function l(e,s){return m(s,"_parse",(t,r)=>(r=e(r),t(r))),s}function B(e,s){return h(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: ${P(c)?JSON.stringify(c):`"${c}"`}`,...s,type:"union"})}function W(e,s){let a=h(c=>c===e,{message:c=>s?.message?typeof s.message=="function"?s.message(c):s.message:`Expected literal value "${e}", received "${c}"`,name:s?.name,type:"literal"});return a._getDescription=()=>`literal("${e}")`,a}var J={boolean(e){let s=e?.message??(t=>`Cannot cast "${t}" to boolean. Accepted: true/false, 1/0, yes/no, on/off.`);return l(t=>{let r;return I(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},_({...e,message:s}))},number(e){let s=e?.message??(t=>`Cannot cast "${t}" to a number. Expected a numeric string or number.`);return l(t=>{if(y(t))return Number(t);if(I(t)){let r=t.trim();if(r==="")return t;let a=Number(r);if(Number.isFinite(a))return a}return t},E({...e,message:s}))},string(e){let s=e?.message??(t=>`Cannot cast "${t}" to string. Expected a string, number, or boolean.`);return l(t=>y(t)||g(t)&&Number.isFinite(t)?String(t):t,$({...e,message:s}))},date(e){let s=e?.message??(t=>`Cannot cast "${t}" to a valid date.`);return l(t=>{let r;return I(t)&&(r=t.trim()),g(t)&&Number.isFinite(t)||r?new Date(r??t):t},D({...e,message:s}))},json(e,s){let t=s?.message??(r=>`Cannot parse "${r}" as JSON.`);return m(e,"_parse",(r,a)=>{if(I(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}},z={object:F,string:$,number:E,boolean:_,date:D,function:V,enum:L,array:M,any:U,preprocess:l,union:B,literal:W,coerce:{},cast:J};function k(e){return s=>`The value "${s}" must be type of ${e} but is type of "${typeof s}".`}function m(e,s,t){let r=e[s];e[s]=(...a)=>t(r,...a);}function h(e,{type:s="any",name:t,message:r}={}){r=r||k(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 m(this,"_parse",(o,i,u)=>{let f=o(i,u);return f.success&&(f.data=n(f.data)),f}),this},optional(){return m(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 m(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 m(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 m(this,"_parse",(o,i,u)=>(i===void 0&&(i=typeof n=="function"?n():n),o(i,u))),this},catch(n){return m(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 m(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||k(i),s=i),m(this,"_parse",(u,f,p)=>{let S=u(f,p),{abortEarly:q}=T(p);if(!S.success)return S;let d=n(S.data);if(d===true||typeof d=="object"&&d?.success===true)return S;let w={message:typeof o=="function"?o(f):o};return {success:false,error:w,errors:[w]}}),this}};return b.length>0?b.reduce((n,o)=>o(n,e,a)??n,c):c}var b=[];function N(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");b.push(e);}N((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.any=U;exports.array=M;exports.boolean=_;exports.cast=J;exports.date=D;exports.enumSchema=L;exports.extend=N;exports.functionSchema=V;exports.hookOriginal=m;exports.literal=W;exports.number=E;exports.object=F;exports.preprocess=l;exports.s=z;exports.schema=z;exports.string=$;exports.union=B;
|
package/dist/string.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
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';
|
|
1
|
+
export { ArraySchemaInterface, BooleanSchemaInterface, CoerceInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, any, array, boolean, cast, date, enumSchema, extend, functionSchema, hookOriginal, literal, number, object, preprocess, s, s as schema, string, union } 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, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s, s as schema } from './index.js';
|
|
1
|
+
export { ArraySchemaInterface, BooleanSchemaInterface, CoerceInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, any, array, boolean, cast, date, enumSchema, extend, functionSchema, hookOriginal, literal, number, object, preprocess, s, s as schema, string, union } 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-7VTVE334.mjs';export{i as any,h as array,d as boolean,m as cast,e as date,g as enumSchema,p as extend,f as functionSchema,o as hookOriginal,l as literal,c as number,a as object,j as preprocess,n as s,n as schema,b as string,k as union}from'./chunk-AKXLQP64.mjs';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@esmj/schema",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Tiny extendable package for schema validation.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"schema",
|
|
@@ -46,13 +46,29 @@
|
|
|
46
46
|
"types": "./dist/array.d.ts",
|
|
47
47
|
"import": "./dist/array.mjs",
|
|
48
48
|
"require": "./dist/array.cjs"
|
|
49
|
+
},
|
|
50
|
+
"./coerce": {
|
|
51
|
+
"types": "./dist/coerce.d.ts",
|
|
52
|
+
"import": "./dist/coerce.mjs",
|
|
53
|
+
"require": "./dist/coerce.cjs"
|
|
49
54
|
}
|
|
50
55
|
},
|
|
51
56
|
"sideEffects": [
|
|
52
|
-
"src/string.ts",
|
|
53
|
-
"src/number.ts",
|
|
54
|
-
"src/array.ts",
|
|
55
|
-
"src/full.ts"
|
|
57
|
+
"./src/string.ts",
|
|
58
|
+
"./src/number.ts",
|
|
59
|
+
"./src/array.ts",
|
|
60
|
+
"./src/full.ts",
|
|
61
|
+
"./dist/string.mjs",
|
|
62
|
+
"./dist/string.cjs",
|
|
63
|
+
"./dist/number.mjs",
|
|
64
|
+
"./dist/number.cjs",
|
|
65
|
+
"./dist/array.mjs",
|
|
66
|
+
"./dist/array.cjs",
|
|
67
|
+
"./dist/full.mjs",
|
|
68
|
+
"./dist/full.cjs",
|
|
69
|
+
"./src/coerce.ts",
|
|
70
|
+
"./dist/coerce.mjs",
|
|
71
|
+
"./dist/coerce.cjs"
|
|
56
72
|
],
|
|
57
73
|
"typings": "dist/index.d.ts",
|
|
58
74
|
"scripts": {
|
|
@@ -99,6 +115,8 @@
|
|
|
99
115
|
"homepage": "https://github.com/mjancarik/esmj-schema#readme",
|
|
100
116
|
"devDependencies": {
|
|
101
117
|
"@biomejs/biome": "1.9.4",
|
|
118
|
+
"ajv": "^8.17.1",
|
|
119
|
+
"ajv-keywords": "^5.1.0",
|
|
102
120
|
"@commitlint/cli": "^19.8.1",
|
|
103
121
|
"@commitlint/config-conventional": "^19.8.1",
|
|
104
122
|
"@sinclair/typebox": "^0.34.41",
|
package/dist/chunk-QQOSS5DZ.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 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};
|