@esmj/schema 0.7.2 → 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 +99 -20
- 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-5XWCCG75.mjs +1 -0
- package/dist/chunk-NCRS6NBV.mjs +1 -0
- package/dist/chunk-NRGPACGD.mjs +1 -0
- 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 +41 -250
- package/dist/index.d.ts +41 -250
- 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 +16 -6
- package/dist/chunk-3C2I2ZOZ.mjs +0 -1
- package/dist/chunk-G6KRMWVT.mjs +0 -1
- package/dist/chunk-GJNOESJT.mjs +0 -1
- package/dist/chunk-QIW3OXV4.mjs +0 -1
package/dist/index.d.ts
CHANGED
|
@@ -19,7 +19,7 @@ interface ParseOptions {
|
|
|
19
19
|
abortEarly?: boolean;
|
|
20
20
|
}
|
|
21
21
|
interface SchemaInterface<Input, Output> {
|
|
22
|
-
_getName(): string;
|
|
22
|
+
_getName(): undefined | string;
|
|
23
23
|
_getType(): string;
|
|
24
24
|
_getDescription(): string;
|
|
25
25
|
_parse(value: Input | Partial<Input>, options?: ParseOptions): InternalParseOutput<Output>;
|
|
@@ -51,6 +51,8 @@ interface BooleanSchemaInterface extends SchemaInterface<boolean, boolean> {
|
|
|
51
51
|
}
|
|
52
52
|
interface DateSchemaInterface extends SchemaInterface<Date, Date> {
|
|
53
53
|
}
|
|
54
|
+
interface FunctionSchemaInterface extends SchemaInterface<Function, Function> {
|
|
55
|
+
}
|
|
54
56
|
interface ArraySchemaInterface<T extends SchemaType> extends SchemaInterface<Array<ReturnType<T['parse']>>, Array<ReturnType<T['parse']>>> {
|
|
55
57
|
}
|
|
56
58
|
interface ObjectSchemaInterface<T extends Record<string, SchemaType>> extends SchemaInterface<{
|
|
@@ -59,7 +61,7 @@ interface ObjectSchemaInterface<T extends Record<string, SchemaType>> extends Sc
|
|
|
59
61
|
[Property in keyof T]: ReturnType<T[Property]['parse']>;
|
|
60
62
|
}> {
|
|
61
63
|
}
|
|
62
|
-
type SchemaType = LiteralSchemaInterface<string> | LiteralSchemaInterface<number> | LiteralSchemaInterface<boolean> | 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>;
|
|
64
|
+
type SchemaType = LiteralSchemaInterface<string> | LiteralSchemaInterface<number> | LiteralSchemaInterface<boolean> | 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> | FunctionSchemaInterface | 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>;
|
|
63
65
|
type ErrorMessage = string | ((value: unknown) => string);
|
|
64
66
|
type ExtenderType = (inter: SchemaType, validation: Function, options?: {
|
|
65
67
|
message: ErrorMessage;
|
|
@@ -71,268 +73,57 @@ interface CreateSchemaInterfaceOptions {
|
|
|
71
73
|
message?: ErrorMessage;
|
|
72
74
|
}
|
|
73
75
|
type SchemaInterfaceOptions = Omit<CreateSchemaInterfaceOptions, 'type'>;
|
|
74
|
-
declare
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
*/
|
|
90
|
-
object<T extends Record<string, SchemaType>>(definition: { [Property in keyof T]: T[Property]; }, options?: SchemaInterfaceOptions): ObjectSchemaInterface<T>;
|
|
91
|
-
/**
|
|
92
|
-
* Creates a string schema.
|
|
93
|
-
*
|
|
94
|
-
* @param options - Optional configuration (name, message)
|
|
95
|
-
* @returns String schema interface
|
|
96
|
-
*
|
|
97
|
-
* @example
|
|
98
|
-
* ```typescript
|
|
99
|
-
* const nameSchema = s.string();
|
|
100
|
-
* const result = nameSchema.parse('John'); // 'John'
|
|
101
|
-
* ```
|
|
102
|
-
*/
|
|
76
|
+
declare function object<T extends Record<string, SchemaType>>(definition: {
|
|
77
|
+
[Property in keyof T]: T[Property];
|
|
78
|
+
}, options?: SchemaInterfaceOptions): ObjectSchemaInterface<T>;
|
|
79
|
+
declare function string(options?: SchemaInterfaceOptions): StringSchemaInterface;
|
|
80
|
+
declare function number(options?: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
81
|
+
declare function boolean(options?: SchemaInterfaceOptions): BooleanSchemaInterface;
|
|
82
|
+
declare function date(options?: SchemaInterfaceOptions): DateSchemaInterface;
|
|
83
|
+
declare function functionSchema(options?: SchemaInterfaceOptions): FunctionSchemaInterface;
|
|
84
|
+
declare function enumSchema(definition: Readonly<Array<string>>, options?: SchemaInterfaceOptions): EnumSchemaInterface<(typeof definition)[number]>;
|
|
85
|
+
declare function array<T extends SchemaType>(definition: T, options?: SchemaInterfaceOptions): ArraySchemaInterface<T>;
|
|
86
|
+
declare function any(): SchemaInterface<unknown, unknown>;
|
|
87
|
+
declare function preprocess<T extends SchemaType>(callback: Function, schema: T): T;
|
|
88
|
+
declare function union<T extends Array<SchemaType>>(definitions: T, options?: SchemaInterfaceOptions): UnionSchemaInterface<T>;
|
|
89
|
+
declare function literal<T extends string | number | boolean>(value: T, options?: SchemaInterfaceOptions): LiteralSchemaInterface<T>;
|
|
90
|
+
declare const coerce: {
|
|
103
91
|
string(options?: SchemaInterfaceOptions): StringSchemaInterface;
|
|
104
|
-
/**
|
|
105
|
-
* Creates a number schema.
|
|
106
|
-
*
|
|
107
|
-
* @param options - Optional configuration (name, message)
|
|
108
|
-
* @returns Number schema interface
|
|
109
|
-
*
|
|
110
|
-
* @example
|
|
111
|
-
* ```typescript
|
|
112
|
-
* const ageSchema = s.number();
|
|
113
|
-
* const result = ageSchema.parse(25); // 25
|
|
114
|
-
* ```
|
|
115
|
-
*/
|
|
116
92
|
number(options?: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
117
|
-
/**
|
|
118
|
-
* Creates a boolean schema.
|
|
119
|
-
*
|
|
120
|
-
* @param options - Optional configuration (name, message)
|
|
121
|
-
* @returns Boolean schema interface
|
|
122
|
-
*
|
|
123
|
-
* @example
|
|
124
|
-
* ```typescript
|
|
125
|
-
* const isActiveSchema = s.boolean();
|
|
126
|
-
* const result = isActiveSchema.parse(true); // true
|
|
127
|
-
* ```
|
|
128
|
-
*/
|
|
129
93
|
boolean(options?: SchemaInterfaceOptions): BooleanSchemaInterface;
|
|
130
|
-
/**
|
|
131
|
-
* Creates a date schema.
|
|
132
|
-
*
|
|
133
|
-
* @param options - Optional configuration (name, message)
|
|
134
|
-
* @returns Date schema interface
|
|
135
|
-
*
|
|
136
|
-
* @example
|
|
137
|
-
* ```typescript
|
|
138
|
-
* const birthdateSchema = s.date();
|
|
139
|
-
* const result = birthdateSchema.parse(new Date()); // Date object
|
|
140
|
-
* ```
|
|
141
|
-
*/
|
|
142
94
|
date(options?: SchemaInterfaceOptions): DateSchemaInterface;
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
* @example
|
|
165
|
-
* ```typescript
|
|
166
|
-
* const tagsSchema = s.array(s.string());
|
|
167
|
-
* const result = tagsSchema.parse(['tag1', 'tag2']); // ['tag1', 'tag2']
|
|
168
|
-
* ```
|
|
169
|
-
*/
|
|
170
|
-
array<T extends SchemaType>(definition: T, options?: SchemaInterfaceOptions): ArraySchemaInterface<T>;
|
|
171
|
-
/**
|
|
172
|
-
* Creates a schema that accepts any value without validation.
|
|
173
|
-
*
|
|
174
|
-
* @returns Schema interface that accepts any value
|
|
175
|
-
*
|
|
176
|
-
* @example
|
|
177
|
-
* ```typescript
|
|
178
|
-
* const anySchema = s.any();
|
|
179
|
-
* const result = anySchema.parse({ anything: true }); // { anything: true }
|
|
180
|
-
* ```
|
|
181
|
-
*/
|
|
182
|
-
any(): any;
|
|
183
|
-
/**
|
|
184
|
-
* Preprocesses a value before passing it to a schema for validation.
|
|
185
|
-
*
|
|
186
|
-
* @param callback - Function to transform the value before validation
|
|
187
|
-
* @param schema - Schema to validate the transformed value
|
|
188
|
-
* @returns Modified schema with preprocessing
|
|
189
|
-
*
|
|
190
|
-
* @example
|
|
191
|
-
* ```typescript
|
|
192
|
-
* const schema = s.preprocess(
|
|
193
|
-
* (val) => String(val).trim(),
|
|
194
|
-
* s.string().min(3)
|
|
195
|
-
* );
|
|
196
|
-
* const result = schema.parse(' hello '); // 'hello'
|
|
197
|
-
* ```
|
|
198
|
-
*/
|
|
199
|
-
preprocess<T extends SchemaType>(callback: Function, schema: T): T;
|
|
200
|
-
/**
|
|
201
|
-
* Creates a union schema that validates against multiple schemas.
|
|
202
|
-
* The value must match at least one of the provided schemas.
|
|
203
|
-
*
|
|
204
|
-
* @param definitions - Array of schemas to validate against
|
|
205
|
-
* @param options - Optional configuration (name, message)
|
|
206
|
-
* @returns Union schema interface
|
|
207
|
-
*
|
|
208
|
-
* @example
|
|
209
|
-
* ```typescript
|
|
210
|
-
* const idSchema = s.union([s.string(), s.number()]);
|
|
211
|
-
* const result1 = idSchema.parse('abc'); // 'abc'
|
|
212
|
-
* const result2 = idSchema.parse(123); // 123
|
|
213
|
-
* ```
|
|
214
|
-
*/
|
|
215
|
-
union<T extends Array<SchemaType>>(definitions: T, options?: SchemaInterfaceOptions): UnionSchemaInterface<T>;
|
|
216
|
-
/**
|
|
217
|
-
* Creates a literal schema that only accepts a specific value.
|
|
218
|
-
*
|
|
219
|
-
* @param value - The exact value to match
|
|
220
|
-
* @param options - Optional configuration
|
|
221
|
-
* @returns Literal schema interface
|
|
222
|
-
*
|
|
223
|
-
* @example
|
|
224
|
-
* ```typescript
|
|
225
|
-
* const adminSchema = s.literal('admin');
|
|
226
|
-
* adminSchema.parse('admin'); // 'admin'
|
|
227
|
-
* adminSchema.parse('user'); // throws error
|
|
228
|
-
* ```
|
|
229
|
-
*/
|
|
230
|
-
literal<T extends string | number | boolean>(value: T, options?: SchemaInterfaceOptions): LiteralSchemaInterface<T>;
|
|
231
|
-
/**
|
|
232
|
-
* Coerce schemas that apply a native JS constructor before validation.
|
|
233
|
-
* Unlike `s.preprocess`, coerce provides a consistent API for common type
|
|
234
|
-
* conversions with clear error messages when coercion produces an invalid result.
|
|
235
|
-
*
|
|
236
|
-
* @example
|
|
237
|
-
* ```typescript
|
|
238
|
-
* s.coerce.number().parse('42'); // 42
|
|
239
|
-
* s.coerce.string().parse(123); // '123'
|
|
240
|
-
* s.coerce.boolean().parse(0); // false
|
|
241
|
-
* s.coerce.date().parse('2024-01-01'); // Date object
|
|
242
|
-
* ```
|
|
243
|
-
*/
|
|
95
|
+
};
|
|
96
|
+
declare const cast: {
|
|
97
|
+
boolean(options?: SchemaInterfaceOptions): BooleanSchemaInterface;
|
|
98
|
+
number(options?: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
99
|
+
string(options?: SchemaInterfaceOptions): StringSchemaInterface;
|
|
100
|
+
date(options?: SchemaInterfaceOptions): DateSchemaInterface;
|
|
101
|
+
json<T extends SchemaType>(schema: T, options?: SchemaInterfaceOptions): T;
|
|
102
|
+
};
|
|
103
|
+
declare const s: {
|
|
104
|
+
object: typeof object;
|
|
105
|
+
string: typeof string;
|
|
106
|
+
number: typeof number;
|
|
107
|
+
boolean: typeof boolean;
|
|
108
|
+
date: typeof date;
|
|
109
|
+
function: typeof functionSchema;
|
|
110
|
+
enum: typeof enumSchema;
|
|
111
|
+
array: typeof array;
|
|
112
|
+
any: typeof any;
|
|
113
|
+
preprocess: typeof preprocess;
|
|
114
|
+
union: typeof union;
|
|
115
|
+
literal: typeof literal;
|
|
244
116
|
coerce: {
|
|
245
|
-
/**
|
|
246
|
-
* Creates a string schema that coerces input using `String(value)`.
|
|
247
|
-
* Always succeeds — `String()` never produces an invalid string.
|
|
248
|
-
*/
|
|
249
117
|
string(options?: SchemaInterfaceOptions): StringSchemaInterface;
|
|
250
|
-
/**
|
|
251
|
-
* Creates a number schema that coerces input using `Number(value)`.
|
|
252
|
-
* Fails when the result is `NaN` (e.g. `'bad'`, `undefined`, plain objects).
|
|
253
|
-
*/
|
|
254
118
|
number(options?: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
255
|
-
/**
|
|
256
|
-
* Creates a boolean schema that coerces input using `Boolean(value)`.
|
|
257
|
-
* Always succeeds — `Boolean()` always produces `true` or `false`.
|
|
258
|
-
* Note: `Boolean('false')` is `true` because `'false'` is a non-empty string.
|
|
259
|
-
*/
|
|
260
119
|
boolean(options?: SchemaInterfaceOptions): BooleanSchemaInterface;
|
|
261
|
-
/**
|
|
262
|
-
* Creates a date schema that coerces input using `new Date(value)`.
|
|
263
|
-
* Fails when the result is an invalid Date (e.g. `'garbage'`).
|
|
264
|
-
*/
|
|
265
120
|
date(options?: SchemaInterfaceOptions): DateSchemaInterface;
|
|
266
121
|
};
|
|
267
|
-
/**
|
|
268
|
-
* Smart cast schemas that apply semantic conversion before validation.
|
|
269
|
-
* Unlike `s.coerce` (which uses raw JS constructors), `s.cast` understands
|
|
270
|
-
* common programmer-friendly string representations and rejects ambiguous
|
|
271
|
-
* inputs such as `null`, `undefined`, and empty strings.
|
|
272
|
-
*
|
|
273
|
-
* Differences from `s.coerce`:
|
|
274
|
-
* - `cast.boolean('false')` → `false` (coerce gives `true` — non-empty string)
|
|
275
|
-
* - `cast.boolean('yes'/'no'/'on'/'off')` → `true`/`false` (coerce doesn't understand these)
|
|
276
|
-
* - `cast.number(null)` → throws (coerce gives `0`)
|
|
277
|
-
* - `cast.number('')` → throws (coerce gives `0`)
|
|
278
|
-
* - `cast.string(null)` → throws (coerce gives `'null'`)
|
|
279
|
-
* - `cast.date(null)` → throws (coerce gives epoch Date)
|
|
280
|
-
*
|
|
281
|
-
* @example
|
|
282
|
-
* ```typescript
|
|
283
|
-
* s.cast.boolean().parse('false'); // false — unlike coerce!
|
|
284
|
-
* s.cast.boolean().parse('yes'); // true
|
|
285
|
-
* s.cast.boolean().parse('on'); // true
|
|
286
|
-
* s.cast.number().parse(' 42 '); // 42 — trims whitespace
|
|
287
|
-
* s.cast.number().parse(null); // throws
|
|
288
|
-
* s.cast.string().parse(123); // '123'
|
|
289
|
-
* s.cast.string().parse(null); // throws — unlike coerce!
|
|
290
|
-
* s.cast.date().parse('2024-01-01'); // Date object
|
|
291
|
-
* s.cast.date().parse(null); // throws — unlike coerce!
|
|
292
|
-
* ```
|
|
293
|
-
*/
|
|
294
122
|
cast: {
|
|
295
|
-
/**
|
|
296
|
-
* Creates a boolean schema with semantic string casting.
|
|
297
|
-
* Recognises (case-insensitive): `'true'/'false'`, `'yes'/'no'`, `'on'/'off'`, `'1'/'0'`.
|
|
298
|
-
* Numbers `1` and `0` are accepted; any other number throws.
|
|
299
|
-
* `null`, `undefined`, and unrecognised strings throw.
|
|
300
|
-
*/
|
|
301
123
|
boolean(options?: SchemaInterfaceOptions): BooleanSchemaInterface;
|
|
302
|
-
/**
|
|
303
|
-
* Creates a number schema with smart string parsing.
|
|
304
|
-
* Trims whitespace from strings before converting. Accepts booleans (`true`→1, `false`→0).
|
|
305
|
-
* Rejects `null`, `undefined`, empty/whitespace-only strings, and non-numeric strings.
|
|
306
|
-
*/
|
|
307
124
|
number(options?: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
308
|
-
/**
|
|
309
|
-
* Creates a string schema that accepts strings, finite numbers, and booleans.
|
|
310
|
-
* Rejects `null`, `undefined`, objects, arrays, `NaN`, and `Infinity`.
|
|
311
|
-
*/
|
|
312
125
|
string(options?: SchemaInterfaceOptions): StringSchemaInterface;
|
|
313
|
-
/**
|
|
314
|
-
* Creates a date schema with controlled casting.
|
|
315
|
-
* Accepts ISO date strings (non-empty), finite integer timestamps, and existing Date objects.
|
|
316
|
-
* Rejects `null`, `undefined`, booleans, empty strings, and non-finite numbers.
|
|
317
|
-
*/
|
|
318
126
|
date(options?: SchemaInterfaceOptions): DateSchemaInterface;
|
|
319
|
-
/**
|
|
320
|
-
* Parses a JSON string and validates the result against the provided schema.
|
|
321
|
-
* If the input is not a string, it is passed directly to the inner schema.
|
|
322
|
-
* Produces a proper validation failure (never throws) when the JSON is malformed.
|
|
323
|
-
*
|
|
324
|
-
* @param schema - Schema to validate the parsed value against
|
|
325
|
-
* @param options - Optional configuration (name, message)
|
|
326
|
-
* @returns The same schema type, with JSON string preprocessing applied
|
|
327
|
-
*
|
|
328
|
-
* @example
|
|
329
|
-
* ```typescript
|
|
330
|
-
* const schema = s.cast.json(s.object({ name: s.string() }));
|
|
331
|
-
* schema.parse('{"name":"Alice"}'); // { name: 'Alice' }
|
|
332
|
-
* schema.parse({ name: 'Alice' }); // { name: 'Alice' } — pass-through
|
|
333
|
-
* schema.safeParse('not json'); // { success: false, error: ... }
|
|
334
|
-
* ```
|
|
335
|
-
*/
|
|
336
127
|
json<T extends SchemaType>(schema: T, options?: SchemaInterfaceOptions): T;
|
|
337
128
|
};
|
|
338
129
|
};
|
|
@@ -395,4 +186,4 @@ declare function extend(callback: ExtenderType): void;
|
|
|
395
186
|
*/
|
|
396
187
|
type Infer<T> = T extends SchemaType ? ReturnType<T['parse']> : unknown;
|
|
397
188
|
|
|
398
|
-
export { type ArraySchemaInterface, type BooleanSchemaInterface, type DateSchemaInterface, type EnumSchemaInterface, type ErrorStructure, type ExtenderType, type Infer, type Invalid, type LiteralSchemaInterface, type NumberSchemaInterface, type ObjectSchemaInterface, type SchemaInterface, type SchemaInterfaceOptions, type SchemaType, type StringSchemaInterface, type UnionSchemaInterface, type Valid, extend, hookOriginal, s, s as schema };
|
|
189
|
+
export { type ArraySchemaInterface, type BooleanSchemaInterface, type DateSchemaInterface, type EnumSchemaInterface, type ErrorStructure, type ExtenderType, type FunctionSchemaInterface, type Infer, type Invalid, type LiteralSchemaInterface, type NumberSchemaInterface, type ObjectSchemaInterface, type SchemaInterface, type SchemaInterfaceOptions, type SchemaType, type StringSchemaInterface, type UnionSchemaInterface, type Valid, any, array, boolean, cast, coerce, date, enumSchema, extend, functionSchema, hookOriginal, literal, number, object, preprocess, s, s as schema, string, union };
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export{
|
|
1
|
+
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/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, 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, 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, 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, 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",
|
|
@@ -113,7 +123,7 @@
|
|
|
113
123
|
"joi": "^17.13.3",
|
|
114
124
|
"lint-staged": "^16.1.6",
|
|
115
125
|
"superstruct": "^2.0.2",
|
|
116
|
-
"tsup": "^8.5.
|
|
126
|
+
"tsup": "^8.5.1",
|
|
117
127
|
"yup": "^1.7.0",
|
|
118
128
|
"zod": "^3.25.42"
|
|
119
129
|
}
|
package/dist/chunk-3C2I2ZOZ.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import {c}from'./chunk-QIW3OXV4.mjs';c((i,c,m)=>{if(m?.type==="number"){let n=i;n.min=function(e,{message:t}={}){return this.refine(r=>r>=e,{message:t||`Number must be greater than or equal to ${e}.`})},n.max=function(e,{message:t}={}){return this.refine(r=>r<=e,{message:t||`Number must be less than or equal to ${e}.`})},n.positive=function({message:e}={}){return this.refine(t=>t>0,{message:e||"Number must be positive."})},n.negative=function({message:e}={}){return this.refine(t=>t<0,{message:e||"Number must be negative."})},n.int=function({message:e}={}){return this.refine(t=>Number.isInteger(t),{message:e||"Number must be an integer."})},n.float=function({message:e}={}){return this.refine(t=>Number.isFinite(t)&&!Number.isInteger(t),{message:e||"Number must be a floating point (non-integer)."})},n.multipleOf=function(e,{message:t}={}){return this.refine(r=>r%e===0,{message:t||`Number must be a multiple of ${e}.`})},n.finite=function({message:e}={}){return this.refine(t=>Number.isFinite(t),{message:e||"Number must be finite."})};}return i});
|
package/dist/chunk-G6KRMWVT.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import {c}from'./chunk-QIW3OXV4.mjs';c((i,s,c)=>{if(c?.type==="string"){let r=i;r.min=function(t,{message:n}={}){return this.refine(e=>e.length>=t,{message:n||(e=>`String must be at least ${t} characters long (received ${e.length} characters: "${e}")`)})},r.max=function(t,{message:n}={}){return this.refine(e=>e.length<=t,{message:n||(e=>`String must be at most ${t} characters long (received ${e.length} characters: "${e}")`)})},r.length=function(t,{message:n}={}){return this.refine(e=>e.length===t,{message:n||(e=>`String must be exactly ${t} characters long (received ${e.length} characters: "${e}")`)})},r.nonEmpty=function({message:t}={}){return this.refine(n=>n.length>0,{message:t||"String must not be empty (received empty string)"})},r.startsWith=function(t,{message:n}={}){return this.refine(e=>e.startsWith(t),{message:n||(e=>`String must start with "${t}" (received: "${e}")`)})},r.endsWith=function(t,{message:n}={}){return this.refine(e=>e.endsWith(t),{message:n||(e=>`String must end with "${t}" (received: "${e}")`)})},r.includes=function(t,{message:n}={}){return this.refine(e=>e.includes(t),{message:n||(e=>`String must include "${t}" (received: "${e}")`)})},r.toLowerCase=function(){return this.transform(t=>t.toLowerCase())},r.toUpperCase=function(){return this.transform(t=>t.toUpperCase())},r.trim=function(){return this.transform(t=>t.trim())},r.padStart=function(t,n=" "){return this.transform(e=>e.padStart(t,n))},r.padEnd=function(t,n=" "){return this.transform(e=>e.padEnd(t,n))},r.replace=function(t,n){return this.transform(e=>e.replace(t,n))};}return i});
|
package/dist/chunk-GJNOESJT.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import {c}from'./chunk-QIW3OXV4.mjs';c((a,o,m)=>{if(m?.type==="array"){let r=a;r.min=function(e,{message:t}={}){return this.refine(n=>n.length>=e,{message:t||`Array must contain at least ${e} items.`})},r.max=function(e,{message:t}={}){return this.refine(n=>n.length<=e,{message:t||`Array must contain at most ${e} items.`})},r.length=function(e,{message:t}={}){return this.refine(n=>n.length===e,{message:t||`Array must contain exactly ${e} items.`})},r.nonEmpty=function({message:e}={}){return this.refine(t=>t.length>0,{message:e||"Array must not be empty."})},r.unique=function({message:e}={}){return this.refine(t=>{let n=new Set;try{return t.every(c=>{let s=JSON.stringify(c);return n.has(s)?!1:(n.add(s),!0)})}catch{return new Set(t).size===t.length}},{message:e||"Array items must be unique."})},r.sort=function(){return this.transform(e=>[...e].sort())},r.reverse=function(){return this.transform(e=>[...e].reverse())};}return a});
|
package/dist/chunk-QIW3OXV4.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 d=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=>Array.isArray(e),x=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),m={object(e,t){let n=l(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 I=b(f.error,i);return {success:false,error:I,errors:[I]}}w(f,c,i);}}return c.length>0?{success:false,error:c[0],errors:c}:{success:true,data:u}}),n},string(e){return l(d,{...e,type:"string"})},number(e){return l(y,{...e,type:"number"})},boolean(e){return l(g,{...e,type:"boolean"})},date(e){return l(D,{...e,type:"date"})},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=l(n,{message:a,...t,type:p});return o._getDescription=()=>`enum(${e.map(r=>`"${r}"`).join(" | ")})`,o},array(e,t){let n=l(_,{...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 I=b(f.error,i);return {success:false,error:I,errors:[I]}}w(f,c,i);}}return c.length>0?{success:false,error:c[0],errors:c}:{success:true,data:u}}),n},any(){return l(()=>true)},preprocess(e,t){return h(t,"_parse",(n,a)=>(a=e(a),n(a))),t},union(e,t){return l(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=l(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 d(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(d(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 d(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(d(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 l(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 I=c(i,f);T(f);if(!I.success)return I;let S=r(I.data);if(S===true||typeof S=="object"&&S?.success===true)return I;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};
|