@esmj/schema 0.7.3 → 0.8.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 +127 -21
- package/dist/array.cjs +1 -1
- package/dist/array.d.mts +1 -1
- package/dist/array.d.ts +1 -1
- package/dist/array.mjs +1 -1
- package/dist/{chunk-YUL3WJPU.mjs → chunk-7VTVE334.mjs} +1 -1
- package/dist/chunk-AKXLQP64.mjs +1 -0
- package/dist/{chunk-YYPP27RX.mjs → chunk-AQFELD23.mjs} +1 -1
- package/dist/{chunk-CLEFU3BA.mjs → chunk-HVQY2UKM.mjs} +1 -1
- package/dist/chunk-SR7R5QYC.mjs +1 -0
- package/dist/coerce.cjs +1 -0
- package/dist/coerce.d.mts +27 -0
- package/dist/coerce.d.ts +27 -0
- package/dist/coerce.mjs +1 -0
- package/dist/full.cjs +1 -1
- package/dist/full.d.mts +2 -1
- package/dist/full.d.ts +2 -1
- package/dist/full.mjs +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.mts +36 -270
- package/dist/index.d.ts +36 -270
- 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 +23 -5
- package/dist/chunk-QQOSS5DZ.mjs +0 -1
package/dist/index.d.mts
CHANGED
|
@@ -55,6 +55,8 @@ interface FunctionSchemaInterface extends SchemaInterface<Function, Function> {
|
|
|
55
55
|
}
|
|
56
56
|
interface ArraySchemaInterface<T extends SchemaType> extends SchemaInterface<Array<ReturnType<T['parse']>>, Array<ReturnType<T['parse']>>> {
|
|
57
57
|
}
|
|
58
|
+
interface CoerceInterface {
|
|
59
|
+
}
|
|
58
60
|
interface ObjectSchemaInterface<T extends Record<string, SchemaType>> extends SchemaInterface<{
|
|
59
61
|
[Property in keyof T]: ReturnType<T[Property]['parse']>;
|
|
60
62
|
}, {
|
|
@@ -73,282 +75,46 @@ interface CreateSchemaInterfaceOptions {
|
|
|
73
75
|
message?: ErrorMessage;
|
|
74
76
|
}
|
|
75
77
|
type SchemaInterfaceOptions = Omit<CreateSchemaInterfaceOptions, 'type'>;
|
|
76
|
-
declare
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
*/
|
|
92
|
-
object<T extends Record<string, SchemaType>>(definition: { [Property in keyof T]: T[Property]; }, options?: SchemaInterfaceOptions): ObjectSchemaInterface<T>;
|
|
93
|
-
/**
|
|
94
|
-
* Creates a string schema.
|
|
95
|
-
*
|
|
96
|
-
* @param options - Optional configuration (name, message)
|
|
97
|
-
* @returns String schema interface
|
|
98
|
-
*
|
|
99
|
-
* @example
|
|
100
|
-
* ```typescript
|
|
101
|
-
* const nameSchema = s.string();
|
|
102
|
-
* const result = nameSchema.parse('John'); // 'John'
|
|
103
|
-
* ```
|
|
104
|
-
*/
|
|
105
|
-
string(options?: SchemaInterfaceOptions): StringSchemaInterface;
|
|
106
|
-
/**
|
|
107
|
-
* Creates a number schema.
|
|
108
|
-
*
|
|
109
|
-
* @param options - Optional configuration (name, message)
|
|
110
|
-
* @returns Number schema interface
|
|
111
|
-
*
|
|
112
|
-
* @example
|
|
113
|
-
* ```typescript
|
|
114
|
-
* const ageSchema = s.number();
|
|
115
|
-
* const result = ageSchema.parse(25); // 25
|
|
116
|
-
* ```
|
|
117
|
-
*/
|
|
118
|
-
number(options?: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
119
|
-
/**
|
|
120
|
-
* Creates a boolean schema.
|
|
121
|
-
*
|
|
122
|
-
* @param options - Optional configuration (name, message)
|
|
123
|
-
* @returns Boolean schema interface
|
|
124
|
-
*
|
|
125
|
-
* @example
|
|
126
|
-
* ```typescript
|
|
127
|
-
* const isActiveSchema = s.boolean();
|
|
128
|
-
* const result = isActiveSchema.parse(true); // true
|
|
129
|
-
* ```
|
|
130
|
-
*/
|
|
78
|
+
declare function object<T extends Record<string, SchemaType>>(definition: {
|
|
79
|
+
[Property in keyof T]: T[Property];
|
|
80
|
+
}, options?: SchemaInterfaceOptions): ObjectSchemaInterface<T>;
|
|
81
|
+
declare function string(options?: SchemaInterfaceOptions): StringSchemaInterface;
|
|
82
|
+
declare function number(options?: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
83
|
+
declare function boolean(options?: SchemaInterfaceOptions): BooleanSchemaInterface;
|
|
84
|
+
declare function date(options?: SchemaInterfaceOptions): DateSchemaInterface;
|
|
85
|
+
declare function functionSchema(options?: SchemaInterfaceOptions): FunctionSchemaInterface;
|
|
86
|
+
declare function enumSchema(definition: Readonly<Array<string>>, options?: SchemaInterfaceOptions): EnumSchemaInterface<(typeof definition)[number]>;
|
|
87
|
+
declare function array<T extends SchemaType>(definition: T, options?: SchemaInterfaceOptions): ArraySchemaInterface<T>;
|
|
88
|
+
declare function any(): SchemaInterface<unknown, unknown>;
|
|
89
|
+
declare function preprocess<T extends SchemaType>(callback: Function, schema: T): T;
|
|
90
|
+
declare function union<T extends Array<SchemaType>>(definitions: T, options?: SchemaInterfaceOptions): UnionSchemaInterface<T>;
|
|
91
|
+
declare function literal<T extends string | number | boolean>(value: T, options?: SchemaInterfaceOptions): LiteralSchemaInterface<T>;
|
|
92
|
+
declare const cast: {
|
|
131
93
|
boolean(options?: SchemaInterfaceOptions): BooleanSchemaInterface;
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
*
|
|
135
|
-
* @param options - Optional configuration (name, message)
|
|
136
|
-
* @returns Date schema interface
|
|
137
|
-
*
|
|
138
|
-
* @example
|
|
139
|
-
* ```typescript
|
|
140
|
-
* const birthdateSchema = s.date();
|
|
141
|
-
* const result = birthdateSchema.parse(new Date()); // Date object
|
|
142
|
-
* ```
|
|
143
|
-
*/
|
|
94
|
+
number(options?: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
95
|
+
string(options?: SchemaInterfaceOptions): StringSchemaInterface;
|
|
144
96
|
date(options?: SchemaInterfaceOptions): DateSchemaInterface;
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
*
|
|
162
|
-
* @param definition - Array of allowed string values
|
|
163
|
-
* @param options - Optional configuration (name, message)
|
|
164
|
-
* @returns Enum schema interface
|
|
165
|
-
*
|
|
166
|
-
* @example
|
|
167
|
-
* ```typescript
|
|
168
|
-
* const roleSchema = s.enum(['admin', 'user', 'guest']);
|
|
169
|
-
* const result = roleSchema.parse('admin'); // 'admin'
|
|
170
|
-
* ```
|
|
171
|
-
*/
|
|
172
|
-
enum(definition: Readonly<Array<string>>, options?: SchemaInterfaceOptions): EnumSchemaInterface<(typeof definition)[number]>;
|
|
173
|
-
/**
|
|
174
|
-
* Creates an array schema with element validation.
|
|
175
|
-
*
|
|
176
|
-
* @param definition - Schema for array elements
|
|
177
|
-
* @param options - Optional configuration (name, message)
|
|
178
|
-
* @returns Array schema interface
|
|
179
|
-
*
|
|
180
|
-
* @example
|
|
181
|
-
* ```typescript
|
|
182
|
-
* const tagsSchema = s.array(s.string());
|
|
183
|
-
* const result = tagsSchema.parse(['tag1', 'tag2']); // ['tag1', 'tag2']
|
|
184
|
-
* ```
|
|
185
|
-
*/
|
|
186
|
-
array<T extends SchemaType>(definition: T, options?: SchemaInterfaceOptions): ArraySchemaInterface<T>;
|
|
187
|
-
/**
|
|
188
|
-
* Creates a schema that accepts any value without validation.
|
|
189
|
-
*
|
|
190
|
-
* @returns Schema interface that accepts any value
|
|
191
|
-
*
|
|
192
|
-
* @example
|
|
193
|
-
* ```typescript
|
|
194
|
-
* const anySchema = s.any();
|
|
195
|
-
* const result = anySchema.parse({ anything: true }); // { anything: true }
|
|
196
|
-
* ```
|
|
197
|
-
*/
|
|
198
|
-
any(): SchemaInterface<unknown, unknown>;
|
|
199
|
-
/**
|
|
200
|
-
* Preprocesses a value before passing it to a schema for validation.
|
|
201
|
-
*
|
|
202
|
-
* @param callback - Function to transform the value before validation
|
|
203
|
-
* @param schema - Schema to validate the transformed value
|
|
204
|
-
* @returns Modified schema with preprocessing
|
|
205
|
-
*
|
|
206
|
-
* @example
|
|
207
|
-
* ```typescript
|
|
208
|
-
* const schema = s.preprocess(
|
|
209
|
-
* (val) => String(val).trim(),
|
|
210
|
-
* s.string().min(3)
|
|
211
|
-
* );
|
|
212
|
-
* const result = schema.parse(' hello '); // 'hello'
|
|
213
|
-
* ```
|
|
214
|
-
*/
|
|
215
|
-
preprocess<T extends SchemaType>(callback: Function, schema: T): T;
|
|
216
|
-
/**
|
|
217
|
-
* Creates a union schema that validates against multiple schemas.
|
|
218
|
-
* The value must match at least one of the provided schemas.
|
|
219
|
-
*
|
|
220
|
-
* @param definitions - Array of schemas to validate against
|
|
221
|
-
* @param options - Optional configuration (name, message)
|
|
222
|
-
* @returns Union schema interface
|
|
223
|
-
*
|
|
224
|
-
* @example
|
|
225
|
-
* ```typescript
|
|
226
|
-
* const idSchema = s.union([s.string(), s.number()]);
|
|
227
|
-
* const result1 = idSchema.parse('abc'); // 'abc'
|
|
228
|
-
* const result2 = idSchema.parse(123); // 123
|
|
229
|
-
* ```
|
|
230
|
-
*/
|
|
231
|
-
union<T extends Array<SchemaType>>(definitions: T, options?: SchemaInterfaceOptions): UnionSchemaInterface<T>;
|
|
232
|
-
/**
|
|
233
|
-
* Creates a literal schema that only accepts a specific value.
|
|
234
|
-
*
|
|
235
|
-
* @param value - The exact value to match
|
|
236
|
-
* @param options - Optional configuration
|
|
237
|
-
* @returns Literal schema interface
|
|
238
|
-
*
|
|
239
|
-
* @example
|
|
240
|
-
* ```typescript
|
|
241
|
-
* const adminSchema = s.literal('admin');
|
|
242
|
-
* adminSchema.parse('admin'); // 'admin'
|
|
243
|
-
* adminSchema.parse('user'); // throws error
|
|
244
|
-
* ```
|
|
245
|
-
*/
|
|
246
|
-
literal<T extends string | number | boolean>(value: T, options?: SchemaInterfaceOptions): LiteralSchemaInterface<T>;
|
|
247
|
-
/**
|
|
248
|
-
* Coerce schemas that apply a native JS constructor before validation.
|
|
249
|
-
* Unlike `s.preprocess`, coerce provides a consistent API for common type
|
|
250
|
-
* conversions with clear error messages when coercion produces an invalid result.
|
|
251
|
-
*
|
|
252
|
-
* @example
|
|
253
|
-
* ```typescript
|
|
254
|
-
* s.coerce.number().parse('42'); // 42
|
|
255
|
-
* s.coerce.string().parse(123); // '123'
|
|
256
|
-
* s.coerce.boolean().parse(0); // false
|
|
257
|
-
* s.coerce.date().parse('2024-01-01'); // Date object
|
|
258
|
-
* ```
|
|
259
|
-
*/
|
|
260
|
-
coerce: {
|
|
261
|
-
/**
|
|
262
|
-
* Creates a string schema that coerces input using `String(value)`.
|
|
263
|
-
* Always succeeds — `String()` never produces an invalid string.
|
|
264
|
-
*/
|
|
265
|
-
string(options?: SchemaInterfaceOptions): StringSchemaInterface;
|
|
266
|
-
/**
|
|
267
|
-
* Creates a number schema that coerces input using `Number(value)`.
|
|
268
|
-
* Fails when the result is `NaN` (e.g. `'bad'`, `undefined`, plain objects).
|
|
269
|
-
*/
|
|
270
|
-
number(options?: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
271
|
-
/**
|
|
272
|
-
* Creates a boolean schema that coerces input using `Boolean(value)`.
|
|
273
|
-
* Always succeeds — `Boolean()` always produces `true` or `false`.
|
|
274
|
-
* Note: `Boolean('false')` is `true` because `'false'` is a non-empty string.
|
|
275
|
-
*/
|
|
276
|
-
boolean(options?: SchemaInterfaceOptions): BooleanSchemaInterface;
|
|
277
|
-
/**
|
|
278
|
-
* Creates a date schema that coerces input using `new Date(value)`.
|
|
279
|
-
* Fails when the result is an invalid Date (e.g. `'garbage'`).
|
|
280
|
-
*/
|
|
281
|
-
date(options?: SchemaInterfaceOptions): DateSchemaInterface;
|
|
282
|
-
};
|
|
283
|
-
/**
|
|
284
|
-
* Smart cast schemas that apply semantic conversion before validation.
|
|
285
|
-
* Unlike `s.coerce` (which uses raw JS constructors), `s.cast` understands
|
|
286
|
-
* common programmer-friendly string representations and rejects ambiguous
|
|
287
|
-
* inputs such as `null`, `undefined`, and empty strings.
|
|
288
|
-
*
|
|
289
|
-
* Differences from `s.coerce`:
|
|
290
|
-
* - `cast.boolean('false')` → `false` (coerce gives `true` — non-empty string)
|
|
291
|
-
* - `cast.boolean('yes'/'no'/'on'/'off')` → `true`/`false` (coerce doesn't understand these)
|
|
292
|
-
* - `cast.number(null)` → throws (coerce gives `0`)
|
|
293
|
-
* - `cast.number('')` → throws (coerce gives `0`)
|
|
294
|
-
* - `cast.string(null)` → throws (coerce gives `'null'`)
|
|
295
|
-
* - `cast.date(null)` → throws (coerce gives epoch Date)
|
|
296
|
-
*
|
|
297
|
-
* @example
|
|
298
|
-
* ```typescript
|
|
299
|
-
* s.cast.boolean().parse('false'); // false — unlike coerce!
|
|
300
|
-
* s.cast.boolean().parse('yes'); // true
|
|
301
|
-
* s.cast.boolean().parse('on'); // true
|
|
302
|
-
* s.cast.number().parse(' 42 '); // 42 — trims whitespace
|
|
303
|
-
* s.cast.number().parse(null); // throws
|
|
304
|
-
* s.cast.string().parse(123); // '123'
|
|
305
|
-
* s.cast.string().parse(null); // throws — unlike coerce!
|
|
306
|
-
* s.cast.date().parse('2024-01-01'); // Date object
|
|
307
|
-
* s.cast.date().parse(null); // throws — unlike coerce!
|
|
308
|
-
* ```
|
|
309
|
-
*/
|
|
97
|
+
json<T extends SchemaType>(schema: T, options?: SchemaInterfaceOptions): T;
|
|
98
|
+
};
|
|
99
|
+
declare const s: {
|
|
100
|
+
object: typeof object;
|
|
101
|
+
string: typeof string;
|
|
102
|
+
number: typeof number;
|
|
103
|
+
boolean: typeof boolean;
|
|
104
|
+
date: typeof date;
|
|
105
|
+
function: typeof functionSchema;
|
|
106
|
+
enum: typeof enumSchema;
|
|
107
|
+
array: typeof array;
|
|
108
|
+
any: typeof any;
|
|
109
|
+
preprocess: typeof preprocess;
|
|
110
|
+
union: typeof union;
|
|
111
|
+
literal: typeof literal;
|
|
112
|
+
coerce: CoerceInterface;
|
|
310
113
|
cast: {
|
|
311
|
-
/**
|
|
312
|
-
* Creates a boolean schema with semantic string casting.
|
|
313
|
-
* Recognises (case-insensitive): `'true'/'false'`, `'yes'/'no'`, `'on'/'off'`, `'1'/'0'`.
|
|
314
|
-
* Numbers `1` and `0` are accepted; any other number throws.
|
|
315
|
-
* `null`, `undefined`, and unrecognised strings throw.
|
|
316
|
-
*/
|
|
317
114
|
boolean(options?: SchemaInterfaceOptions): BooleanSchemaInterface;
|
|
318
|
-
/**
|
|
319
|
-
* Creates a number schema with smart string parsing.
|
|
320
|
-
* Trims whitespace from strings before converting. Accepts booleans (`true`→1, `false`→0).
|
|
321
|
-
* Rejects `null`, `undefined`, empty/whitespace-only strings, and non-numeric strings.
|
|
322
|
-
*/
|
|
323
115
|
number(options?: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
324
|
-
/**
|
|
325
|
-
* Creates a string schema that accepts strings, finite numbers, and booleans.
|
|
326
|
-
* Rejects `null`, `undefined`, objects, arrays, `NaN`, and `Infinity`.
|
|
327
|
-
*/
|
|
328
116
|
string(options?: SchemaInterfaceOptions): StringSchemaInterface;
|
|
329
|
-
/**
|
|
330
|
-
* Creates a date schema with controlled casting.
|
|
331
|
-
* Accepts ISO date strings (non-empty), finite integer timestamps, and existing Date objects.
|
|
332
|
-
* Rejects `null`, `undefined`, booleans, empty strings, and non-finite numbers.
|
|
333
|
-
*/
|
|
334
117
|
date(options?: SchemaInterfaceOptions): DateSchemaInterface;
|
|
335
|
-
/**
|
|
336
|
-
* Parses a JSON string and validates the result against the provided schema.
|
|
337
|
-
* If the input is not a string, it is passed directly to the inner schema.
|
|
338
|
-
* Produces a proper validation failure (never throws) when the JSON is malformed.
|
|
339
|
-
*
|
|
340
|
-
* @param schema - Schema to validate the parsed value against
|
|
341
|
-
* @param options - Optional configuration (name, message)
|
|
342
|
-
* @returns The same schema type, with JSON string preprocessing applied
|
|
343
|
-
*
|
|
344
|
-
* @example
|
|
345
|
-
* ```typescript
|
|
346
|
-
* const schema = s.cast.json(s.object({ name: s.string() }));
|
|
347
|
-
* schema.parse('{"name":"Alice"}'); // { name: 'Alice' }
|
|
348
|
-
* schema.parse({ name: 'Alice' }); // { name: 'Alice' } — pass-through
|
|
349
|
-
* schema.safeParse('not json'); // { success: false, error: ... }
|
|
350
|
-
* ```
|
|
351
|
-
*/
|
|
352
118
|
json<T extends SchemaType>(schema: T, options?: SchemaInterfaceOptions): T;
|
|
353
119
|
};
|
|
354
120
|
};
|
|
@@ -411,4 +177,4 @@ declare function extend(callback: ExtenderType): void;
|
|
|
411
177
|
*/
|
|
412
178
|
type Infer<T> = T extends SchemaType ? ReturnType<T['parse']> : unknown;
|
|
413
179
|
|
|
414
|
-
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, extend, hookOriginal, s, s as schema };
|
|
180
|
+
export { type ArraySchemaInterface, type BooleanSchemaInterface, type CoerceInterface, 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, date, enumSchema, extend, functionSchema, hookOriginal, literal, number, object, preprocess, s, s as schema, string, union };
|
package/dist/index.d.ts
CHANGED
|
@@ -55,6 +55,8 @@ interface FunctionSchemaInterface extends SchemaInterface<Function, Function> {
|
|
|
55
55
|
}
|
|
56
56
|
interface ArraySchemaInterface<T extends SchemaType> extends SchemaInterface<Array<ReturnType<T['parse']>>, Array<ReturnType<T['parse']>>> {
|
|
57
57
|
}
|
|
58
|
+
interface CoerceInterface {
|
|
59
|
+
}
|
|
58
60
|
interface ObjectSchemaInterface<T extends Record<string, SchemaType>> extends SchemaInterface<{
|
|
59
61
|
[Property in keyof T]: ReturnType<T[Property]['parse']>;
|
|
60
62
|
}, {
|
|
@@ -73,282 +75,46 @@ interface CreateSchemaInterfaceOptions {
|
|
|
73
75
|
message?: ErrorMessage;
|
|
74
76
|
}
|
|
75
77
|
type SchemaInterfaceOptions = Omit<CreateSchemaInterfaceOptions, 'type'>;
|
|
76
|
-
declare
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
*/
|
|
92
|
-
object<T extends Record<string, SchemaType>>(definition: { [Property in keyof T]: T[Property]; }, options?: SchemaInterfaceOptions): ObjectSchemaInterface<T>;
|
|
93
|
-
/**
|
|
94
|
-
* Creates a string schema.
|
|
95
|
-
*
|
|
96
|
-
* @param options - Optional configuration (name, message)
|
|
97
|
-
* @returns String schema interface
|
|
98
|
-
*
|
|
99
|
-
* @example
|
|
100
|
-
* ```typescript
|
|
101
|
-
* const nameSchema = s.string();
|
|
102
|
-
* const result = nameSchema.parse('John'); // 'John'
|
|
103
|
-
* ```
|
|
104
|
-
*/
|
|
105
|
-
string(options?: SchemaInterfaceOptions): StringSchemaInterface;
|
|
106
|
-
/**
|
|
107
|
-
* Creates a number schema.
|
|
108
|
-
*
|
|
109
|
-
* @param options - Optional configuration (name, message)
|
|
110
|
-
* @returns Number schema interface
|
|
111
|
-
*
|
|
112
|
-
* @example
|
|
113
|
-
* ```typescript
|
|
114
|
-
* const ageSchema = s.number();
|
|
115
|
-
* const result = ageSchema.parse(25); // 25
|
|
116
|
-
* ```
|
|
117
|
-
*/
|
|
118
|
-
number(options?: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
119
|
-
/**
|
|
120
|
-
* Creates a boolean schema.
|
|
121
|
-
*
|
|
122
|
-
* @param options - Optional configuration (name, message)
|
|
123
|
-
* @returns Boolean schema interface
|
|
124
|
-
*
|
|
125
|
-
* @example
|
|
126
|
-
* ```typescript
|
|
127
|
-
* const isActiveSchema = s.boolean();
|
|
128
|
-
* const result = isActiveSchema.parse(true); // true
|
|
129
|
-
* ```
|
|
130
|
-
*/
|
|
78
|
+
declare function object<T extends Record<string, SchemaType>>(definition: {
|
|
79
|
+
[Property in keyof T]: T[Property];
|
|
80
|
+
}, options?: SchemaInterfaceOptions): ObjectSchemaInterface<T>;
|
|
81
|
+
declare function string(options?: SchemaInterfaceOptions): StringSchemaInterface;
|
|
82
|
+
declare function number(options?: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
83
|
+
declare function boolean(options?: SchemaInterfaceOptions): BooleanSchemaInterface;
|
|
84
|
+
declare function date(options?: SchemaInterfaceOptions): DateSchemaInterface;
|
|
85
|
+
declare function functionSchema(options?: SchemaInterfaceOptions): FunctionSchemaInterface;
|
|
86
|
+
declare function enumSchema(definition: Readonly<Array<string>>, options?: SchemaInterfaceOptions): EnumSchemaInterface<(typeof definition)[number]>;
|
|
87
|
+
declare function array<T extends SchemaType>(definition: T, options?: SchemaInterfaceOptions): ArraySchemaInterface<T>;
|
|
88
|
+
declare function any(): SchemaInterface<unknown, unknown>;
|
|
89
|
+
declare function preprocess<T extends SchemaType>(callback: Function, schema: T): T;
|
|
90
|
+
declare function union<T extends Array<SchemaType>>(definitions: T, options?: SchemaInterfaceOptions): UnionSchemaInterface<T>;
|
|
91
|
+
declare function literal<T extends string | number | boolean>(value: T, options?: SchemaInterfaceOptions): LiteralSchemaInterface<T>;
|
|
92
|
+
declare const cast: {
|
|
131
93
|
boolean(options?: SchemaInterfaceOptions): BooleanSchemaInterface;
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
*
|
|
135
|
-
* @param options - Optional configuration (name, message)
|
|
136
|
-
* @returns Date schema interface
|
|
137
|
-
*
|
|
138
|
-
* @example
|
|
139
|
-
* ```typescript
|
|
140
|
-
* const birthdateSchema = s.date();
|
|
141
|
-
* const result = birthdateSchema.parse(new Date()); // Date object
|
|
142
|
-
* ```
|
|
143
|
-
*/
|
|
94
|
+
number(options?: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
95
|
+
string(options?: SchemaInterfaceOptions): StringSchemaInterface;
|
|
144
96
|
date(options?: SchemaInterfaceOptions): DateSchemaInterface;
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
*
|
|
162
|
-
* @param definition - Array of allowed string values
|
|
163
|
-
* @param options - Optional configuration (name, message)
|
|
164
|
-
* @returns Enum schema interface
|
|
165
|
-
*
|
|
166
|
-
* @example
|
|
167
|
-
* ```typescript
|
|
168
|
-
* const roleSchema = s.enum(['admin', 'user', 'guest']);
|
|
169
|
-
* const result = roleSchema.parse('admin'); // 'admin'
|
|
170
|
-
* ```
|
|
171
|
-
*/
|
|
172
|
-
enum(definition: Readonly<Array<string>>, options?: SchemaInterfaceOptions): EnumSchemaInterface<(typeof definition)[number]>;
|
|
173
|
-
/**
|
|
174
|
-
* Creates an array schema with element validation.
|
|
175
|
-
*
|
|
176
|
-
* @param definition - Schema for array elements
|
|
177
|
-
* @param options - Optional configuration (name, message)
|
|
178
|
-
* @returns Array schema interface
|
|
179
|
-
*
|
|
180
|
-
* @example
|
|
181
|
-
* ```typescript
|
|
182
|
-
* const tagsSchema = s.array(s.string());
|
|
183
|
-
* const result = tagsSchema.parse(['tag1', 'tag2']); // ['tag1', 'tag2']
|
|
184
|
-
* ```
|
|
185
|
-
*/
|
|
186
|
-
array<T extends SchemaType>(definition: T, options?: SchemaInterfaceOptions): ArraySchemaInterface<T>;
|
|
187
|
-
/**
|
|
188
|
-
* Creates a schema that accepts any value without validation.
|
|
189
|
-
*
|
|
190
|
-
* @returns Schema interface that accepts any value
|
|
191
|
-
*
|
|
192
|
-
* @example
|
|
193
|
-
* ```typescript
|
|
194
|
-
* const anySchema = s.any();
|
|
195
|
-
* const result = anySchema.parse({ anything: true }); // { anything: true }
|
|
196
|
-
* ```
|
|
197
|
-
*/
|
|
198
|
-
any(): SchemaInterface<unknown, unknown>;
|
|
199
|
-
/**
|
|
200
|
-
* Preprocesses a value before passing it to a schema for validation.
|
|
201
|
-
*
|
|
202
|
-
* @param callback - Function to transform the value before validation
|
|
203
|
-
* @param schema - Schema to validate the transformed value
|
|
204
|
-
* @returns Modified schema with preprocessing
|
|
205
|
-
*
|
|
206
|
-
* @example
|
|
207
|
-
* ```typescript
|
|
208
|
-
* const schema = s.preprocess(
|
|
209
|
-
* (val) => String(val).trim(),
|
|
210
|
-
* s.string().min(3)
|
|
211
|
-
* );
|
|
212
|
-
* const result = schema.parse(' hello '); // 'hello'
|
|
213
|
-
* ```
|
|
214
|
-
*/
|
|
215
|
-
preprocess<T extends SchemaType>(callback: Function, schema: T): T;
|
|
216
|
-
/**
|
|
217
|
-
* Creates a union schema that validates against multiple schemas.
|
|
218
|
-
* The value must match at least one of the provided schemas.
|
|
219
|
-
*
|
|
220
|
-
* @param definitions - Array of schemas to validate against
|
|
221
|
-
* @param options - Optional configuration (name, message)
|
|
222
|
-
* @returns Union schema interface
|
|
223
|
-
*
|
|
224
|
-
* @example
|
|
225
|
-
* ```typescript
|
|
226
|
-
* const idSchema = s.union([s.string(), s.number()]);
|
|
227
|
-
* const result1 = idSchema.parse('abc'); // 'abc'
|
|
228
|
-
* const result2 = idSchema.parse(123); // 123
|
|
229
|
-
* ```
|
|
230
|
-
*/
|
|
231
|
-
union<T extends Array<SchemaType>>(definitions: T, options?: SchemaInterfaceOptions): UnionSchemaInterface<T>;
|
|
232
|
-
/**
|
|
233
|
-
* Creates a literal schema that only accepts a specific value.
|
|
234
|
-
*
|
|
235
|
-
* @param value - The exact value to match
|
|
236
|
-
* @param options - Optional configuration
|
|
237
|
-
* @returns Literal schema interface
|
|
238
|
-
*
|
|
239
|
-
* @example
|
|
240
|
-
* ```typescript
|
|
241
|
-
* const adminSchema = s.literal('admin');
|
|
242
|
-
* adminSchema.parse('admin'); // 'admin'
|
|
243
|
-
* adminSchema.parse('user'); // throws error
|
|
244
|
-
* ```
|
|
245
|
-
*/
|
|
246
|
-
literal<T extends string | number | boolean>(value: T, options?: SchemaInterfaceOptions): LiteralSchemaInterface<T>;
|
|
247
|
-
/**
|
|
248
|
-
* Coerce schemas that apply a native JS constructor before validation.
|
|
249
|
-
* Unlike `s.preprocess`, coerce provides a consistent API for common type
|
|
250
|
-
* conversions with clear error messages when coercion produces an invalid result.
|
|
251
|
-
*
|
|
252
|
-
* @example
|
|
253
|
-
* ```typescript
|
|
254
|
-
* s.coerce.number().parse('42'); // 42
|
|
255
|
-
* s.coerce.string().parse(123); // '123'
|
|
256
|
-
* s.coerce.boolean().parse(0); // false
|
|
257
|
-
* s.coerce.date().parse('2024-01-01'); // Date object
|
|
258
|
-
* ```
|
|
259
|
-
*/
|
|
260
|
-
coerce: {
|
|
261
|
-
/**
|
|
262
|
-
* Creates a string schema that coerces input using `String(value)`.
|
|
263
|
-
* Always succeeds — `String()` never produces an invalid string.
|
|
264
|
-
*/
|
|
265
|
-
string(options?: SchemaInterfaceOptions): StringSchemaInterface;
|
|
266
|
-
/**
|
|
267
|
-
* Creates a number schema that coerces input using `Number(value)`.
|
|
268
|
-
* Fails when the result is `NaN` (e.g. `'bad'`, `undefined`, plain objects).
|
|
269
|
-
*/
|
|
270
|
-
number(options?: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
271
|
-
/**
|
|
272
|
-
* Creates a boolean schema that coerces input using `Boolean(value)`.
|
|
273
|
-
* Always succeeds — `Boolean()` always produces `true` or `false`.
|
|
274
|
-
* Note: `Boolean('false')` is `true` because `'false'` is a non-empty string.
|
|
275
|
-
*/
|
|
276
|
-
boolean(options?: SchemaInterfaceOptions): BooleanSchemaInterface;
|
|
277
|
-
/**
|
|
278
|
-
* Creates a date schema that coerces input using `new Date(value)`.
|
|
279
|
-
* Fails when the result is an invalid Date (e.g. `'garbage'`).
|
|
280
|
-
*/
|
|
281
|
-
date(options?: SchemaInterfaceOptions): DateSchemaInterface;
|
|
282
|
-
};
|
|
283
|
-
/**
|
|
284
|
-
* Smart cast schemas that apply semantic conversion before validation.
|
|
285
|
-
* Unlike `s.coerce` (which uses raw JS constructors), `s.cast` understands
|
|
286
|
-
* common programmer-friendly string representations and rejects ambiguous
|
|
287
|
-
* inputs such as `null`, `undefined`, and empty strings.
|
|
288
|
-
*
|
|
289
|
-
* Differences from `s.coerce`:
|
|
290
|
-
* - `cast.boolean('false')` → `false` (coerce gives `true` — non-empty string)
|
|
291
|
-
* - `cast.boolean('yes'/'no'/'on'/'off')` → `true`/`false` (coerce doesn't understand these)
|
|
292
|
-
* - `cast.number(null)` → throws (coerce gives `0`)
|
|
293
|
-
* - `cast.number('')` → throws (coerce gives `0`)
|
|
294
|
-
* - `cast.string(null)` → throws (coerce gives `'null'`)
|
|
295
|
-
* - `cast.date(null)` → throws (coerce gives epoch Date)
|
|
296
|
-
*
|
|
297
|
-
* @example
|
|
298
|
-
* ```typescript
|
|
299
|
-
* s.cast.boolean().parse('false'); // false — unlike coerce!
|
|
300
|
-
* s.cast.boolean().parse('yes'); // true
|
|
301
|
-
* s.cast.boolean().parse('on'); // true
|
|
302
|
-
* s.cast.number().parse(' 42 '); // 42 — trims whitespace
|
|
303
|
-
* s.cast.number().parse(null); // throws
|
|
304
|
-
* s.cast.string().parse(123); // '123'
|
|
305
|
-
* s.cast.string().parse(null); // throws — unlike coerce!
|
|
306
|
-
* s.cast.date().parse('2024-01-01'); // Date object
|
|
307
|
-
* s.cast.date().parse(null); // throws — unlike coerce!
|
|
308
|
-
* ```
|
|
309
|
-
*/
|
|
97
|
+
json<T extends SchemaType>(schema: T, options?: SchemaInterfaceOptions): T;
|
|
98
|
+
};
|
|
99
|
+
declare const s: {
|
|
100
|
+
object: typeof object;
|
|
101
|
+
string: typeof string;
|
|
102
|
+
number: typeof number;
|
|
103
|
+
boolean: typeof boolean;
|
|
104
|
+
date: typeof date;
|
|
105
|
+
function: typeof functionSchema;
|
|
106
|
+
enum: typeof enumSchema;
|
|
107
|
+
array: typeof array;
|
|
108
|
+
any: typeof any;
|
|
109
|
+
preprocess: typeof preprocess;
|
|
110
|
+
union: typeof union;
|
|
111
|
+
literal: typeof literal;
|
|
112
|
+
coerce: CoerceInterface;
|
|
310
113
|
cast: {
|
|
311
|
-
/**
|
|
312
|
-
* Creates a boolean schema with semantic string casting.
|
|
313
|
-
* Recognises (case-insensitive): `'true'/'false'`, `'yes'/'no'`, `'on'/'off'`, `'1'/'0'`.
|
|
314
|
-
* Numbers `1` and `0` are accepted; any other number throws.
|
|
315
|
-
* `null`, `undefined`, and unrecognised strings throw.
|
|
316
|
-
*/
|
|
317
114
|
boolean(options?: SchemaInterfaceOptions): BooleanSchemaInterface;
|
|
318
|
-
/**
|
|
319
|
-
* Creates a number schema with smart string parsing.
|
|
320
|
-
* Trims whitespace from strings before converting. Accepts booleans (`true`→1, `false`→0).
|
|
321
|
-
* Rejects `null`, `undefined`, empty/whitespace-only strings, and non-numeric strings.
|
|
322
|
-
*/
|
|
323
115
|
number(options?: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
324
|
-
/**
|
|
325
|
-
* Creates a string schema that accepts strings, finite numbers, and booleans.
|
|
326
|
-
* Rejects `null`, `undefined`, objects, arrays, `NaN`, and `Infinity`.
|
|
327
|
-
*/
|
|
328
116
|
string(options?: SchemaInterfaceOptions): StringSchemaInterface;
|
|
329
|
-
/**
|
|
330
|
-
* Creates a date schema with controlled casting.
|
|
331
|
-
* Accepts ISO date strings (non-empty), finite integer timestamps, and existing Date objects.
|
|
332
|
-
* Rejects `null`, `undefined`, booleans, empty strings, and non-finite numbers.
|
|
333
|
-
*/
|
|
334
117
|
date(options?: SchemaInterfaceOptions): DateSchemaInterface;
|
|
335
|
-
/**
|
|
336
|
-
* Parses a JSON string and validates the result against the provided schema.
|
|
337
|
-
* If the input is not a string, it is passed directly to the inner schema.
|
|
338
|
-
* Produces a proper validation failure (never throws) when the JSON is malformed.
|
|
339
|
-
*
|
|
340
|
-
* @param schema - Schema to validate the parsed value against
|
|
341
|
-
* @param options - Optional configuration (name, message)
|
|
342
|
-
* @returns The same schema type, with JSON string preprocessing applied
|
|
343
|
-
*
|
|
344
|
-
* @example
|
|
345
|
-
* ```typescript
|
|
346
|
-
* const schema = s.cast.json(s.object({ name: s.string() }));
|
|
347
|
-
* schema.parse('{"name":"Alice"}'); // { name: 'Alice' }
|
|
348
|
-
* schema.parse({ name: 'Alice' }); // { name: 'Alice' } — pass-through
|
|
349
|
-
* schema.safeParse('not json'); // { success: false, error: ... }
|
|
350
|
-
* ```
|
|
351
|
-
*/
|
|
352
118
|
json<T extends SchemaType>(schema: T, options?: SchemaInterfaceOptions): T;
|
|
353
119
|
};
|
|
354
120
|
};
|
|
@@ -411,4 +177,4 @@ declare function extend(callback: ExtenderType): void;
|
|
|
411
177
|
*/
|
|
412
178
|
type Infer<T> = T extends SchemaType ? ReturnType<T['parse']> : unknown;
|
|
413
179
|
|
|
414
|
-
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, extend, hookOriginal, s, s as schema };
|
|
180
|
+
export { type ArraySchemaInterface, type BooleanSchemaInterface, type CoerceInterface, 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, 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,m as cast,e as date,g as enumSchema,p as extend,f as functionSchema,o as hookOriginal,l as literal,c as number,a as object,j as preprocess,n as s,n as schema,b as string,k as union}from'./chunk-AKXLQP64.mjs';
|