@esmj/schema 0.7.3 → 0.7.4
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 +78 -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-37YJYCTR.mjs +1 -0
- package/dist/{chunk-YYPP27RX.mjs → chunk-5XWCCG75.mjs} +1 -1
- package/dist/{chunk-CLEFU3BA.mjs → chunk-NCRS6NBV.mjs} +1 -1
- package/dist/{chunk-YUL3WJPU.mjs → chunk-NRGPACGD.mjs} +1 -1
- 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 +37 -262
- package/dist/index.d.ts +37 -262
- 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 +15 -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,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 $(e,a,n){if(e.errors?.length)for(let r=0;r<e.errors.length;r++)a.push(O(e.errors[r],n));}function T(e){return e?.abortEarly!==void 0?e:R}var l=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),_=e=>typeof e=="object"&&e!==null&&!Array.isArray(e);function C(e,a){let n=h(_,{...a,type:"object"});return n._getDescription=()=>`object({ ${Object.entries(e).map(([s,c])=>`${s}: ${c._getDescription()}`).join(", ")} })`,p(n,"_parse",(r,s,c)=>{let t=r(s,c),{abortEarly:u}=T(c);if(t.success===false)return t;let i={},o=[];for(let f in e){let m=e[f]._parse(t.data[f],c);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]}}$(m,o,f);}}return o.length>0?{success:false,error:o[0],errors:o}:{success:true,data:i}}),n}function k(e){return h(l,{...e,type:"string"})}function w(e){return h(y,{...e,type:"number"})}function x(e){return h(b,{...e,type:"boolean"})}function N(e){return h(A,{...e,type:"date"})}function V(e){return h(j,{...e,type:"function"})}function B(e,a){let n=t=>e.includes(t),r=t=>`Invalid ${s} value. Expected ${e.map(u=>`"${u}"`).join(" | ")}, received "${t}".`,s="enum",c=h(n,{message:r,...a,type:s});return c._getDescription=()=>`enum(${e.map(t=>`"${t}"`).join(" | ")})`,c}function L(e,a){let n=h(F,{...a,type:"array"});return n._getDescription=()=>`array(${e._getDescription()})`,p(n,"_parse",(r,s,c)=>{let t=r(s,c),{abortEarly:u}=T(c);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],c);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]}}$(m,o,f);}}return o.length>0?{success:false,error:o[0],errors:o}:{success:true,data:i}}),n}function M(){return h(()=>true)}function S(e,a){return p(a,"_parse",(n,r)=>(r=e(r),n(r))),a}function v(e,a){return h(c=>{for(let t=0;t<e.length;t++){let u=e[t]._parse(c);if(u.success)return u}return false},{message:c=>`Invalid union value. Expected the value to match one of the schemas:${e.map((t,u)=>` ${u+1}. ${t._getDescription()}`).join(",")} but received "${typeof c}" with value: ${_(c)?JSON.stringify(c):`"${c}"`}`,...a,type:"union"})}function U(e,a){let s=h(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 s._getDescription=()=>`literal("${e}")`,s}var q={string(e){return S(a=>String(a),k(e))},number(e){let a=e?.message??(n=>`Cannot coerce "${n}" to a valid number.`);return S(n=>Number(n),w({...e,message:a}))},boolean(e){return S(a=>!!a,x(e))},date(e){let a=e?.message??(n=>`Cannot coerce "${n}" to a valid date.`);return S(n=>new Date(n),N({...e,message:a}))}},J={boolean(e){let a=e?.message??(n=>`Cannot cast "${n}" to boolean. Accepted: true/false, 1/0, yes/no, on/off.`);return S(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},x({...e,message:a}))},number(e){let a=e?.message??(n=>`Cannot cast "${n}" to a number. Expected a numeric string or number.`);return S(n=>{if(b(n))return Number(n);if(l(n)){let r=n.trim();if(r==="")return n;let s=Number(r);if(Number.isFinite(s))return s}return n},w({...e,message:a}))},string(e){let a=e?.message??(n=>`Cannot cast "${n}" to string. Expected a string, number, or boolean.`);return S(n=>b(n)||y(n)&&Number.isFinite(n)?String(n):n,k({...e,message:a}))},date(e){let a=e?.message??(n=>`Cannot cast "${n}" to a valid date.`);return S(n=>{let r;return l(n)&&(r=n.trim()),y(n)&&Number.isFinite(n)||r?new Date(r??n):n},N({...e,message:a}))},json(e,a){let n=a?.message??(r=>`Cannot parse "${r}" as JSON.`);return p(e,"_parse",(r,s)=>{if(l(s))try{s=JSON.parse(s);}catch{let c={message:typeof n=="function"?n(s):n};return {success:false,error:c,errors:[c]}}return r(s)}),e}},H={object:C,string:k,number:w,boolean:x,date:N,function:V,enum:B,array:L,any:M,preprocess:S,union:v,literal:U,coerce:q,cast:J};function E(e){return a=>`The value "${a}" must be type of ${e} but is type of "${typeof a}".`}function p(e,a,n){let r=e[a];e[a]=(...s)=>n(r,...s);}function h(e,{type:a="any",name:n,message:r}={}){r=r||E(a);let s={name:n,message:r,type:a},c={_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=c._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 c._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||E(i),a=i),p(this,"_parse",(o,f,m)=>{let I=o(f,m),{abortEarly:z}=T(m);if(!I.success)return I;let d=t(I.data);if(d===true||typeof d=="object"&&d?.success===true)return I;let P={message:typeof u=="function"?u(f):u};return {success:false,error:P,errors:[P]}}),this}};return g.length>0?g.reduce((t,u)=>u(t,e,s)??t,c):c}var g=[];function D(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");g.push(e);}D((e,a,n)=>{if(n?.type==="number"){let r=e;r.min=function(s,{message:c}={}){return this.refine(t=>t>=s,{message:c||`Number must be greater than or equal to ${s}.`})},r.max=function(s,{message:c}={}){return this.refine(t=>t<=s,{message:c||`Number must be less than or equal to ${s}.`})},r.positive=function({message:s}={}){return this.refine(c=>c>0,{message:s||"Number must be positive."})},r.negative=function({message:s}={}){return this.refine(c=>c<0,{message:s||"Number must be negative."})},r.int=function({message:s}={}){return this.refine(c=>Number.isInteger(c),{message:s||"Number must be an integer."})},r.float=function({message:s}={}){return this.refine(c=>Number.isFinite(c)&&!Number.isInteger(c),{message:s||"Number must be a floating point (non-integer)."})},r.multipleOf=function(s,{message:c}={}){return this.refine(t=>t%s===0,{message:c||`Number must be a multiple of ${s}.`})},r.finite=function({message:s}={}){return this.refine(c=>Number.isFinite(c),{message:s||"Number must be finite."})};}return e});exports.any=M;exports.array=L;exports.boolean=x;exports.cast=J;exports.coerce=q;exports.date=N;exports.enumSchema=B;exports.extend=D;exports.functionSchema=V;exports.hookOriginal=p;exports.literal=U;exports.number=w;exports.object=C;exports.preprocess=S;exports.s=H;exports.schema=H;exports.string=k;exports.union=v;
|
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, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, any, array, boolean, cast, coerce, 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, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, any, array, boolean, cast, coerce, 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-NCRS6NBV.mjs';export{i as any,h as array,d as boolean,n as cast,m as coerce,e as date,g as enumSchema,q as extend,f as functionSchema,p as hookOriginal,l as literal,c as number,a as object,j as preprocess,o as s,o as schema,b as string,k as union}from'./chunk-37YJYCTR.mjs';
|
package/dist/string.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var
|
|
1
|
+
'use strict';var R={abortEarly:true};function O(e,c){if(!c)return e;let t=e?.cause?.key?`${c}.${e.cause.key}`:`${c}`;return {message:`Error parsing key "${t}": ${e.message}`,cause:{key:t}}}function _(e,c,t){if(e.errors?.length)for(let r=0;r<e.errors.length;r++)c.push(O(e.errors[r],t));}function T(e){return e?.abortEarly!==void 0?e:R}var l=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),D=e=>typeof e=="object"&&e!==null&&!Array.isArray(e);function F(e,c){let t=h(D,{...c,type:"object"});return t._getDescription=()=>`object({ ${Object.entries(e).map(([a,s])=>`${a}: ${s._getDescription()}`).join(", ")} })`,p(t,"_parse",(r,a,s)=>{let n=r(a,s),{abortEarly:o}=T(s);if(n.success===false)return n;let i={},u=[];for(let f in e){let m=e[f]._parse(n.data[f],s);if(m.success)i[f]=m.data;else {if(m=m,o!==false){let S=O(m.error,f);return {success:false,error:S,errors:[S]}}_(m,u,f);}}return u.length>0?{success:false,error:u[0],errors:u}:{success:true,data:i}}),t}function w(e){return h(l,{...e,type:"string"})}function k(e){return h(g,{...e,type:"number"})}function x(e){return h(y,{...e,type:"boolean"})}function P(e){return h(A,{...e,type:"date"})}function V(e){return h(j,{...e,type:"function"})}function L(e,c){let t=n=>e.includes(n),r=n=>`Invalid ${a} value. Expected ${e.map(o=>`"${o}"`).join(" | ")}, received "${n}".`,a="enum",s=h(t,{message:r,...c,type:a});return s._getDescription=()=>`enum(${e.map(n=>`"${n}"`).join(" | ")})`,s}function B(e,c){let t=h(C,{...c,type:"array"});return t._getDescription=()=>`array(${e._getDescription()})`,p(t,"_parse",(r,a,s)=>{let n=r(a,s),{abortEarly:o}=T(s);if(n.success===false)return n;let i=[],u=[];for(let f=0;f<n.data.length;f++){let m=e._parse(n.data[f],s);if(m.success)i.push(m.data);else {if(m=m,o!==false){let S=O(m.error,f);return {success:false,error:S,errors:[S]}}_(m,u,f);}}return u.length>0?{success:false,error:u[0],errors:u}:{success:true,data:i}}),t}function M(){return h(()=>true)}function I(e,c){return p(c,"_parse",(t,r)=>(r=e(r),t(r))),c}function U(e,c){return h(s=>{for(let n=0;n<e.length;n++){let o=e[n]._parse(s);if(o.success)return o}return false},{message:s=>`Invalid union value. Expected the value to match one of the schemas:${e.map((n,o)=>` ${o+1}. ${n._getDescription()}`).join(",")} but received "${typeof s}" with value: ${D(s)?JSON.stringify(s):`"${s}"`}`,...c,type:"union"})}function W(e,c){let a=h(s=>s===e,{message:s=>c?.message?typeof c.message=="function"?c.message(s):c.message:`Expected literal value "${e}", received "${s}"`,name:c?.name,type:"literal"});return a._getDescription=()=>`literal("${e}")`,a}var v={string(e){return I(c=>String(c),w(e))},number(e){let c=e?.message??(t=>`Cannot coerce "${t}" to a valid number.`);return I(t=>Number(t),k({...e,message:c}))},boolean(e){return I(c=>!!c,x(e))},date(e){let c=e?.message??(t=>`Cannot coerce "${t}" to a valid date.`);return I(t=>new Date(t),P({...e,message:c}))}},J={boolean(e){let c=e?.message??(t=>`Cannot cast "${t}" to boolean. Accepted: true/false, 1/0, yes/no, on/off.`);return I(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},x({...e,message:c}))},number(e){let c=e?.message??(t=>`Cannot cast "${t}" to a number. Expected a numeric string or number.`);return I(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},k({...e,message:c}))},string(e){let c=e?.message??(t=>`Cannot cast "${t}" to string. Expected a string, number, or boolean.`);return I(t=>y(t)||g(t)&&Number.isFinite(t)?String(t):t,w({...e,message:c}))},date(e){let c=e?.message??(t=>`Cannot cast "${t}" to a valid date.`);return I(t=>{let r;return l(t)&&(r=t.trim()),g(t)&&Number.isFinite(t)||r?new Date(r??t):t},P({...e,message:c}))},json(e,c){let t=c?.message??(r=>`Cannot parse "${r}" as JSON.`);return p(e,"_parse",(r,a)=>{if(l(a))try{a=JSON.parse(a);}catch{let s={message:typeof t=="function"?t(a):t};return {success:false,error:s,errors:[s]}}return r(a)}),e}},G={object:F,string:w,number:k,boolean:x,date:P,function:V,enum:L,array:B,any:M,preprocess:I,union:U,literal:W,coerce:v,cast:J};function E(e){return c=>`The value "${c}" must be type of ${e} but is type of "${typeof c}".`}function p(e,c,t){let r=e[c];e[c]=(...a)=>t(r,...a);}function h(e,{type:c="any",name:t,message:r}={}){r=r||E(c);let a={name:t,message:r,type:c},s={_getName(){return t},_getType(){return c},_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=s._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 s._parse(n,o)},transform(n){return p(this,"_parse",(o,i,u)=>{let f=o(i,u);return f.success&&(f.data=n(f.data)),f}),this},optional(){return p(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 p(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 p(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 p(this,"_parse",(o,i,u)=>(i===void 0&&(i=typeof n=="function"?n():n),o(i,u))),this},catch(n){return p(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 p(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||E(i),c=i),p(this,"_parse",(u,f,m)=>{let S=u(f,m),{abortEarly:q}=T(m);if(!S.success)return S;let d=n(S.data);if(d===true||typeof d=="object"&&d?.success===true)return S;let $={message:typeof o=="function"?o(f):o};return {success:false,error:$,errors:[$]}}),this}};return b.length>0?b.reduce((n,o)=>o(n,e,a)??n,s):s}var b=[];function N(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");b.push(e);}N((e,c,t)=>{if(t?.type==="string"){let r=e;r.min=function(a,{message:s}={}){return this.refine(n=>n.length>=a,{message:s||(n=>`String must be at least ${a} characters long (received ${n.length} characters: "${n}")`)})},r.max=function(a,{message:s}={}){return this.refine(n=>n.length<=a,{message:s||(n=>`String must be at most ${a} characters long (received ${n.length} characters: "${n}")`)})},r.length=function(a,{message:s}={}){return this.refine(n=>n.length===a,{message:s||(n=>`String must be exactly ${a} characters long (received ${n.length} characters: "${n}")`)})},r.nonEmpty=function({message:a}={}){return this.refine(s=>s.length>0,{message:a||"String must not be empty (received empty string)"})},r.startsWith=function(a,{message:s}={}){return this.refine(n=>n.startsWith(a),{message:s||(n=>`String must start with "${a}" (received: "${n}")`)})},r.endsWith=function(a,{message:s}={}){return this.refine(n=>n.endsWith(a),{message:s||(n=>`String must end with "${a}" (received: "${n}")`)})},r.includes=function(a,{message:s}={}){return this.refine(n=>n.includes(a),{message:s||(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,s=" "){return this.transform(n=>n.padStart(a,s))},r.padEnd=function(a,s=" "){return this.transform(n=>n.padEnd(a,s))},r.replace=function(a,s){return this.transform(n=>n.replace(a,s))};}return e});exports.any=M;exports.array=B;exports.boolean=x;exports.cast=J;exports.coerce=v;exports.date=P;exports.enumSchema=L;exports.extend=N;exports.functionSchema=V;exports.hookOriginal=p;exports.literal=W;exports.number=k;exports.object=F;exports.preprocess=I;exports.s=G;exports.schema=G;exports.string=w;exports.union=U;
|
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, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, any, array, boolean, cast, coerce, 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, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, any, array, boolean, cast, coerce, 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-NRGPACGD.mjs';export{i as any,h as array,d as boolean,n as cast,m as coerce,e as date,g as enumSchema,q as extend,f as functionSchema,p as hookOriginal,l as literal,c as number,a as object,j as preprocess,o as s,o as schema,b as string,k as union}from'./chunk-37YJYCTR.mjs';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@esmj/schema",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.4",
|
|
4
4
|
"description": "Tiny extendable package for schema validation.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"schema",
|
|
@@ -49,10 +49,18 @@
|
|
|
49
49
|
}
|
|
50
50
|
},
|
|
51
51
|
"sideEffects": [
|
|
52
|
-
"src/string.ts",
|
|
53
|
-
"src/number.ts",
|
|
54
|
-
"src/array.ts",
|
|
55
|
-
"src/full.ts"
|
|
52
|
+
"./src/string.ts",
|
|
53
|
+
"./src/number.ts",
|
|
54
|
+
"./src/array.ts",
|
|
55
|
+
"./src/full.ts",
|
|
56
|
+
"./dist/string.mjs",
|
|
57
|
+
"./dist/string.cjs",
|
|
58
|
+
"./dist/number.mjs",
|
|
59
|
+
"./dist/number.cjs",
|
|
60
|
+
"./dist/array.mjs",
|
|
61
|
+
"./dist/array.cjs",
|
|
62
|
+
"./dist/full.mjs",
|
|
63
|
+
"./dist/full.cjs"
|
|
56
64
|
],
|
|
57
65
|
"typings": "dist/index.d.ts",
|
|
58
66
|
"scripts": {
|
|
@@ -99,6 +107,8 @@
|
|
|
99
107
|
"homepage": "https://github.com/mjancarik/esmj-schema#readme",
|
|
100
108
|
"devDependencies": {
|
|
101
109
|
"@biomejs/biome": "1.9.4",
|
|
110
|
+
"ajv": "^8.17.1",
|
|
111
|
+
"ajv-keywords": "^5.1.0",
|
|
102
112
|
"@commitlint/cli": "^19.8.1",
|
|
103
113
|
"@commitlint/config-conventional": "^19.8.1",
|
|
104
114
|
"@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};
|