@esmj/schema 0.4.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ 'use strict';var P={abortEarly:true};function S(e,o){if(!o)return e;let i=e?.cause?.key?`${o}.${e.cause.key}`:`${o}`;return {message:`Error parsing key "${i}": ${e.message}`,cause:{key:i}}}function g(e,o,i){!e.errors||e.errors.length===0||e.errors.forEach(u=>{let c=S(u,i);o.push(c);});}function d(e){return {...P,...e}}var N=e=>typeof e=="string"||e instanceof String,k=e=>typeof e=="number"||e instanceof Number,w=e=>e===true||e===false,E=e=>e instanceof Date&&!Number.isNaN(e.getTime()),_=e=>Array.isArray(e),O=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),A={object(e,o){let i=I(O,{...o,type:"object"});return i._getDescription=()=>`object({ ${Object.entries(e).map(([c,r])=>`${c}: ${r._getDescription()}`).join(", ")} })`,h(i,"_parse",(u,c,r)=>{let t=u(c,r),{abortEarly:n}=d(r);if(t.success===false)return t;let s={},a=[];for(let p in e){let f=e[p]._parse(t.data[p],r);if(f.success)s[p]=f.data;else {if(f=f,n!==false){let m=S(f.error,p);return {success:false,error:m,errors:[m]}}g(f,a,p);}}return a.length>0?{success:false,error:a[0],errors:a}:{success:true,data:s}}),i},string(e){return I(N,{...e,type:"string"})},number(e){return I(k,{...e,type:"number"})},boolean(e){return I(w,{...e,type:"boolean"})},date(e){return I(E,{...e,type:"date"})},enum(e,o){let i=t=>e.includes(t),u=t=>`Invalid ${c} value. Expected ${e.map(n=>`"${n}"`).join(" | ")}, received "${t}".`,c="enum",r=I(i,{message:u,...o,type:c});return r._getDescription=()=>`enum(${e.map(t=>`"${t}"`).join(" | ")})`,r},array(e,o){let i=I(_,{...o,type:"array"});return i._getDescription=()=>`array(${e._getDescription()})`,h(i,"_parse",(u,c,r)=>{let t=u(c,r),{abortEarly:n}=d(r);if(t.success===false)return t;let s=[],a=[];for(let p=0;p<t.data.length;p++){let f=e._parse(t.data[p],r);if(f.success)s.push(f.data);else {if(f=f,n!==false){let m=S(f.error,p);return {success:false,error:m,errors:[m]}}g(f,a,p);}}return a.length>0?{success:false,error:a[0],errors:a}:{success:true,data:s}}),i},any(){return I(()=>true)},preprocess(e,o){return h(o,"_parse",(i,u)=>(u=e(u),i(u))),o},union(e,o){return I(r=>{for(let t=0;t<e.length;t++){let n=e[t]._parse(r);if(n.success)return n}return false},{message:r=>`Invalid union value. Expected the value to match one of the schemas:${e.map((t,n)=>` ${n+1}. ${t._getDescription()}`).join(",")} but received "${typeof r}" with value: ${O(r)?JSON.stringify(r):`"${r}"`}`,...o,type:"union"})}};function T(e){return o=>`The value "${o}" must be type of ${e} but is type of "${typeof o}".`}function h(e,o,i){let u=e[o];e[o]=(...c)=>i(u,...c);}function I(e,{type:o="any",name:i,message:u}={}){u=u||T(o);let c={name:i,message:u,type:o},r={_getName(){return i},_getType(){return o},_getDescription(){return this._getName()??this._getType()},_parse(t,n){let s=e(t);if(s===true)return {success:true,data:t};if(typeof s=="object"&&s?.success===true)return s;let a={message:typeof u=="function"?u(t):u};return {success:false,error:a,errors:[a]}},parse(t,n){let s=r._parse(t,n);if(!s.success)throw s=s,new Error(s.error.message,{cause:s.error.cause});return s.data},safeParse(t,n){return r._parse(t,n)},transform(t){return h(this,"_parse",(n,s,a)=>{let p=n(s,a);return p.success&&(p.data=t(p.data)),p}),this},optional(){return h(this,"_parse",(t,n,s)=>{let a=t(n,s);return !a.success&&n===void 0&&(a.data=void 0,a.success=true),a}),this},nullable(){return h(this,"_parse",(t,n,s)=>{let a=t(n,s);return !a.success&&n===null&&(a.data=null,a.success=true),a}),this},nullish(){return h(this,"_parse",(t,n,s)=>{let a=t(n,s);return !a.success&&n==null&&(a.success=true,a.data=n),a}),this},default(t){return h(this,"_parse",(n,s,a)=>(s===void 0&&(s=t,s=typeof t=="function"?t():t),n(s,a))),this},pipe(t){return h(this,"_parse",(n,s,a)=>{let p=n(s,a);return p.success?t._parse(p.data,a):p}),this},refine(t,{message:n,type:s}={}){return s&&(n=n||T(s),o=s),h(this,"_parse",(a,p,f)=>{let m=a(p,f);d(f);if(!m.success)return m;let l=t(m.data);if(l===true||typeof l=="object"&&l?.success===true)return m;let b={message:typeof n=="function"?n(p):n};return {success:false,error:b,errors:[b]}}),this}};return y.length>0?y.reduce((t,n)=>n(t,e,c)??t,r):r}var y=[];function x(e){y.push(e);}x((e,o,i)=>{if(i?.type==="number"){let u=e;u.min=function(c,{message:r}={}){return this.refine(t=>t>=c,{message:r||`Number must be greater than or equal to ${c}.`})},u.max=function(c,{message:r}={}){return this.refine(t=>t<=c,{message:r||`Number must be less than or equal to ${c}.`})},u.positive=function({message:c}={}){return this.refine(r=>r>0,{message:c||"Number must be positive."})},u.negative=function({message:c}={}){return this.refine(r=>r<0,{message:c||"Number must be negative."})},u.int=function({message:c}={}){return this.refine(r=>Number.isInteger(r),{message:c||"Number must be an integer."})},u.float=function({message:c}={}){return this.refine(r=>Number.isFinite(r)&&!Number.isInteger(r),{message:c||"Number must be a floating point (non-integer)."})},u.multipleOf=function(c,{message:r}={}){return this.refine(t=>t%c===0,{message:r||`Number must be a multiple of ${c}.`})},u.finite=function({message:c}={}){return this.refine(r=>Number.isFinite(r),{message:c||"Number must be finite."})};}return e});exports.extend=x;exports.hookOriginal=h;exports.s=A;
@@ -0,0 +1,14 @@
1
+ export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, Infer, Invalid, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s } from './index.cjs';
2
+
3
+ declare module './index.ts' {
4
+ interface NumberSchemaInterface {
5
+ min(value: number, options?: SchemaInterfaceOptions): NumberSchemaInterface;
6
+ max(value: number, options?: SchemaInterfaceOptions): NumberSchemaInterface;
7
+ positive(options?: SchemaInterfaceOptions): NumberSchemaInterface;
8
+ negative(options?: SchemaInterfaceOptions): NumberSchemaInterface;
9
+ int(options?: SchemaInterfaceOptions): NumberSchemaInterface;
10
+ float(options?: SchemaInterfaceOptions): NumberSchemaInterface;
11
+ multipleOf(value: number, options?: SchemaInterfaceOptions): NumberSchemaInterface;
12
+ finite(options?: SchemaInterfaceOptions): NumberSchemaInterface;
13
+ }
14
+ }
@@ -0,0 +1,14 @@
1
+ export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, Infer, Invalid, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s } from './index.js';
2
+
3
+ declare module './index.ts' {
4
+ interface NumberSchemaInterface {
5
+ min(value: number, options?: SchemaInterfaceOptions): NumberSchemaInterface;
6
+ max(value: number, options?: SchemaInterfaceOptions): NumberSchemaInterface;
7
+ positive(options?: SchemaInterfaceOptions): NumberSchemaInterface;
8
+ negative(options?: SchemaInterfaceOptions): NumberSchemaInterface;
9
+ int(options?: SchemaInterfaceOptions): NumberSchemaInterface;
10
+ float(options?: SchemaInterfaceOptions): NumberSchemaInterface;
11
+ multipleOf(value: number, options?: SchemaInterfaceOptions): NumberSchemaInterface;
12
+ finite(options?: SchemaInterfaceOptions): NumberSchemaInterface;
13
+ }
14
+ }
@@ -0,0 +1 @@
1
+ import'./chunk-SHVCROKM.mjs';export{c as extend,b as hookOriginal,a as s}from'./chunk-NCHMO7B3.mjs';
@@ -0,0 +1 @@
1
+ 'use strict';var P={abortEarly:true};function I(t,o){if(!o)return t;let i=t?.cause?.key?`${o}.${t.cause.key}`:`${o}`;return {message:`Error parsing key "${i}": ${t.message}`,cause:{key:i}}}function O(t,o,i){!t.errors||t.errors.length===0||t.errors.forEach(c=>{let r=I(c,i);o.push(r);});}function d(t){return {...P,...t}}var k=t=>typeof t=="string"||t instanceof String,w=t=>typeof t=="number"||t instanceof Number,E=t=>t===true||t===false,$=t=>t instanceof Date&&!Number.isNaN(t.getTime()),_=t=>Array.isArray(t),b=t=>typeof t=="object"&&t!==null&&!Array.isArray(t),A={object(t,o){let i=S(b,{...o,type:"object"});return i._getDescription=()=>`object({ ${Object.entries(t).map(([r,n])=>`${r}: ${n._getDescription()}`).join(", ")} })`,h(i,"_parse",(c,r,n)=>{let e=c(r,n),{abortEarly:a}=d(n);if(e.success===false)return e;let u={},s=[];for(let p in t){let f=t[p]._parse(e.data[p],n);if(f.success)u[p]=f.data;else {if(f=f,a!==false){let m=I(f.error,p);return {success:false,error:m,errors:[m]}}O(f,s,p);}}return s.length>0?{success:false,error:s[0],errors:s}:{success:true,data:u}}),i},string(t){return S(k,{...t,type:"string"})},number(t){return S(w,{...t,type:"number"})},boolean(t){return S(E,{...t,type:"boolean"})},date(t){return S($,{...t,type:"date"})},enum(t,o){let i=e=>t.includes(e),c=e=>`Invalid ${r} value. Expected ${t.map(a=>`"${a}"`).join(" | ")}, received "${e}".`,r="enum",n=S(i,{message:c,...o,type:r});return n._getDescription=()=>`enum(${t.map(e=>`"${e}"`).join(" | ")})`,n},array(t,o){let i=S(_,{...o,type:"array"});return i._getDescription=()=>`array(${t._getDescription()})`,h(i,"_parse",(c,r,n)=>{let e=c(r,n),{abortEarly:a}=d(n);if(e.success===false)return e;let u=[],s=[];for(let p=0;p<e.data.length;p++){let f=t._parse(e.data[p],n);if(f.success)u.push(f.data);else {if(f=f,a!==false){let m=I(f.error,p);return {success:false,error:m,errors:[m]}}O(f,s,p);}}return s.length>0?{success:false,error:s[0],errors:s}:{success:true,data:u}}),i},any(){return S(()=>true)},preprocess(t,o){return h(o,"_parse",(i,c)=>(c=t(c),i(c))),o},union(t,o){return S(n=>{for(let e=0;e<t.length;e++){let a=t[e]._parse(n);if(a.success)return a}return false},{message:n=>`Invalid union value. Expected the value to match one of the schemas:${t.map((e,a)=>` ${a+1}. ${e._getDescription()}`).join(",")} but received "${typeof n}" with value: ${b(n)?JSON.stringify(n):`"${n}"`}`,...o,type:"union"})}};function T(t){return o=>`The value "${o}" must be type of ${t} but is type of "${typeof o}".`}function h(t,o,i){let c=t[o];t[o]=(...r)=>i(c,...r);}function S(t,{type:o="any",name:i,message:c}={}){c=c||T(o);let r={name:i,message:c,type:o},n={_getName(){return i},_getType(){return o},_getDescription(){return this._getName()??this._getType()},_parse(e,a){let u=t(e);if(u===true)return {success:true,data:e};if(typeof u=="object"&&u?.success===true)return u;let s={message:typeof c=="function"?c(e):c};return {success:false,error:s,errors:[s]}},parse(e,a){let u=n._parse(e,a);if(!u.success)throw u=u,new Error(u.error.message,{cause:u.error.cause});return u.data},safeParse(e,a){return n._parse(e,a)},transform(e){return h(this,"_parse",(a,u,s)=>{let p=a(u,s);return p.success&&(p.data=e(p.data)),p}),this},optional(){return h(this,"_parse",(e,a,u)=>{let s=e(a,u);return !s.success&&a===void 0&&(s.data=void 0,s.success=true),s}),this},nullable(){return h(this,"_parse",(e,a,u)=>{let s=e(a,u);return !s.success&&a===null&&(s.data=null,s.success=true),s}),this},nullish(){return h(this,"_parse",(e,a,u)=>{let s=e(a,u);return !s.success&&a==null&&(s.success=true,s.data=a),s}),this},default(e){return h(this,"_parse",(a,u,s)=>(u===void 0&&(u=e,u=typeof e=="function"?e():e),a(u,s))),this},pipe(e){return h(this,"_parse",(a,u,s)=>{let p=a(u,s);return p.success?e._parse(p.data,s):p}),this},refine(e,{message:a,type:u}={}){return u&&(a=a||T(u),o=u),h(this,"_parse",(s,p,f)=>{let m=s(p,f);d(f);if(!m.success)return m;let l=e(m.data);if(l===true||typeof l=="object"&&l?.success===true)return m;let g={message:typeof a=="function"?a(p):a};return {success:false,error:g,errors:[g]}}),this}};return y.length>0?y.reduce((e,a)=>a(e,t,r)??e,n):n}var y=[];function x(t){y.push(t);}x((t,o,i)=>{if(i?.type==="string"){let c=t;c.min=function(r,{message:n}={}){return this.refine(e=>e.length>=r,{message:n||(e=>`String must be at least ${r} characters long (received ${e.length} characters: "${e}")`)})},c.max=function(r,{message:n}={}){return this.refine(e=>e.length<=r,{message:n||(e=>`String must be at most ${r} characters long (received ${e.length} characters: "${e}")`)})},c.length=function(r,{message:n}={}){return this.refine(e=>e.length===r,{message:n||(e=>`String must be exactly ${r} characters long (received ${e.length} characters: "${e}")`)})},c.nonEmpty=function({message:r}={}){return this.refine(n=>n.length>0,{message:r||"String must not be empty (received empty string)"})},c.startsWith=function(r,{message:n}={}){return this.refine(e=>e.startsWith(r),{message:n||(e=>`String must start with "${r}" (received: "${e}")`)})},c.endsWith=function(r,{message:n}={}){return this.refine(e=>e.endsWith(r),{message:n||(e=>`String must end with "${r}" (received: "${e}")`)})},c.includes=function(r,{message:n}={}){return this.refine(e=>e.includes(r),{message:n||(e=>`String must include "${r}" (received: "${e}")`)})},c.toLowerCase=function(){return this.transform(r=>r.toLowerCase())},c.toUpperCase=function(){return this.transform(r=>r.toUpperCase())},c.trim=function(){return this.transform(r=>r.trim())},c.padStart=function(r,n=" "){return this.transform(e=>e.padStart(r,n))},c.padEnd=function(r,n=" "){return this.transform(e=>e.padEnd(r,n))},c.replace=function(r,n){return this.transform(e=>e.replace(r,n))};}return t});exports.extend=x;exports.hookOriginal=h;exports.s=A;
@@ -0,0 +1,19 @@
1
+ export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, Infer, Invalid, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s } from './index.cjs';
2
+
3
+ declare module './index.ts' {
4
+ interface StringSchemaInterface {
5
+ min(length: number, options?: SchemaInterfaceOptions): StringSchemaInterface;
6
+ max(length: number, options?: SchemaInterfaceOptions): StringSchemaInterface;
7
+ length(length: number, options?: SchemaInterfaceOptions): StringSchemaInterface;
8
+ nonEmpty(options?: SchemaInterfaceOptions): StringSchemaInterface;
9
+ startsWith(prefix: string, options?: SchemaInterfaceOptions): StringSchemaInterface;
10
+ endsWith(suffix: string, options?: SchemaInterfaceOptions): StringSchemaInterface;
11
+ includes(substring: string, options?: SchemaInterfaceOptions): StringSchemaInterface;
12
+ trim(): StringSchemaInterface;
13
+ toLowerCase(): StringSchemaInterface;
14
+ toUpperCase(): StringSchemaInterface;
15
+ padStart(length: number, fillChar?: string): StringSchemaInterface;
16
+ padEnd(length: number, fillChar?: string): StringSchemaInterface;
17
+ replace(search: string | RegExp, replace: string): StringSchemaInterface;
18
+ }
19
+ }
@@ -0,0 +1,19 @@
1
+ export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, Infer, Invalid, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s } from './index.js';
2
+
3
+ declare module './index.ts' {
4
+ interface StringSchemaInterface {
5
+ min(length: number, options?: SchemaInterfaceOptions): StringSchemaInterface;
6
+ max(length: number, options?: SchemaInterfaceOptions): StringSchemaInterface;
7
+ length(length: number, options?: SchemaInterfaceOptions): StringSchemaInterface;
8
+ nonEmpty(options?: SchemaInterfaceOptions): StringSchemaInterface;
9
+ startsWith(prefix: string, options?: SchemaInterfaceOptions): StringSchemaInterface;
10
+ endsWith(suffix: string, options?: SchemaInterfaceOptions): StringSchemaInterface;
11
+ includes(substring: string, options?: SchemaInterfaceOptions): StringSchemaInterface;
12
+ trim(): StringSchemaInterface;
13
+ toLowerCase(): StringSchemaInterface;
14
+ toUpperCase(): StringSchemaInterface;
15
+ padStart(length: number, fillChar?: string): StringSchemaInterface;
16
+ padEnd(length: number, fillChar?: string): StringSchemaInterface;
17
+ replace(search: string | RegExp, replace: string): StringSchemaInterface;
18
+ }
19
+ }
@@ -0,0 +1 @@
1
+ import'./chunk-BV6SWQSH.mjs';export{c as extend,b as hookOriginal,a as s}from'./chunk-NCHMO7B3.mjs';
package/package.json CHANGED
@@ -1,28 +1,60 @@
1
1
  {
2
2
  "name": "@esmj/schema",
3
- "version": "0.4.0",
3
+ "version": "0.5.1",
4
4
  "description": "Tiny extendable package for schema validation.",
5
+ "type": "module",
5
6
  "keywords": [
6
7
  "schema",
7
8
  "validation",
8
9
  "type",
10
+ "typescript",
11
+ "type-safe",
12
+ "validator",
13
+ "parse",
14
+ "transform",
9
15
  "inference",
10
16
  "zod",
11
- "@sinclair/typebox",
12
17
  "yup",
18
+ "@sinclair/typebox",
13
19
  "superstruct",
14
20
  "joi",
15
21
  "arktype"
16
22
  ],
17
- "main": "dist/index",
18
- "module": "dist/index",
23
+ "main": "./dist/index.cjs",
24
+ "module": "./dist/index.mjs",
19
25
  "exports": {
20
26
  ".": {
27
+ "types": "./dist/index.d.ts",
21
28
  "import": "./dist/index.mjs",
22
- "require": "./dist/index.js"
29
+ "require": "./dist/index.cjs"
30
+ },
31
+ "./full": {
32
+ "types": "./dist/full.d.ts",
33
+ "import": "./dist/full.mjs",
34
+ "require": "./dist/full.cjs"
35
+ },
36
+ "./string": {
37
+ "types": "./dist/string.d.ts",
38
+ "import": "./dist/string.mjs",
39
+ "require": "./dist/string.cjs"
40
+ },
41
+ "./number": {
42
+ "types": "./dist/number.d.ts",
43
+ "import": "./dist/number.mjs",
44
+ "require": "./dist/number.cjs"
45
+ },
46
+ "./array": {
47
+ "types": "./dist/array.d.ts",
48
+ "import": "./dist/array.mjs",
49
+ "require": "./dist/array.cjs"
23
50
  }
24
51
  },
25
- "sideEffects": false,
52
+ "sideEffects": [
53
+ "src/string.ts",
54
+ "src/number.ts",
55
+ "src/array.ts",
56
+ "src/full.ts"
57
+ ],
26
58
  "typings": "dist/index.d.ts",
27
59
  "scripts": {
28
60
  "lint": "biome check --no-errors-on-unmatched",
@@ -70,22 +102,20 @@
70
102
  "@biomejs/biome": "1.9.4",
71
103
  "@commitlint/cli": "^19.8.1",
72
104
  "@commitlint/config-conventional": "^19.8.1",
73
- "@sinclair/typebox": "^0.34.38",
74
- "@typescript-eslint/eslint-plugin": "^8.33.0",
75
- "@typescript-eslint/parser": "^8.33.0",
105
+ "@sinclair/typebox": "^0.34.41",
76
106
  "@zod/mini": "^4.0.0-beta.0",
77
- "arktype": "^2.1.20",
107
+ "arktype": "^2.1.22",
78
108
  "commitizen": "^4.3.1",
79
109
  "conventional-changelog-cli": "^5.0.0",
80
110
  "cz-conventional-changelog": "^3.3.0",
81
- "effect": "^3.17.6",
111
+ "effect": "^3.17.13",
82
112
  "git-cz": "^4.9.0",
83
113
  "husky": "^9.1.7",
84
114
  "joi": "^17.13.3",
85
- "lint-staged": "^16.1.0",
115
+ "lint-staged": "^16.1.6",
86
116
  "superstruct": "^2.0.2",
87
117
  "tsup": "^8.5.0",
88
- "yup": "^1.6.1",
118
+ "yup": "^1.7.0",
89
119
  "zod": "^3.25.42"
90
120
  }
91
121
  }
package/dist/index.d.mts DELETED
@@ -1,80 +0,0 @@
1
- type ErrorStructure = {
2
- message: string;
3
- cause?: {
4
- key?: string;
5
- };
6
- };
7
- type Valid<Output> = {
8
- success: true;
9
- data: Output;
10
- };
11
- type Invalid = {
12
- success: false;
13
- error: ErrorStructure;
14
- errors?: ErrorStructure[];
15
- };
16
- type InternalParseOutput<Output> = Valid<Output> | Invalid;
17
- interface ParseOptions {
18
- abortEarly?: boolean;
19
- }
20
- interface SchemaInterface<Input, Output> {
21
- _getType(): string;
22
- _getDescription(): string;
23
- _parse(value: Input | Partial<Input>, options?: ParseOptions): InternalParseOutput<Output>;
24
- parse(value: Input | Partial<Input>, options?: ParseOptions): Output;
25
- safeParse(value: Input | Partial<Input>, options?: ParseOptions): InternalParseOutput<Output>;
26
- optional(): SchemaInterface<Input, Partial<Output> | undefined>;
27
- transform<NewOutput>(callback: (value: Input) => NewOutput): SchemaInterface<Input, NewOutput>;
28
- nullable(): SchemaInterface<Input, Output | null>;
29
- nullish(): SchemaInterface<Input, Output | undefined | null>;
30
- default(defaultValue: Partial<Input> | (() => Partial<Input>) | Partial<Output>): SchemaInterface<Input, Output>;
31
- pipe<NewOutput>(schema: SchemaInterface<Output, NewOutput>): SchemaInterface<Output, NewOutput>;
32
- refine(validation: (value: Output) => boolean, options?: SchemaInterfaceOptions): SchemaInterface<Input, Output>;
33
- }
34
- interface UnionSchemaInterface<T extends Array<SchemaInterface<unknown, unknown>>> extends SchemaInterface<ReturnType<T[number]['parse']>, ReturnType<T[number]['parse']>> {
35
- }
36
- interface EnumSchemaInterface<T extends string> extends SchemaInterface<string, T> {
37
- }
38
- interface StringSchemaInterface extends SchemaInterface<string, string> {
39
- }
40
- interface NumberSchemaInterface extends SchemaInterface<number, number> {
41
- }
42
- interface BooleanSchemaInterface extends SchemaInterface<boolean, boolean> {
43
- }
44
- interface DateSchemaInterface extends SchemaInterface<Date, Date> {
45
- }
46
- interface ArraySchemaInterface<T extends SchemaType> extends SchemaInterface<Array<T>, Array<ReturnType<T['parse']>>> {
47
- }
48
- interface ObjectSchemaInterface<T extends Record<string, SchemaType>> extends SchemaInterface<{
49
- [Property in keyof T]: ReturnType<T[Property]['parse']>;
50
- }, {
51
- [Property in keyof T]: ReturnType<T[Property]['parse']>;
52
- }> {
53
- }
54
- type SchemaType = StringSchemaInterface | SchemaInterface<unknown, unknown> | SchemaInterface<string, string> | SchemaInterface<string, string | undefined> | SchemaInterface<string, string | null> | SchemaInterface<string, string | undefined | null> | ObjectSchemaInterface<Record<string, SchemaType>> | SchemaInterface<object, object> | SchemaInterface<object, object | undefined> | SchemaInterface<object, object | null> | SchemaInterface<object, object | undefined | null> | NumberSchemaInterface | SchemaInterface<number, number> | SchemaInterface<number, number | undefined> | SchemaInterface<number, number | null> | SchemaInterface<number, number | undefined | null> | BooleanSchemaInterface | SchemaInterface<boolean, boolean> | SchemaInterface<boolean, boolean | undefined> | SchemaInterface<boolean, boolean | null> | SchemaInterface<boolean, boolean | undefined | null> | DateSchemaInterface | SchemaInterface<Date, Date> | SchemaInterface<Date, Date | undefined> | SchemaInterface<Date, Date | null> | SchemaInterface<Date, Date | undefined | null> | EnumSchemaInterface<string> | UnionSchemaInterface<Array<SchemaInterface<unknown, unknown>>> | ArraySchemaInterface<StringSchemaInterface | ObjectSchemaInterface<Record<string, SchemaType>> | NumberSchemaInterface | BooleanSchemaInterface | DateSchemaInterface | EnumSchemaInterface<string>> | SchemaInterface<Array<unknown>, Array<unknown>> | SchemaInterface<Array<unknown>, Array<unknown> | undefined> | SchemaInterface<Array<unknown>, Array<unknown> | null> | SchemaInterface<Array<unknown>, Array<unknown> | undefined | null>;
55
- type ErrorMessage = string | ((value: unknown) => string);
56
- type ExtenderType = (inter: SchemaType, validation: Function, options?: {
57
- message: ErrorMessage;
58
- type: string;
59
- }) => SchemaType;
60
- interface CreateSchemaInterfaceOptions {
61
- type?: string;
62
- message?: ErrorMessage;
63
- }
64
- type SchemaInterfaceOptions = Omit<CreateSchemaInterfaceOptions, 'type'>;
65
- declare const s: {
66
- object<T extends Record<string, SchemaType>>(definition: { [Property in keyof T]: T[Property]; }, options?: SchemaInterfaceOptions): ObjectSchemaInterface<T>;
67
- string(options?: SchemaInterfaceOptions): StringSchemaInterface;
68
- number(options?: SchemaInterfaceOptions): NumberSchemaInterface;
69
- boolean(options?: SchemaInterfaceOptions): BooleanSchemaInterface;
70
- date(options?: SchemaInterfaceOptions): DateSchemaInterface;
71
- enum(definition: Readonly<Array<string>>, options?: SchemaInterfaceOptions): EnumSchemaInterface<(typeof definition)[number]>;
72
- array<T extends SchemaType>(definition: T, options?: SchemaInterfaceOptions): ArraySchemaInterface<T>;
73
- any(): any;
74
- preprocess<T extends SchemaType>(callback: Function, schema: T): T;
75
- union<T extends Array<SchemaType>>(definitions: T, options?: SchemaInterfaceOptions): UnionSchemaInterface<T>;
76
- };
77
- declare function extend(callback: ExtenderType): void;
78
- type Infer<T> = T extends SchemaType ? ReturnType<T['parse']> : unknown;
79
-
80
- export { type ArraySchemaInterface, type BooleanSchemaInterface, type DateSchemaInterface, type EnumSchemaInterface, type ExtenderType, type Infer, type NumberSchemaInterface, type ObjectSchemaInterface, type SchemaInterface, type SchemaInterfaceOptions, type SchemaType, type StringSchemaInterface, type UnionSchemaInterface, extend, s };
package/dist/index.js DELETED
@@ -1 +0,0 @@
1
- 'use strict';var g={abortEarly:true};function I(e,s){if(!s)return e;let c=e?.cause?.key?`${s}.${e.cause.key}`:`${s}`;return {message:`Error parsing key "${c}": ${e.message}`,cause:{key:c}}}function y(e,s,c){!e.errors||e.errors.length===0||e.errors.forEach(i=>{let o=I(i,c);s.push(o);});}function S(e){return {...g,...e}}var b=e=>typeof e=="string"||e instanceof String,O=e=>typeof e=="number"||e instanceof Number,P=e=>e===true||e===false,k=e=>e instanceof Date&&!Number.isNaN(e.getTime()),x=e=>Array.isArray(e),T=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),E={object(e,s){let c=h(T,{...s,type:"object"});return c._getDescription=()=>`object({ ${Object.entries(e).map(([o,t])=>`${o}: ${t._getDescription()}`).join(", ")} })`,m(c,"_parse",(i,o,t)=>{let r=i(o,t),{abortEarly:a}=S(t);if(r.success===false)return r;let n={},u=[];for(let p in e){let f=e[p]._parse(r.data[p],t);if(f.success)n[p]=f.data;else {if(f=f,a!==false){let l=I(f.error,p);return {success:false,error:l,errors:[l]}}y(f,u,p);}}return u.length>0?{success:false,error:u[0],errors:u}:{success:true,data:n}}),c},string(e){return h(b,{...e,type:"string"})},number(e){return h(O,{...e,type:"number"})},boolean(e){return h(P,{...e,type:"boolean"})},date(e){return h(k,{...e,type:"date"})},enum(e,s){let c=r=>e.includes(r),i=r=>`Invalid ${o} value. Expected ${e.map(a=>`"${a}"`).join(" | ")}, received "${r}".`,o="enum",t=h(c,{message:i,...s,type:o});return t._getDescription=()=>`enum(${e.map(r=>`"${r}"`).join(" | ")})`,t},array(e,s){let c=h(x,{...s,type:"array"});return c._getDescription=()=>`array(${e._getDescription()})`,m(c,"_parse",(i,o,t)=>{let r=i(o,t),{abortEarly:a}=S(t);if(r.success===false)return r;let n=[],u=[];for(let p=0;p<r.data.length;p++){let f=e._parse(r.data[p],t);if(f.success)n.push(f.data);else {if(f=f,a!==false){let l=I(f.error,p);return {success:false,error:l,errors:[l]}}y(f,u,p);}}return u.length>0?{success:false,error:u[0],errors:u}:{success:true,data:n}}),c},any(){return h(()=>true)},preprocess(e,s){return m(s,"_parse",(c,i)=>(i=e(i),c(i))),s},union(e,s){return h(t=>{for(let r=0;r<e.length;r++)if(e[r]._parse(t).success)return true;return false},{message:t=>`Invalid union value. Expected the value to match one of the schemas:${e.map((r,a)=>` ${a+1}. ${r._getDescription()}`).join(",")} but received "${typeof t}" with value: ${T(t)?JSON.stringify(t):`"${t}"`}`,...s,type:"union"})}};function w(e){return s=>`The value "${s}" must be type of ${e} but is type of "${typeof s}".`}function m(e,s,c){let i=e[s];e[s]=(...o)=>c(i,...o);}function h(e,{type:s="any",message:c}={}){c=c||w(s);let i={message:c,type:s},o={_getType(){return s},_getDescription(){return this._getType()},_parse(t,r){if(e(t))return {success:true,data:t};let n={message:typeof c=="function"?c(t):c};return {success:false,error:n,errors:[n]}},parse(t,r){let a=o._parse(t,r);if(!a.success)throw a=a,new Error(a.error.message,{cause:a.error.cause});return a.data},safeParse(t,r){return o._parse(t,r)},transform(t){return m(this,"_parse",(r,a,n)=>{let u=r(a,n);return u.success&&(u.data=t(u.data)),u}),this},optional(){return m(this,"_parse",(t,r,a)=>{let n=t(r,a);return n.success||(n.data=void 0,n.success=true),n}),this},nullable(){return m(this,"_parse",(t,r,a)=>{let n=t(r,a);return n.success||(n.data=null,n.success=true),n}),this},nullish(){return m(this,"_parse",(t,r,a)=>{let n=t(r,a);return !n.success&&r==null&&(n.success=true,n.data=r),n}),this},default(t){return m(this,"_parse",(r,a,n)=>(a===void 0&&(a=t,a=typeof t=="function"?t():t),r(a,n))),this},pipe(t){return m(this,"_parse",(r,a,n)=>{let u=r(a,n);return u.success?t._parse(u.data,n):u}),this},refine(t,{message:r}={}){return m(this,"_parse",(a,n,u)=>{let p=a(n,u);if(!p.success)return p;if(!t(p.data)){let f=typeof r=="function"?r(n):r;return {success:false,error:{message:f},errors:[{message:f}]}}return p}),this}};return d.length>0?d.reduce((t,r)=>r(t,e,i)??t,o):o}var d=[];function _(e){d.push(e);}exports.extend=_;exports.s=E;