@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/dist/index.d.mts 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 const s: {
75
- /**
76
- * Creates an object schema with validated fields.
77
- *
78
- * @param definition - Object containing field schemas
79
- * @param options - Optional configuration (name, message)
80
- * @returns Object schema interface
81
- *
82
- * @example
83
- * ```typescript
84
- * const userSchema = s.object({
85
- * name: s.string(),
86
- * age: s.number()
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
- * Creates an enum schema with predefined values.
145
- *
146
- * @param definition - Array of allowed string values
147
- * @param options - Optional configuration (name, message)
148
- * @returns Enum schema interface
149
- *
150
- * @example
151
- * ```typescript
152
- * const roleSchema = s.enum(['admin', 'user', 'guest']);
153
- * const result = roleSchema.parse('admin'); // 'admin'
154
- * ```
155
- */
156
- enum(definition: Readonly<Array<string>>, options?: SchemaInterfaceOptions): EnumSchemaInterface<(typeof definition)[number]>;
157
- /**
158
- * Creates an array schema with element validation.
159
- *
160
- * @param definition - Schema for array elements
161
- * @param options - Optional configuration (name, message)
162
- * @returns Array schema interface
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 };