@esmj/schema 0.3.0 → 0.3.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.
- package/README.md +59 -43
- package/dist/index.d.mts +17 -11
- package/dist/index.d.ts +17 -11
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -130,88 +130,87 @@ console.log(result);
|
|
|
130
130
|
|
|
131
131
|
### Schema Types
|
|
132
132
|
|
|
133
|
-
#### `s.string()`
|
|
133
|
+
#### `s.string(options?)`
|
|
134
134
|
|
|
135
|
-
Creates a string schema.
|
|
135
|
+
Creates a string schema. You can optionally pass `SchemaInterfaceOptions` to customize error messages.
|
|
136
136
|
|
|
137
137
|
```typescript
|
|
138
|
-
const stringSchema = s.string(
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
#### `s.number()`
|
|
142
|
-
|
|
143
|
-
Creates a number schema.
|
|
144
|
-
|
|
145
|
-
```typescript
|
|
146
|
-
const numberSchema = s.number();
|
|
138
|
+
const stringSchema = s.string({
|
|
139
|
+
message: (value) => `Custom error: "${value}" is not a valid string.`,
|
|
140
|
+
});
|
|
147
141
|
```
|
|
148
142
|
|
|
149
|
-
#### `s.
|
|
143
|
+
#### `s.number(options?)`
|
|
150
144
|
|
|
151
|
-
Creates a
|
|
145
|
+
Creates a number schema. You can optionally pass `SchemaInterfaceOptions` to customize error messages.
|
|
152
146
|
|
|
153
147
|
```typescript
|
|
154
|
-
const
|
|
148
|
+
const numberSchema = s.number({
|
|
149
|
+
message: (value) => `Custom error: "${value}" is not a valid number.`,
|
|
150
|
+
});
|
|
155
151
|
```
|
|
156
152
|
|
|
157
|
-
#### `s.
|
|
153
|
+
#### `s.boolean(options?)`
|
|
158
154
|
|
|
159
|
-
Creates a
|
|
155
|
+
Creates a boolean schema. You can optionally pass `SchemaInterfaceOptions` to customize error messages.
|
|
160
156
|
|
|
161
157
|
```typescript
|
|
162
|
-
const
|
|
158
|
+
const booleanSchema = s.boolean({
|
|
159
|
+
message: (value) => `Custom error: "${value}" is not a valid boolean.`,
|
|
160
|
+
});
|
|
163
161
|
```
|
|
164
162
|
|
|
165
|
-
#### `s.
|
|
163
|
+
#### `s.date(options?)`
|
|
166
164
|
|
|
167
|
-
Creates
|
|
165
|
+
Creates a date schema. You can optionally pass `SchemaInterfaceOptions` to customize error messages.
|
|
168
166
|
|
|
169
167
|
```typescript
|
|
170
|
-
const
|
|
171
|
-
|
|
172
|
-
value: s.number(),
|
|
168
|
+
const dateSchema = s.date({
|
|
169
|
+
message: (value) => `Custom error: "${value}" is not a valid date.`,
|
|
173
170
|
});
|
|
174
171
|
```
|
|
175
172
|
|
|
176
|
-
#### `s.
|
|
173
|
+
#### `s.object(definition, options?)`
|
|
177
174
|
|
|
178
|
-
Creates an
|
|
175
|
+
Creates an object schema with the given definition. You can optionally pass `SchemaInterfaceOptions` to customize error messages.
|
|
179
176
|
|
|
180
177
|
```typescript
|
|
181
|
-
const
|
|
178
|
+
const objectSchema = s.object(
|
|
179
|
+
{
|
|
180
|
+
key: s.string(),
|
|
181
|
+
value: s.number(),
|
|
182
|
+
},
|
|
183
|
+
{
|
|
184
|
+
message: (value) => `Custom error: "${JSON.stringify(value)}" is not a valid object.`,
|
|
185
|
+
},
|
|
186
|
+
);
|
|
182
187
|
```
|
|
183
188
|
|
|
184
|
-
#### `s.
|
|
189
|
+
#### `s.array(definition, options?)`
|
|
185
190
|
|
|
186
|
-
Creates
|
|
191
|
+
Creates an array schema with the given item definition. You can optionally pass `SchemaInterfaceOptions` to customize error messages.
|
|
187
192
|
|
|
188
193
|
```typescript
|
|
189
|
-
const
|
|
194
|
+
const arraySchema = s.array(s.string(), {
|
|
195
|
+
message: (value) => `Custom error: "${JSON.stringify(value)}" is not a valid array.`,
|
|
196
|
+
});
|
|
190
197
|
```
|
|
191
198
|
|
|
192
|
-
#### `s.enum(values)`
|
|
199
|
+
#### `s.enum(values, options?)`
|
|
193
200
|
|
|
194
|
-
Creates an enum schema that validates against a predefined set of string values.
|
|
201
|
+
Creates an enum schema that validates against a predefined set of string values. You can optionally pass `SchemaInterfaceOptions` to customize error messages.
|
|
195
202
|
|
|
196
203
|
- **`values`**: An array of strings representing the allowed values for the enum. Each value must be a string.
|
|
197
204
|
|
|
198
205
|
```typescript
|
|
199
|
-
const enumSchema = s.enum(['admin', 'user', 'guest']
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
console.log(validResult);
|
|
203
|
-
// 'admin'
|
|
204
|
-
|
|
205
|
-
const invalidResult = enumSchema.safeParse('invalidRole');
|
|
206
|
-
console.log(invalidResult.success);
|
|
207
|
-
// false
|
|
208
|
-
console.log(invalidResult.error.message);
|
|
209
|
-
// Error: Invalid enum value. Expected "admin" | "user" | "guest", received "invalidRole".
|
|
206
|
+
const enumSchema = s.enum(['admin', 'user', 'guest'], {
|
|
207
|
+
message: (value) => `Custom error: "${value}" is not a valid enum value.`,
|
|
208
|
+
});
|
|
210
209
|
```
|
|
211
210
|
|
|
212
|
-
#### `s.union(definitions)`
|
|
211
|
+
#### `s.union(definitions, options?)`
|
|
213
212
|
|
|
214
|
-
Creates a schema that validates against multiple schemas (a union of schemas). The value must match at least one of the provided schemas.
|
|
213
|
+
Creates a schema that validates against multiple schemas (a union of schemas). The value must match at least one of the provided schemas. You can optionally pass `SchemaInterfaceOptions` to customize error messages.
|
|
215
214
|
|
|
216
215
|
- **`definitions`**: An array of schemas to validate against.
|
|
217
216
|
|
|
@@ -243,6 +242,23 @@ console.log(invalidValue.error.message);
|
|
|
243
242
|
|
|
244
243
|
**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
244
|
|
|
245
|
+
```typescript
|
|
246
|
+
const schema = s.union(
|
|
247
|
+
[s.string(), s.number(), s.boolean()],
|
|
248
|
+
{
|
|
249
|
+
message: (value) => `Custom error: "${value}" does not match any of the union schemas.`,
|
|
250
|
+
},
|
|
251
|
+
);
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
#### `s.any()`
|
|
255
|
+
|
|
256
|
+
Creates a schema that accepts any value.
|
|
257
|
+
|
|
258
|
+
```typescript
|
|
259
|
+
const anySchema = s.any();
|
|
260
|
+
```
|
|
261
|
+
|
|
246
262
|
#### `s.preprocess(callback, schema)`
|
|
247
263
|
|
|
248
264
|
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
|
@@ -25,7 +25,7 @@ interface SchemaInterface<Input, Output> {
|
|
|
25
25
|
default(defaultValue: Partial<Input> | (() => Partial<Input>) | Partial<Output>): SchemaInterface<Input, Output>;
|
|
26
26
|
pipe<NewOutput>(schema: SchemaInterface<Output, NewOutput>): SchemaInterface<Output, NewOutput>;
|
|
27
27
|
refine(validation: (value: Output) => boolean, { message }: {
|
|
28
|
-
message:
|
|
28
|
+
message: ErrorMessage;
|
|
29
29
|
}): SchemaInterface<Input, Output>;
|
|
30
30
|
}
|
|
31
31
|
interface UnionSchemaInterface<T extends Array<SchemaInterface<unknown, unknown>>> extends SchemaInterface<ReturnType<T[number]['parse']>, ReturnType<T[number]['parse']>> {
|
|
@@ -49,23 +49,29 @@ interface ObjectSchemaInterface<T extends Record<string, SchemaType>> extends Sc
|
|
|
49
49
|
}> {
|
|
50
50
|
}
|
|
51
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>;
|
|
52
|
+
type ErrorMessage = string | ((value: unknown) => string);
|
|
52
53
|
type ExtenderType = (inter: SchemaType, validation: Function, options: {
|
|
53
|
-
message:
|
|
54
|
+
message: ErrorMessage;
|
|
54
55
|
type: string;
|
|
55
56
|
}) => SchemaType;
|
|
57
|
+
interface CreateSchemaInterfaceOptions {
|
|
58
|
+
type?: string;
|
|
59
|
+
message?: (value: unknown) => string;
|
|
60
|
+
}
|
|
61
|
+
type SchemaInterfaceOptions = Omit<CreateSchemaInterfaceOptions, 'type'>;
|
|
56
62
|
declare const s: {
|
|
57
|
-
object<T extends Record<string, SchemaType>>(definition: { [Property in keyof T]: T[Property]; }): ObjectSchemaInterface<T>;
|
|
58
|
-
string(): StringSchemaInterface;
|
|
59
|
-
number(): NumberSchemaInterface;
|
|
60
|
-
boolean(): BooleanSchemaInterface;
|
|
61
|
-
date(): DateSchemaInterface;
|
|
62
|
-
enum(definition: Readonly<Array<string
|
|
63
|
-
array<T extends SchemaType>(definition: T): ArraySchemaInterface<T>;
|
|
63
|
+
object<T extends Record<string, SchemaType>>(definition: { [Property in keyof T]: T[Property]; }, options: SchemaInterfaceOptions): ObjectSchemaInterface<T>;
|
|
64
|
+
string(options: SchemaInterfaceOptions): StringSchemaInterface;
|
|
65
|
+
number(options: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
66
|
+
boolean(options: SchemaInterfaceOptions): BooleanSchemaInterface;
|
|
67
|
+
date(options: SchemaInterfaceOptions): DateSchemaInterface;
|
|
68
|
+
enum(definition: Readonly<Array<string>>, options: SchemaInterfaceOptions): EnumSchemaInterface<(typeof definition)[number]>;
|
|
69
|
+
array<T extends SchemaType>(definition: T, options: SchemaInterfaceOptions): ArraySchemaInterface<T>;
|
|
64
70
|
any(): any;
|
|
65
71
|
preprocess<T extends SchemaType>(callback: Function, schema: T): T;
|
|
66
|
-
union<T extends Array<SchemaType>>(definitions: T): UnionSchemaInterface<T>;
|
|
72
|
+
union<T extends Array<SchemaType>>(definitions: T, options: SchemaInterfaceOptions): UnionSchemaInterface<T>;
|
|
67
73
|
};
|
|
68
74
|
declare function extend(callback: ExtenderType): void;
|
|
69
75
|
type Infer<T> = T extends SchemaType ? ReturnType<T['parse']> : unknown;
|
|
70
76
|
|
|
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 };
|
|
77
|
+
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.d.ts
CHANGED
|
@@ -25,7 +25,7 @@ interface SchemaInterface<Input, Output> {
|
|
|
25
25
|
default(defaultValue: Partial<Input> | (() => Partial<Input>) | Partial<Output>): SchemaInterface<Input, Output>;
|
|
26
26
|
pipe<NewOutput>(schema: SchemaInterface<Output, NewOutput>): SchemaInterface<Output, NewOutput>;
|
|
27
27
|
refine(validation: (value: Output) => boolean, { message }: {
|
|
28
|
-
message:
|
|
28
|
+
message: ErrorMessage;
|
|
29
29
|
}): SchemaInterface<Input, Output>;
|
|
30
30
|
}
|
|
31
31
|
interface UnionSchemaInterface<T extends Array<SchemaInterface<unknown, unknown>>> extends SchemaInterface<ReturnType<T[number]['parse']>, ReturnType<T[number]['parse']>> {
|
|
@@ -49,23 +49,29 @@ interface ObjectSchemaInterface<T extends Record<string, SchemaType>> extends Sc
|
|
|
49
49
|
}> {
|
|
50
50
|
}
|
|
51
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>;
|
|
52
|
+
type ErrorMessage = string | ((value: unknown) => string);
|
|
52
53
|
type ExtenderType = (inter: SchemaType, validation: Function, options: {
|
|
53
|
-
message:
|
|
54
|
+
message: ErrorMessage;
|
|
54
55
|
type: string;
|
|
55
56
|
}) => SchemaType;
|
|
57
|
+
interface CreateSchemaInterfaceOptions {
|
|
58
|
+
type?: string;
|
|
59
|
+
message?: (value: unknown) => string;
|
|
60
|
+
}
|
|
61
|
+
type SchemaInterfaceOptions = Omit<CreateSchemaInterfaceOptions, 'type'>;
|
|
56
62
|
declare const s: {
|
|
57
|
-
object<T extends Record<string, SchemaType>>(definition: { [Property in keyof T]: T[Property]; }): ObjectSchemaInterface<T>;
|
|
58
|
-
string(): StringSchemaInterface;
|
|
59
|
-
number(): NumberSchemaInterface;
|
|
60
|
-
boolean(): BooleanSchemaInterface;
|
|
61
|
-
date(): DateSchemaInterface;
|
|
62
|
-
enum(definition: Readonly<Array<string
|
|
63
|
-
array<T extends SchemaType>(definition: T): ArraySchemaInterface<T>;
|
|
63
|
+
object<T extends Record<string, SchemaType>>(definition: { [Property in keyof T]: T[Property]; }, options: SchemaInterfaceOptions): ObjectSchemaInterface<T>;
|
|
64
|
+
string(options: SchemaInterfaceOptions): StringSchemaInterface;
|
|
65
|
+
number(options: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
66
|
+
boolean(options: SchemaInterfaceOptions): BooleanSchemaInterface;
|
|
67
|
+
date(options: SchemaInterfaceOptions): DateSchemaInterface;
|
|
68
|
+
enum(definition: Readonly<Array<string>>, options: SchemaInterfaceOptions): EnumSchemaInterface<(typeof definition)[number]>;
|
|
69
|
+
array<T extends SchemaType>(definition: T, options: SchemaInterfaceOptions): ArraySchemaInterface<T>;
|
|
64
70
|
any(): any;
|
|
65
71
|
preprocess<T extends SchemaType>(callback: Function, schema: T): T;
|
|
66
|
-
union<T extends Array<SchemaType>>(definitions: T): UnionSchemaInterface<T>;
|
|
72
|
+
union<T extends Array<SchemaType>>(definitions: T, options: SchemaInterfaceOptions): UnionSchemaInterface<T>;
|
|
67
73
|
};
|
|
68
74
|
declare function extend(callback: ExtenderType): void;
|
|
69
75
|
type Infer<T> = T extends SchemaType ? ReturnType<T['parse']> : unknown;
|
|
70
76
|
|
|
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 };
|
|
77
|
+
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
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var h=
|
|
1
|
+
'use strict';var h=e=>typeof e=="string"||e instanceof String,I=e=>typeof e=="number"||e instanceof Number,l=e=>e===true||e===false,y=e=>e instanceof Date&&!Number.isNaN(e.getTime()),S=e=>Array.isArray(e),d=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),g={object(e,c){let s=p(d,{...c,type:"object"});return i(s,"_parse",(u,...o)=>{let t=u(...o);if(t.success===false)return t;let n={};for(let r in e){let a=e[r]._parse(t.data[r]);if(a.success)n[r]=a.data;else {a=a;let f=a?.error?.cause?.key?`${r}.${a?.error?.cause?.key}`:r;return a.error.message=`Error parsing key "${f}": ${a.error.message}`,a.error.cause={key:f},a}}return {success:true,data:n}}),s},string(e){return p(h,{...e,type:"string"})},number(e){return p(I,{...e,type:"number"})},boolean(e){return p(l,{...e,type:"boolean"})},date(e){return p(y,{...e,type:"date"})},enum(e,c){let s=n=>e.includes(n),u=n=>`Invalid ${o} value. Expected ${e.map(r=>`"${r}"`).join(" | ")}, received "${n}".`,o="enum";return p(s,{message:u,...c,type:o})},array(e,c){let s=p(S,{...c,type:"array"});return i(s,"_parse",(u,...o)=>{let t=u(...o);if(t.success===false)return t;let n=[];for(let r=0;r<t.data.length;r++){let a=e._parse(t.data[r]);if(a.success)n.push(a.data);else {a=a;let f=a.error?.cause?.key?`${r}.${a.error.cause.key}`:`${r}`;return a.error.message=`Error parsing index "${r}": ${a.error.message}`,a.error.cause={key:f},a}}return {success:true,data:n}}),s},any(){return p(()=>true)},preprocess(e,c){return i(c,"_parse",(s,u)=>(u=e(u),s(u))),c},union(e,c){return p(t=>{for(let n=0;n<e.length;n++)if(e[n]._parse(t).success)return true;return false},{message:t=>`Invalid union value. Expected the value to match one of the schemas: ${e.map(n=>`"${n._getType()}"`).join(" | ")}, but received "${typeof t}" with value "${t}".`,...c,type:"union"})}};function T(e){return c=>`The value "${c}" must be type of ${e} but is type of "${typeof c}".`}function i(e,c,s){let u=e[c];e[c]=(...o)=>s(u,...o);}function p(e,{type:c="any",message:s}={}){s=s||T(c);let u={message:s,type:c},o={_getType(){return c},_parse(t){return e(t)?{success:true,data:t}:{success:false,error:{message:s(t)}}},parse(t){let n=o._parse(t);if(!n.success)throw n=n,new Error(n.error.message,{cause:n.error.cause});return n.data},safeParse(t){return o._parse(t)},transform(t){return i(this,"_parse",(n,r)=>{let a=n(r);return a.success&&(a.data=t(a.data)),a}),this},optional(){return i(this,"_parse",(t,n)=>{let r=t(n);return r.success||(r.data=void 0,r.success=true),r}),this},nullable(){return i(this,"_parse",(t,n)=>{let r=t(n);return r.success||(r.data=null,r.success=true),r}),this},nullish(){return i(this,"_parse",(t,n)=>{let r=t(n);return !r.success&&n==null&&(r.success=true,r.data=n),r}),this},default(t){return i(this,"_parse",(n,r)=>(r===void 0&&(r=t,r=typeof t=="function"?t():t),n(r))),this},pipe(t){return i(this,"_parse",(n,r)=>{let a=n(r);return a.success?t._parse(a.data):a}),this},refine(t,{message:n}){return i(this,"_parse",(r,a)=>{let f=r(a);return t(f.data)?f:{success:false,error:{message:n}}}),this}};return m.length>0?m.reduce((t,n)=>n(t,e,u)??t,o):o}var m=[];function b(e){m.push(e);}exports.extend=b;exports.s=g;
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var h=
|
|
1
|
+
var h=e=>typeof e=="string"||e instanceof String,I=e=>typeof e=="number"||e instanceof Number,l=e=>e===true||e===false,y=e=>e instanceof Date&&!Number.isNaN(e.getTime()),S=e=>Array.isArray(e),d=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),g={object(e,c){let s=p(d,{...c,type:"object"});return i(s,"_parse",(u,...o)=>{let t=u(...o);if(t.success===false)return t;let n={};for(let r in e){let a=e[r]._parse(t.data[r]);if(a.success)n[r]=a.data;else {a=a;let f=a?.error?.cause?.key?`${r}.${a?.error?.cause?.key}`:r;return a.error.message=`Error parsing key "${f}": ${a.error.message}`,a.error.cause={key:f},a}}return {success:true,data:n}}),s},string(e){return p(h,{...e,type:"string"})},number(e){return p(I,{...e,type:"number"})},boolean(e){return p(l,{...e,type:"boolean"})},date(e){return p(y,{...e,type:"date"})},enum(e,c){let s=n=>e.includes(n),u=n=>`Invalid ${o} value. Expected ${e.map(r=>`"${r}"`).join(" | ")}, received "${n}".`,o="enum";return p(s,{message:u,...c,type:o})},array(e,c){let s=p(S,{...c,type:"array"});return i(s,"_parse",(u,...o)=>{let t=u(...o);if(t.success===false)return t;let n=[];for(let r=0;r<t.data.length;r++){let a=e._parse(t.data[r]);if(a.success)n.push(a.data);else {a=a;let f=a.error?.cause?.key?`${r}.${a.error.cause.key}`:`${r}`;return a.error.message=`Error parsing index "${r}": ${a.error.message}`,a.error.cause={key:f},a}}return {success:true,data:n}}),s},any(){return p(()=>true)},preprocess(e,c){return i(c,"_parse",(s,u)=>(u=e(u),s(u))),c},union(e,c){return p(t=>{for(let n=0;n<e.length;n++)if(e[n]._parse(t).success)return true;return false},{message:t=>`Invalid union value. Expected the value to match one of the schemas: ${e.map(n=>`"${n._getType()}"`).join(" | ")}, but received "${typeof t}" with value "${t}".`,...c,type:"union"})}};function T(e){return c=>`The value "${c}" must be type of ${e} but is type of "${typeof c}".`}function i(e,c,s){let u=e[c];e[c]=(...o)=>s(u,...o);}function p(e,{type:c="any",message:s}={}){s=s||T(c);let u={message:s,type:c},o={_getType(){return c},_parse(t){return e(t)?{success:true,data:t}:{success:false,error:{message:s(t)}}},parse(t){let n=o._parse(t);if(!n.success)throw n=n,new Error(n.error.message,{cause:n.error.cause});return n.data},safeParse(t){return o._parse(t)},transform(t){return i(this,"_parse",(n,r)=>{let a=n(r);return a.success&&(a.data=t(a.data)),a}),this},optional(){return i(this,"_parse",(t,n)=>{let r=t(n);return r.success||(r.data=void 0,r.success=true),r}),this},nullable(){return i(this,"_parse",(t,n)=>{let r=t(n);return r.success||(r.data=null,r.success=true),r}),this},nullish(){return i(this,"_parse",(t,n)=>{let r=t(n);return !r.success&&n==null&&(r.success=true,r.data=n),r}),this},default(t){return i(this,"_parse",(n,r)=>(r===void 0&&(r=t,r=typeof t=="function"?t():t),n(r))),this},pipe(t){return i(this,"_parse",(n,r)=>{let a=n(r);return a.success?t._parse(a.data):a}),this},refine(t,{message:n}){return i(this,"_parse",(r,a)=>{let f=r(a);return t(f.data)?f:{success:false,error:{message:n}}}),this}};return m.length>0?m.reduce((t,n)=>n(t,e,u)??t,o):o}var m=[];function b(e){m.push(e);}export{b as extend,g as s};
|