@esmj/schema 0.2.3 → 0.3.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 +43 -0
- package/dist/index.d.mts +10 -6
- package/dist/index.d.ts +10 -6
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -17,6 +17,15 @@ npm install @esmj/schema
|
|
|
17
17
|
3. **Rich Features**: Includes advanced features like preprocessing, transformations, piping, and refinements, which are not always available in similar libraries.
|
|
18
18
|
4. **Lightweight**: None dependencies and a small footprint make it ideal for projects where performance and simplicity are key.
|
|
19
19
|
5. **Customizable**: Offers fine-grained control over validation and error handling.
|
|
20
|
+
6. **Performance**: `@esmj/schema` is optimized for speed, making it one of the fastest schema validation libraries available. Whether you're creating schemas, parsing data, or handling errors, `@esmj/schema` consistently outperforms many popular alternatives. Its minimalistic design ensures low overhead, even in high-performance applications.
|
|
21
|
+
|
|
22
|
+
### Performance Highlights
|
|
23
|
+
|
|
24
|
+
- **Schema Creation**: Create schemas in as little as `0.02 ms`, even for complex structures.
|
|
25
|
+
- **Parsing**: Parse data with blazing-fast speeds, handling 1,000,000 iterations in under `300 ms`.
|
|
26
|
+
- **Error Handling**: Efficiently manage errors with minimal performance impact, processing 1,000,000 iterations in under `400 ms`.
|
|
27
|
+
|
|
28
|
+
These performance metrics make `@esmj/schema` an excellent choice for both frontend and backend applications where speed and efficiency are critical.
|
|
20
29
|
|
|
21
30
|
## Comparison with Similar Libraries
|
|
22
31
|
|
|
@@ -200,6 +209,40 @@ console.log(invalidResult.error.message);
|
|
|
200
209
|
// Error: Invalid enum value. Expected "admin" | "user" | "guest", received "invalidRole".
|
|
201
210
|
```
|
|
202
211
|
|
|
212
|
+
#### `s.union(definitions)`
|
|
213
|
+
|
|
214
|
+
Creates a schema that validates against multiple schemas (a union of schemas). The value must match at least one of the provided schemas.
|
|
215
|
+
|
|
216
|
+
- **`definitions`**: An array of schemas to validate against.
|
|
217
|
+
|
|
218
|
+
```typescript
|
|
219
|
+
const schema = s.union([
|
|
220
|
+
s.string(),
|
|
221
|
+
s.number(),
|
|
222
|
+
s.boolean(),
|
|
223
|
+
]);
|
|
224
|
+
|
|
225
|
+
const validString = schema.parse('hello');
|
|
226
|
+
console.log(validString);
|
|
227
|
+
// 'hello'
|
|
228
|
+
|
|
229
|
+
const validNumber = schema.parse(42);
|
|
230
|
+
console.log(validNumber);
|
|
231
|
+
// 42
|
|
232
|
+
|
|
233
|
+
const validBoolean = schema.parse(true);
|
|
234
|
+
console.log(validBoolean);
|
|
235
|
+
// true
|
|
236
|
+
|
|
237
|
+
const invalidValue = schema.safeParse({ key: 'value' });
|
|
238
|
+
console.log(invalidValue.success);
|
|
239
|
+
// false
|
|
240
|
+
console.log(invalidValue.error.message);
|
|
241
|
+
// Validation failed. Expected the value to match one of the schemas: "string" | "number" | "boolean", but received "object" with value "{"key":"value"}".
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
**Use Case**: The `union` method is useful when you need to validate data that can be of multiple types, such as a value that can be a string, number, or boolean.
|
|
245
|
+
|
|
203
246
|
#### `s.preprocess(callback, schema)`
|
|
204
247
|
|
|
205
248
|
Creates a schema that preprocesses the input value using the provided callback before validating it with the given schema.
|
package/dist/index.d.mts
CHANGED
|
@@ -14,9 +14,10 @@ type Invalid = {
|
|
|
14
14
|
};
|
|
15
15
|
type InternalParseOutput<Output> = Valid<Output> | Invalid;
|
|
16
16
|
interface SchemaInterface<Input, Output> {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
_getType(): string;
|
|
18
|
+
_parse(value: Input | Partial<Input>): InternalParseOutput<Output>;
|
|
19
|
+
parse(value: Input | Partial<Input>): Output;
|
|
20
|
+
safeParse(value: Input | Partial<Input>): InternalParseOutput<Output>;
|
|
20
21
|
optional(): SchemaInterface<Input, Partial<Output> | undefined>;
|
|
21
22
|
transform<NewOutput>(callback: (value: Input) => NewOutput): SchemaInterface<Input, NewOutput>;
|
|
22
23
|
nullable(): SchemaInterface<Input, Output | null>;
|
|
@@ -27,6 +28,8 @@ interface SchemaInterface<Input, Output> {
|
|
|
27
28
|
message: string;
|
|
28
29
|
}): SchemaInterface<Input, Output>;
|
|
29
30
|
}
|
|
31
|
+
interface UnionSchemaInterface<T extends Array<SchemaInterface<unknown, unknown>>> extends SchemaInterface<ReturnType<T[number]['parse']>, ReturnType<T[number]['parse']>> {
|
|
32
|
+
}
|
|
30
33
|
interface EnumSchemaInterface<T extends string> extends SchemaInterface<string, T> {
|
|
31
34
|
}
|
|
32
35
|
interface StringSchemaInterface extends SchemaInterface<string, string> {
|
|
@@ -40,12 +43,12 @@ interface DateSchemaInterface extends SchemaInterface<Date, Date> {
|
|
|
40
43
|
interface ArraySchemaInterface<T extends SchemaType> extends SchemaInterface<Array<T>, Array<ReturnType<T['parse']>>> {
|
|
41
44
|
}
|
|
42
45
|
interface ObjectSchemaInterface<T extends Record<string, SchemaType>> extends SchemaInterface<{
|
|
43
|
-
[Property in keyof T]: T[Property]
|
|
46
|
+
[Property in keyof T]: ReturnType<T[Property]['parse']>;
|
|
44
47
|
}, {
|
|
45
48
|
[Property in keyof T]: ReturnType<T[Property]['parse']>;
|
|
46
49
|
}> {
|
|
47
50
|
}
|
|
48
|
-
type SchemaType = StringSchemaInterface | 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> | 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>;
|
|
51
|
+
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>;
|
|
49
52
|
type ExtenderType = (inter: SchemaType, validation: Function, options: {
|
|
50
53
|
message: string;
|
|
51
54
|
type: string;
|
|
@@ -60,8 +63,9 @@ declare const s: {
|
|
|
60
63
|
array<T extends SchemaType>(definition: T): ArraySchemaInterface<T>;
|
|
61
64
|
any(): any;
|
|
62
65
|
preprocess<T extends SchemaType>(callback: Function, schema: T): T;
|
|
66
|
+
union<T extends Array<SchemaType>>(definitions: T): UnionSchemaInterface<T>;
|
|
63
67
|
};
|
|
64
68
|
declare function extend(callback: ExtenderType): void;
|
|
65
69
|
type Infer<T> = T extends SchemaType ? ReturnType<T['parse']> : unknown;
|
|
66
70
|
|
|
67
|
-
export { type ArraySchemaInterface, type BooleanSchemaInterface, type DateSchemaInterface, type EnumSchemaInterface, type ExtenderType, type Infer, type NumberSchemaInterface, type ObjectSchemaInterface, type SchemaInterface, type SchemaType, type StringSchemaInterface, extend, s };
|
|
71
|
+
export { type ArraySchemaInterface, type BooleanSchemaInterface, type DateSchemaInterface, type EnumSchemaInterface, type ExtenderType, type Infer, type NumberSchemaInterface, type ObjectSchemaInterface, type SchemaInterface, type SchemaType, type StringSchemaInterface, type UnionSchemaInterface, extend, s };
|
package/dist/index.d.ts
CHANGED
|
@@ -14,9 +14,10 @@ type Invalid = {
|
|
|
14
14
|
};
|
|
15
15
|
type InternalParseOutput<Output> = Valid<Output> | Invalid;
|
|
16
16
|
interface SchemaInterface<Input, Output> {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
_getType(): string;
|
|
18
|
+
_parse(value: Input | Partial<Input>): InternalParseOutput<Output>;
|
|
19
|
+
parse(value: Input | Partial<Input>): Output;
|
|
20
|
+
safeParse(value: Input | Partial<Input>): InternalParseOutput<Output>;
|
|
20
21
|
optional(): SchemaInterface<Input, Partial<Output> | undefined>;
|
|
21
22
|
transform<NewOutput>(callback: (value: Input) => NewOutput): SchemaInterface<Input, NewOutput>;
|
|
22
23
|
nullable(): SchemaInterface<Input, Output | null>;
|
|
@@ -27,6 +28,8 @@ interface SchemaInterface<Input, Output> {
|
|
|
27
28
|
message: string;
|
|
28
29
|
}): SchemaInterface<Input, Output>;
|
|
29
30
|
}
|
|
31
|
+
interface UnionSchemaInterface<T extends Array<SchemaInterface<unknown, unknown>>> extends SchemaInterface<ReturnType<T[number]['parse']>, ReturnType<T[number]['parse']>> {
|
|
32
|
+
}
|
|
30
33
|
interface EnumSchemaInterface<T extends string> extends SchemaInterface<string, T> {
|
|
31
34
|
}
|
|
32
35
|
interface StringSchemaInterface extends SchemaInterface<string, string> {
|
|
@@ -40,12 +43,12 @@ interface DateSchemaInterface extends SchemaInterface<Date, Date> {
|
|
|
40
43
|
interface ArraySchemaInterface<T extends SchemaType> extends SchemaInterface<Array<T>, Array<ReturnType<T['parse']>>> {
|
|
41
44
|
}
|
|
42
45
|
interface ObjectSchemaInterface<T extends Record<string, SchemaType>> extends SchemaInterface<{
|
|
43
|
-
[Property in keyof T]: T[Property]
|
|
46
|
+
[Property in keyof T]: ReturnType<T[Property]['parse']>;
|
|
44
47
|
}, {
|
|
45
48
|
[Property in keyof T]: ReturnType<T[Property]['parse']>;
|
|
46
49
|
}> {
|
|
47
50
|
}
|
|
48
|
-
type SchemaType = StringSchemaInterface | 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> | 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>;
|
|
51
|
+
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>;
|
|
49
52
|
type ExtenderType = (inter: SchemaType, validation: Function, options: {
|
|
50
53
|
message: string;
|
|
51
54
|
type: string;
|
|
@@ -60,8 +63,9 @@ declare const s: {
|
|
|
60
63
|
array<T extends SchemaType>(definition: T): ArraySchemaInterface<T>;
|
|
61
64
|
any(): any;
|
|
62
65
|
preprocess<T extends SchemaType>(callback: Function, schema: T): T;
|
|
66
|
+
union<T extends Array<SchemaType>>(definitions: T): UnionSchemaInterface<T>;
|
|
63
67
|
};
|
|
64
68
|
declare function extend(callback: ExtenderType): void;
|
|
65
69
|
type Infer<T> = T extends SchemaType ? ReturnType<T['parse']> : unknown;
|
|
66
70
|
|
|
67
|
-
export { type ArraySchemaInterface, type BooleanSchemaInterface, type DateSchemaInterface, type EnumSchemaInterface, type ExtenderType, type Infer, type NumberSchemaInterface, type ObjectSchemaInterface, type SchemaInterface, type SchemaType, type StringSchemaInterface, extend, s };
|
|
71
|
+
export { type ArraySchemaInterface, type BooleanSchemaInterface, type DateSchemaInterface, type EnumSchemaInterface, type ExtenderType, type Infer, type NumberSchemaInterface, type ObjectSchemaInterface, type SchemaInterface, type SchemaType, type StringSchemaInterface, type UnionSchemaInterface, extend, s };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var h=r=>typeof r=="string"||r instanceof String,
|
|
1
|
+
'use strict';var h=r=>typeof r=="string"||r instanceof String,l=r=>typeof r=="number"||r instanceof Number,y=r=>r===true||r===false,I=r=>r instanceof Date&&!Number.isNaN(r.getTime()),d=r=>Array.isArray(r),S=r=>typeof r=="object"&&r!==null&&!Array.isArray(r),b={object(r){let a=p(S,{type:"object"});return i(a,"_parse",(s,...o)=>{let c=s(...o);if(c.success===false)return c;let t={};for(let n in r){let e=r[n]._parse(c.data[n]);if(e.success)t[n]=e.data;else {e=e;let u=e?.error?.cause?.key?`${n}.${e?.error?.cause?.key}`:n;return e.error.message=`Error parsing key "${u}": ${e.error.message}`,e.error.cause={key:u},e}}return {success:true,data:t}}),a},string(){return p(h,{type:"string"})},number(){return p(l,{type:"number"})},boolean(){return p(y,{type:"boolean"})},date(){return p(I,{type:"date"})},enum(r){let a=t=>r.includes(t),s=t=>`Invalid ${o} value. Expected ${r.map(n=>`"${n}"`).join(" | ")}, received "${t}".`,o="enum";return p(a,{type:o,message:s})},array(r){let a=p(d,{type:"array"});return i(a,"_parse",(s,...o)=>{let c=s(...o);if(c.success===false)return c;let t=[];for(let n=0;n<c.data.length;n++){let e=r._parse(c.data[n]);if(e.success)t.push(e.data);else {e=e;let u=e.error?.cause?.key?`${n}.${e.error.cause.key}`:`${n}`;return e.error.message=`Error parsing index "${n}": ${e.error.message}`,e.error.cause={key:u},e}}return {success:true,data:t}}),a},any(){return p(()=>true)},preprocess(r,a){return i(a,"_parse",(s,o)=>(o=r(o),s(o))),a},union(r){return p(c=>{for(let t=0;t<r.length;t++)if(r[t]._parse(c).success)return true;return false},{type:"union",message:c=>`Invalid union value. Expected the value to match one of the schemas: ${r.map(t=>`"${t._getType()}"`).join(" | ")}, but received "${typeof c}" with value "${c}".`})}};function T(r){return a=>`The value "${a}" must be type of ${r} but is type of "${typeof a}".`}function i(r,a,s){let o=r[a];r[a]=(...c)=>s(o,...c);}function p(r,{type:a="any",message:s}={}){s=s||T(a);let o={message:s,type:a},c={_getType(){return a},_parse(t){return r(t)?{success:true,data:t}:{success:false,error:{message:s(t)}}},parse(t){let n=c._parse(t);if(!n.success)throw n=n,new Error(n.error.message,{cause:n.error.cause});return n.data},safeParse(t){return c._parse(t)},transform(t){return i(this,"_parse",(n,e)=>{let u=n(e);return u.success&&(u.data=t(u.data)),u}),this},optional(){return i(this,"_parse",(t,n)=>{let e=t(n);return e.success||(e.data=void 0,e.success=true),e}),this},nullable(){return i(this,"_parse",(t,n)=>{let e=t(n);return e.success||(e.data=null,e.success=true),e}),this},nullish(){return i(this,"_parse",(t,n)=>{let e=t(n);return !e.success&&n==null&&(e.success=true,e.data=n),e}),this},default(t){return i(this,"_parse",(n,e)=>(e===void 0&&(e=t,e=typeof t=="function"?t():t),n(e))),this},pipe(t){return i(this,"_parse",(n,e)=>{let u=n(e);return u.success?t._parse(u.data):u}),this},refine(t,{message:n}){return i(this,"_parse",(e,u)=>{let m=e(u);return t(m.data)?m:{success:false,error:{message:n}}}),this}};return f.length>0?f.reduce((t,n)=>n(t,r,o)??t,c):c}var f=[];function g(r){f.push(r);}exports.extend=g;exports.s=b;
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var h=r=>typeof r=="string"||r instanceof String,
|
|
1
|
+
var h=r=>typeof r=="string"||r instanceof String,l=r=>typeof r=="number"||r instanceof Number,y=r=>r===true||r===false,I=r=>r instanceof Date&&!Number.isNaN(r.getTime()),d=r=>Array.isArray(r),S=r=>typeof r=="object"&&r!==null&&!Array.isArray(r),b={object(r){let a=p(S,{type:"object"});return i(a,"_parse",(s,...o)=>{let c=s(...o);if(c.success===false)return c;let t={};for(let n in r){let e=r[n]._parse(c.data[n]);if(e.success)t[n]=e.data;else {e=e;let u=e?.error?.cause?.key?`${n}.${e?.error?.cause?.key}`:n;return e.error.message=`Error parsing key "${u}": ${e.error.message}`,e.error.cause={key:u},e}}return {success:true,data:t}}),a},string(){return p(h,{type:"string"})},number(){return p(l,{type:"number"})},boolean(){return p(y,{type:"boolean"})},date(){return p(I,{type:"date"})},enum(r){let a=t=>r.includes(t),s=t=>`Invalid ${o} value. Expected ${r.map(n=>`"${n}"`).join(" | ")}, received "${t}".`,o="enum";return p(a,{type:o,message:s})},array(r){let a=p(d,{type:"array"});return i(a,"_parse",(s,...o)=>{let c=s(...o);if(c.success===false)return c;let t=[];for(let n=0;n<c.data.length;n++){let e=r._parse(c.data[n]);if(e.success)t.push(e.data);else {e=e;let u=e.error?.cause?.key?`${n}.${e.error.cause.key}`:`${n}`;return e.error.message=`Error parsing index "${n}": ${e.error.message}`,e.error.cause={key:u},e}}return {success:true,data:t}}),a},any(){return p(()=>true)},preprocess(r,a){return i(a,"_parse",(s,o)=>(o=r(o),s(o))),a},union(r){return p(c=>{for(let t=0;t<r.length;t++)if(r[t]._parse(c).success)return true;return false},{type:"union",message:c=>`Invalid union value. Expected the value to match one of the schemas: ${r.map(t=>`"${t._getType()}"`).join(" | ")}, but received "${typeof c}" with value "${c}".`})}};function T(r){return a=>`The value "${a}" must be type of ${r} but is type of "${typeof a}".`}function i(r,a,s){let o=r[a];r[a]=(...c)=>s(o,...c);}function p(r,{type:a="any",message:s}={}){s=s||T(a);let o={message:s,type:a},c={_getType(){return a},_parse(t){return r(t)?{success:true,data:t}:{success:false,error:{message:s(t)}}},parse(t){let n=c._parse(t);if(!n.success)throw n=n,new Error(n.error.message,{cause:n.error.cause});return n.data},safeParse(t){return c._parse(t)},transform(t){return i(this,"_parse",(n,e)=>{let u=n(e);return u.success&&(u.data=t(u.data)),u}),this},optional(){return i(this,"_parse",(t,n)=>{let e=t(n);return e.success||(e.data=void 0,e.success=true),e}),this},nullable(){return i(this,"_parse",(t,n)=>{let e=t(n);return e.success||(e.data=null,e.success=true),e}),this},nullish(){return i(this,"_parse",(t,n)=>{let e=t(n);return !e.success&&n==null&&(e.success=true,e.data=n),e}),this},default(t){return i(this,"_parse",(n,e)=>(e===void 0&&(e=t,e=typeof t=="function"?t():t),n(e))),this},pipe(t){return i(this,"_parse",(n,e)=>{let u=n(e);return u.success?t._parse(u.data):u}),this},refine(t,{message:n}){return i(this,"_parse",(e,u)=>{let m=e(u);return t(m.data)?m:{success:false,error:{message:n}}}),this}};return f.length>0?f.reduce((t,n)=>n(t,r,o)??t,c):c}var f=[];function g(r){f.push(r);}export{g as extend,b as s};
|