@zod-utils/core 1.0.0 → 1.2.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 CHANGED
@@ -35,6 +35,8 @@ npm install @zod-utils/core zod
35
35
 
36
36
  Extract all default values from a Zod object schema. Only extracts fields that explicitly have `.default()` on them.
37
37
 
38
+ **Transform support:** Works with schemas that have `.transform()` - extracts defaults from the input type.
39
+
38
40
  ```typescript
39
41
  import { getSchemaDefaults } from "@zod-utils/core";
40
42
  import { z } from "zod";
@@ -137,9 +139,11 @@ requiresValidInput(middleName); // false - user can leave null
137
139
 
138
140
  ### `getPrimitiveType(field)`
139
141
 
140
- Get the primitive type of a Zod field by unwrapping optional/nullable wrappers.
142
+ Get the primitive type of a Zod field by unwrapping optional/nullable/transform wrappers.
141
143
  Stops at arrays without unwrapping them.
142
144
 
145
+ **Transform support:** Automatically unwraps `.transform()` to get the underlying input type.
146
+
143
147
  ```typescript
144
148
  import { getPrimitiveType } from "@zod-utils/core";
145
149
  import { z } from "zod";
@@ -151,6 +155,11 @@ const primitive = getPrimitiveType(field);
151
155
  const arrayField = z.array(z.string()).optional();
152
156
  const arrayPrimitive = getPrimitiveType(arrayField);
153
157
  // Returns the ZodArray (stops at arrays)
158
+
159
+ // Transform support
160
+ const transformed = z.string().transform((val) => val.toUpperCase());
161
+ const primitiveFromTransform = getPrimitiveType(transformed);
162
+ // Returns the underlying ZodString (unwraps the transform)
154
163
  ```
155
164
 
156
165
  ---
@@ -172,34 +181,40 @@ withoutDefault.parse(undefined); // throws error
172
181
 
173
182
  ---
174
183
 
175
- ### `extractDefault(field)`
184
+ ### `extractDefaultValue(field)`
176
185
 
177
- Extract the default value from a Zod field (recursively unwraps optional/nullable/union layers).
186
+ Extract the default value from a Zod field (recursively unwraps optional/nullable/union/transform layers).
178
187
 
179
188
  **Union handling:** For union types, extracts the default from the first option. If the first option has no default, returns `undefined` (defaults in other union options are not checked).
180
189
 
190
+ **Transform support:** Automatically unwraps `.transform()` to get the input type's default value.
191
+
181
192
  ```typescript
182
- import { extractDefault } from "@zod-utils/core";
193
+ import { extractDefaultValue } from "@zod-utils/core";
183
194
  import { z } from "zod";
184
195
 
185
196
  // Basic usage
186
197
  const field = z.string().optional().default("hello");
187
- extractDefault(field); // 'hello'
198
+ extractDefaultValue(field); // 'hello'
188
199
 
189
200
  const noDefault = z.string();
190
- extractDefault(noDefault); // undefined
201
+ extractDefaultValue(noDefault); // undefined
191
202
 
192
203
  // Union with default in first option
193
204
  const unionField = z.union([z.string().default('hello'), z.number()]);
194
- extractDefault(unionField); // 'hello'
205
+ extractDefaultValue(unionField); // 'hello'
195
206
 
196
207
  // Union with default in second option (only checks first)
197
208
  const unionField2 = z.union([z.string(), z.number().default(42)]);
198
- extractDefault(unionField2); // undefined
209
+ extractDefaultValue(unionField2); // undefined
199
210
 
200
211
  // Union wrapped in optional
201
212
  const wrappedUnion = z.union([z.string().default('test'), z.number()]).optional();
202
- extractDefault(wrappedUnion); // 'test'
213
+ extractDefaultValue(wrappedUnion); // 'test'
214
+
215
+ // Transform support - extracts input default, not output
216
+ const transformed = z.string().default('hello').transform((val) => val.toUpperCase());
217
+ extractDefaultValue(transformed); // 'hello' (not 'HELLO')
203
218
  ```
204
219
 
205
220
  ---
@@ -319,6 +334,8 @@ const editSchema = extractDiscriminatedSchema({
319
334
 
320
335
  Extract a single field from a Zod object or discriminated union schema.
321
336
 
337
+ **Transform support:** Works with schemas that have `.transform()` - extracts fields from the input type.
338
+
322
339
  ```typescript
323
340
  import { extractFieldFromSchema } from "@zod-utils/core";
324
341
  import { z } from "zod";
@@ -367,6 +384,20 @@ const fieldWithoutDiscriminator = extractFieldFromSchema({
367
384
  fieldName: 'name',
368
385
  });
369
386
  // Returns: undefined (need discriminator to know which variant)
387
+
388
+ // Works with transforms - extracts from input type
389
+ const transformedSchema = z
390
+ .object({
391
+ name: z.string(),
392
+ age: z.number(),
393
+ })
394
+ .transform((data) => ({ ...data, computed: true }));
395
+
396
+ const nameFromTransformed = extractFieldFromSchema({
397
+ schema: transformedSchema,
398
+ fieldName: 'name',
399
+ });
400
+ // Returns: ZodString (from the input type, not affected by transform)
370
401
  ```
371
402
 
372
403
  **Discriminated union support:** When extracting fields from discriminated unions, you must provide the `discriminator` option with `key` and `value` to specify which variant to use.
package/dist/index.d.mts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as z from 'zod';
2
- import { util, z as z$1 } from 'zod';
3
- import { $InferUnionOutput, $ZodCheckLessThanDef, $ZodCheckGreaterThanDef, $ZodCheckMultipleOfDef, $ZodCheckNumberFormatDef, $ZodCheckBigIntFormatDef, $ZodCheckMaxSizeDef, $ZodCheckMinSizeDef, $ZodCheckSizeEqualsDef, $ZodCheckMaxLengthDef, $ZodCheckMinLengthDef, $ZodCheckLengthEqualsDef, $ZodCheckStringFormatDef, $ZodCheckRegexDef, $ZodCheckLowerCaseDef, $ZodCheckUpperCaseDef, $ZodCheckIncludesDef, $ZodCheckStartsWithDef, $ZodCheckEndsWithDef, $ZodCheckPropertyDef, $ZodCheckMimeTypeDef, $ZodCheckOverwriteDef } from 'zod/v4/core';
2
+ import { z as z$1, util } from 'zod';
3
+ import { $InferUnionInput, $ZodCheckLessThanDef, $ZodCheckGreaterThanDef, $ZodCheckMultipleOfDef, $ZodCheckNumberFormatDef, $ZodCheckBigIntFormatDef, $ZodCheckMaxSizeDef, $ZodCheckMinSizeDef, $ZodCheckSizeEqualsDef, $ZodCheckMaxLengthDef, $ZodCheckMinLengthDef, $ZodCheckLengthEqualsDef, $ZodCheckStringFormatDef, $ZodCheckRegexDef, $ZodCheckLowerCaseDef, $ZodCheckUpperCaseDef, $ZodCheckIncludesDef, $ZodCheckStartsWithDef, $ZodCheckEndsWithDef, $ZodCheckPropertyDef, $ZodCheckMimeTypeDef, $ZodCheckOverwriteDef } from 'zod/v4/core';
4
4
 
5
5
  /**
6
6
  * Simplifies complex TypeScript types for better IDE hover tooltips and error messages.
@@ -35,8 +35,8 @@ import { $InferUnionOutput, $ZodCheckLessThanDef, $ZodCheckGreaterThanDef, $ZodC
35
35
  * const schema = z.object({ id: z.string() })
36
36
  * .merge(z.object({ name: z.string() }));
37
37
  *
38
- * type InferredType = z.infer<typeof schema>; // May show complex type
39
- * type SimplifiedType = Simplify<z.infer<typeof schema>>;
38
+ * type InferredType = z.input<typeof schema>; // May show complex type
39
+ * type SimplifiedType = Simplify<z.input<typeof schema>>;
40
40
  * // Shows clear: { id: string; name: string }
41
41
  * ```
42
42
  *
@@ -45,6 +45,38 @@ import { $InferUnionOutput, $ZodCheckLessThanDef, $ZodCheckGreaterThanDef, $ZodC
45
45
  type Simplify<T> = {
46
46
  [K in keyof T]: T[K];
47
47
  } & {};
48
+ /**
49
+ * Extracts all field keys from a Zod schema's input type.
50
+ *
51
+ * @example
52
+ * ```typescript
53
+ * const schema = z.object({ name: z.string(), age: z.number() });
54
+ * type Keys = DiscriminatorKey<typeof schema>;
55
+ * // "name" | "age"
56
+ * ```
57
+ */
58
+ type DiscriminatorKey<TSchema extends z$1.ZodType> = keyof z$1.input<TSchema> & string;
59
+ /**
60
+ * Extracts the value type for a discriminator field.
61
+ *
62
+ * @example
63
+ * ```typescript
64
+ * const schema = z.discriminatedUnion('mode', [
65
+ * z.object({ mode: z.literal('create'), name: z.string() }),
66
+ * z.object({ mode: z.literal('edit'), id: z.number() }),
67
+ * ]);
68
+ * type Mode = DiscriminatorValue<typeof schema, 'mode'>;
69
+ * // "create" | "edit"
70
+ * ```
71
+ */
72
+ type DiscriminatorValue<TSchema extends z$1.ZodType, TDiscriminatorKey extends DiscriminatorKey<TSchema>> = TDiscriminatorKey extends string ? z$1.input<TSchema>[TDiscriminatorKey] & util.Literal : never;
73
+ /**
74
+ * Discriminator configuration for discriminated union schemas.
75
+ */
76
+ type Discriminator<TSchema extends z$1.ZodType, TDiscriminatorKey extends DiscriminatorKey<TSchema>, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey>> = {
77
+ key: TDiscriminatorKey;
78
+ value: TDiscriminatorValue;
79
+ };
48
80
 
49
81
  /**
50
82
  * Extracts the default value from a Zod field, recursively unwrapping optional, nullable, and union layers.
@@ -64,7 +96,7 @@ type Simplify<T> = {
64
96
  * Basic usage with default value
65
97
  * ```typescript
66
98
  * const field = z.string().default('hello');
67
- * const defaultValue = extractDefault(field);
99
+ * const defaultValue = extractDefaultValue(field);
68
100
  * // Result: 'hello'
69
101
  * ```
70
102
  *
@@ -72,7 +104,7 @@ type Simplify<T> = {
72
104
  * Unwrapping optional/nullable layers
73
105
  * ```typescript
74
106
  * const field = z.string().default('world').optional();
75
- * const defaultValue = extractDefault(field);
107
+ * const defaultValue = extractDefaultValue(field);
76
108
  * // Result: 'world' (unwraps optional to find default)
77
109
  * ```
78
110
  *
@@ -80,7 +112,7 @@ type Simplify<T> = {
80
112
  * Union with only nullish types stripped to single type
81
113
  * ```typescript
82
114
  * const field = z.union([z.string().default('hello'), z.null()]);
83
- * const defaultValue = extractDefault(field);
115
+ * const defaultValue = extractDefaultValue(field);
84
116
  * // Result: 'hello' (null stripped, leaving only string)
85
117
  * ```
86
118
  *
@@ -88,7 +120,7 @@ type Simplify<T> = {
88
120
  * Union with multiple non-nullish types
89
121
  * ```typescript
90
122
  * const field = z.union([z.string().default('hello'), z.number()]);
91
- * const defaultValue = extractDefault(field);
123
+ * const defaultValue = extractDefaultValue(field);
92
124
  * // Result: undefined (multiple non-nullish types - no default extracted)
93
125
  * ```
94
126
  *
@@ -96,7 +128,7 @@ type Simplify<T> = {
96
128
  * Field without default
97
129
  * ```typescript
98
130
  * const field = z.string().optional();
99
- * const defaultValue = extractDefault(field);
131
+ * const defaultValue = extractDefaultValue(field);
100
132
  * // Result: undefined
101
133
  * ```
102
134
  *
@@ -104,7 +136,7 @@ type Simplify<T> = {
104
136
  * @see {@link tryStripNullishOnly} for union nullish stripping logic
105
137
  * @since 0.1.0
106
138
  */
107
- declare function extractDefault<T extends z.ZodTypeAny>(field: T): z.infer<T> | undefined;
139
+ declare function extractDefaultValue<T extends z.ZodType>(field: T): z.input<T> | undefined;
108
140
  /**
109
141
  * Extracts default values from a Zod object schema, returning only fields with explicit `.default()`.
110
142
  *
@@ -174,30 +206,24 @@ declare function extractDefault<T extends z.ZodTypeAny>(field: T): z.infer<T> |
174
206
  * <Input type="number" value={field.value ?? ''} />
175
207
  * ```
176
208
  *
177
- * @see {@link extractDefault} for extracting defaults from individual fields
209
+ * @see {@link extractDefaultValue} for extracting defaults from individual fields
178
210
  * @since 0.1.0
179
211
  */
180
- declare function getSchemaDefaults<TSchema extends z.ZodObject | z.ZodDiscriminatedUnion, TDiscriminatorKey extends keyof z.infer<TSchema> & string, TDiscriminatorValue extends z.infer<TSchema>[TDiscriminatorKey] & util.Literal>(schema: TSchema, options?: {
181
- discriminator?: {
182
- key: TDiscriminatorKey;
183
- value: TDiscriminatorValue;
184
- };
185
- }): Simplify<Partial<z.infer<TSchema>>>;
212
+ declare function getSchemaDefaults<TSchema extends z.ZodType, TDiscriminatorKey extends DiscriminatorKey<TSchema>, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey>>(schema: TSchema, options?: {
213
+ discriminator?: Discriminator<TSchema, TDiscriminatorKey, TDiscriminatorValue>;
214
+ }): Simplify<Partial<z.input<TSchema>>>;
186
215
 
187
- declare function extractFieldFromSchema<TSchema extends z$1.ZodObject | z$1.ZodDiscriminatedUnion, TName extends keyof Extract<Required<z$1.infer<TSchema>>, Record<TDiscriminatorKey, TDiscriminatorValue>>, TDiscriminatorKey extends keyof z$1.infer<TSchema> & string, TDiscriminatorValue extends z$1.infer<TSchema>[TDiscriminatorKey] & util.Literal>({ schema, fieldName, discriminator, }: {
216
+ declare function extractFieldFromSchema<TSchema extends z$1.ZodType, TName extends keyof Extract<Required<z$1.input<TSchema>>, Record<TDiscriminatorKey, TDiscriminatorValue>>, TDiscriminatorKey extends DiscriminatorKey<TSchema>, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey>>({ schema, fieldName, discriminator, }: {
188
217
  schema: TSchema;
189
218
  fieldName: TName;
190
- discriminator?: {
191
- key: TDiscriminatorKey;
192
- value: TDiscriminatorValue;
193
- };
219
+ discriminator?: Discriminator<TSchema, TDiscriminatorKey, TDiscriminatorValue>;
194
220
  }): z$1.ZodType<unknown, unknown, z$1.core.$ZodTypeInternals<unknown, unknown>> | undefined;
195
221
 
196
222
  /**
197
223
  * Type representing a Zod type that has an unwrap method
198
224
  */
199
225
  type Unwrappable = {
200
- unwrap: () => z.ZodTypeAny;
226
+ unwrap: () => z$1.ZodTypeAny;
201
227
  };
202
228
  /**
203
229
  * Type guard to check if a Zod field can be unwrapped (has wrapper types like optional, nullable, default).
@@ -219,7 +245,7 @@ type Unwrappable = {
219
245
  *
220
246
  * @since 0.1.0
221
247
  */
222
- declare function canUnwrap(field: z.ZodTypeAny): field is z.ZodTypeAny & Unwrappable;
248
+ declare function canUnwrap(field: z$1.ZodTypeAny): field is z$1.ZodTypeAny & Unwrappable;
223
249
  /**
224
250
  * Attempts to strip nullish types from a union and return the single remaining type.
225
251
  *
@@ -257,7 +283,7 @@ declare function canUnwrap(field: z.ZodTypeAny): field is z.ZodTypeAny & Unwrapp
257
283
  * @see {@link getPrimitiveType} for unwrapping wrapper types
258
284
  * @since 0.5.0
259
285
  */
260
- declare function tryStripNullishOnly(field: z.ZodTypeAny): z.ZodType | false;
286
+ declare function tryStripNullishOnly(field: z$1.ZodTypeAny): z$1.ZodType | false;
261
287
  /**
262
288
  * Gets the underlying primitive type of a Zod field by recursively unwrapping wrapper types.
263
289
  *
@@ -316,8 +342,8 @@ declare function tryStripNullishOnly(field: z.ZodTypeAny): z.ZodType | false;
316
342
  * @see {@link tryStripNullishOnly} for union nullish stripping logic
317
343
  * @since 0.1.0
318
344
  */
319
- declare const getPrimitiveType: <T extends z.ZodType>(field: T) => z.ZodTypeAny;
320
- type StripZodDefault<T> = T extends z.ZodDefault<infer Inner> ? StripZodDefault<Inner> : T extends z.ZodOptional<infer Inner> ? z.ZodOptional<StripZodDefault<Inner>> : T extends z.ZodNullable<infer Inner> ? z.ZodNullable<StripZodDefault<Inner>> : T;
345
+ declare const getPrimitiveType: <T extends z$1.ZodType>(field: T) => z$1.ZodTypeAny;
346
+ type StripZodDefault<T> = T extends z$1.ZodDefault<infer Inner> ? StripZodDefault<Inner> : T extends z$1.ZodOptional<infer Inner> ? z$1.ZodOptional<StripZodDefault<Inner>> : T extends z$1.ZodNullable<infer Inner> ? z$1.ZodNullable<StripZodDefault<Inner>> : T;
321
347
  /**
322
348
  * Removes default values from a Zod field while preserving other wrapper types.
323
349
  *
@@ -356,7 +382,7 @@ type StripZodDefault<T> = T extends z.ZodDefault<infer Inner> ? StripZodDefault<
356
382
  * @see {@link requiresValidInput} for usage with requirement checking
357
383
  * @since 0.1.0
358
384
  */
359
- declare function removeDefault<T extends z.ZodType>(field: T): StripZodDefault<T>;
385
+ declare function removeDefault<T extends z$1.ZodType>(field: T): StripZodDefault<T>;
360
386
  /**
361
387
  * Determines if a field will show validation errors when the user submits empty or invalid input.
362
388
  *
@@ -434,7 +460,7 @@ declare function removeDefault<T extends z.ZodType>(field: T): StripZodDefault<T
434
460
  * @see {@link getPrimitiveType} for understanding type unwrapping
435
461
  * @since 0.1.0
436
462
  */
437
- declare const requiresValidInput: <T extends z.ZodType>(field: T) => boolean;
463
+ declare const requiresValidInput: <T extends z$1.ZodType>(field: T) => boolean;
438
464
  /**
439
465
  * Union type of all Zod check definition types.
440
466
  *
@@ -524,7 +550,7 @@ type ZodUnionCheck = $ZodCheckLessThanDef | $ZodCheckGreaterThanDef | $ZodCheckM
524
550
  * @see {@link ZodUnionCheck} for all supported check types
525
551
  * @since 0.4.0
526
552
  */
527
- declare function getFieldChecks<T extends z.ZodTypeAny>(field: T): Array<ZodUnionCheck>;
553
+ declare function getFieldChecks<T extends z$1.ZodTypeAny>(field: T): Array<ZodUnionCheck>;
528
554
  /**
529
555
  * Recursively extracts the exact schema type from a discriminated union based on the discriminator value.
530
556
  *
@@ -560,10 +586,10 @@ declare function getFieldChecks<T extends z.ZodTypeAny>(field: T): Array<ZodUnio
560
586
  * type CreateSchema = ExtractZodUnionMember<typeof schema, 'mode', 'create'>;
561
587
  * ```
562
588
  */
563
- type ExtractZodUnionMember<TSchema extends z.ZodUnion | z.ZodDiscriminatedUnion, TDiscriminatorKey extends keyof z.infer<TSchema> & string, TDiscriminatorValue extends z.infer<TSchema>[TDiscriminatorKey] & util.Literal> = TSchema extends z.ZodUnion<infer Options> ? Options extends readonly [
564
- infer First extends z.ZodTypeAny,
565
- ...infer Rest extends z.ZodTypeAny[]
566
- ] ? First extends z.ZodObject<infer Shape> ? TDiscriminatorKey extends keyof Shape ? Shape[TDiscriminatorKey] extends z.ZodLiteral<TDiscriminatorValue> ? First : Rest extends [] ? never : TDiscriminatorValue extends $InferUnionOutput<Rest[number]>[TDiscriminatorKey] ? ExtractZodUnionMember<z.ZodUnion<Rest>, TDiscriminatorKey, TDiscriminatorValue> : never : Rest extends [] ? never : TDiscriminatorValue extends $InferUnionOutput<Rest[number]>[TDiscriminatorKey] ? ExtractZodUnionMember<z.ZodUnion<Rest>, TDiscriminatorKey, TDiscriminatorValue> : never : never : never : never;
589
+ type ExtractZodUnionMember<TSchema extends z$1.ZodUnion | z$1.ZodDiscriminatedUnion, TDiscriminatorKey extends DiscriminatorKey<TSchema>, TDiscriminatorValue extends z$1.input<TSchema>[TDiscriminatorKey] & util.Literal> = TSchema extends z$1.ZodUnion<infer Options> ? Options extends readonly [
590
+ infer First extends z$1.ZodTypeAny,
591
+ ...infer Rest extends z$1.ZodTypeAny[]
592
+ ] ? First extends z$1.ZodObject<infer Shape> ? TDiscriminatorKey extends keyof Shape ? Shape[TDiscriminatorKey] extends z$1.ZodLiteral<TDiscriminatorValue> | z$1.ZodDefault<z$1.ZodLiteral<TDiscriminatorValue>> ? First : Rest extends [] ? never : TDiscriminatorValue extends $InferUnionInput<Rest[number]>[TDiscriminatorKey] ? ExtractZodUnionMember<z$1.ZodUnion<Rest>, TDiscriminatorKey, TDiscriminatorValue> : never : Rest extends [] ? never : TDiscriminatorValue extends $InferUnionInput<Rest[number]>[TDiscriminatorKey] ? ExtractZodUnionMember<z$1.ZodUnion<Rest>, TDiscriminatorKey, TDiscriminatorValue> : never : never : never : never;
567
593
  /**
568
594
  * Extracts a specific schema option from a discriminated union based on the discriminator field value.
569
595
  *
@@ -682,10 +708,8 @@ type ExtractZodUnionMember<TSchema extends z.ZodUnion | z.ZodDiscriminatedUnion,
682
708
  * @see {@link ExtractZodUnionMember} for the type-level extraction logic
683
709
  * @since 0.6.0
684
710
  */
685
- declare const extractDiscriminatedSchema: <TSchema extends z.ZodUnion | z.ZodDiscriminatedUnion, TDiscriminatorKey extends keyof z.infer<TSchema> & string, TDiscriminatorValue extends z.infer<TSchema>[TDiscriminatorKey] & util.Literal>({ schema, key, value, }: {
711
+ declare const extractDiscriminatedSchema: <TSchema extends z$1.ZodType, TDiscriminatorKey extends DiscriminatorKey<TSchema>, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey>, ReturnType extends TSchema extends z$1.ZodDiscriminatedUnion ? ExtractZodUnionMember<TSchema, TDiscriminatorKey, TDiscriminatorValue> : never>({ schema, key, value, }: {
686
712
  schema: TSchema;
687
- key: TDiscriminatorKey;
688
- value: TDiscriminatorValue;
689
- }) => ExtractZodUnionMember<TSchema, TDiscriminatorKey, TDiscriminatorValue> | undefined;
713
+ } & Discriminator<TSchema, TDiscriminatorKey, TDiscriminatorValue>) => ReturnType;
690
714
 
691
- export { type Simplify, type ZodUnionCheck, canUnwrap, extractDefault, extractDiscriminatedSchema, extractFieldFromSchema, getFieldChecks, getPrimitiveType, getSchemaDefaults, removeDefault, requiresValidInput, tryStripNullishOnly };
715
+ export { type Discriminator, type DiscriminatorKey, type DiscriminatorValue, type Simplify, type ZodUnionCheck, canUnwrap, extractDefaultValue, extractDiscriminatedSchema, extractFieldFromSchema, getFieldChecks, getPrimitiveType, getSchemaDefaults, removeDefault, requiresValidInput, tryStripNullishOnly };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as z from 'zod';
2
- import { util, z as z$1 } from 'zod';
3
- import { $InferUnionOutput, $ZodCheckLessThanDef, $ZodCheckGreaterThanDef, $ZodCheckMultipleOfDef, $ZodCheckNumberFormatDef, $ZodCheckBigIntFormatDef, $ZodCheckMaxSizeDef, $ZodCheckMinSizeDef, $ZodCheckSizeEqualsDef, $ZodCheckMaxLengthDef, $ZodCheckMinLengthDef, $ZodCheckLengthEqualsDef, $ZodCheckStringFormatDef, $ZodCheckRegexDef, $ZodCheckLowerCaseDef, $ZodCheckUpperCaseDef, $ZodCheckIncludesDef, $ZodCheckStartsWithDef, $ZodCheckEndsWithDef, $ZodCheckPropertyDef, $ZodCheckMimeTypeDef, $ZodCheckOverwriteDef } from 'zod/v4/core';
2
+ import { z as z$1, util } from 'zod';
3
+ import { $InferUnionInput, $ZodCheckLessThanDef, $ZodCheckGreaterThanDef, $ZodCheckMultipleOfDef, $ZodCheckNumberFormatDef, $ZodCheckBigIntFormatDef, $ZodCheckMaxSizeDef, $ZodCheckMinSizeDef, $ZodCheckSizeEqualsDef, $ZodCheckMaxLengthDef, $ZodCheckMinLengthDef, $ZodCheckLengthEqualsDef, $ZodCheckStringFormatDef, $ZodCheckRegexDef, $ZodCheckLowerCaseDef, $ZodCheckUpperCaseDef, $ZodCheckIncludesDef, $ZodCheckStartsWithDef, $ZodCheckEndsWithDef, $ZodCheckPropertyDef, $ZodCheckMimeTypeDef, $ZodCheckOverwriteDef } from 'zod/v4/core';
4
4
 
5
5
  /**
6
6
  * Simplifies complex TypeScript types for better IDE hover tooltips and error messages.
@@ -35,8 +35,8 @@ import { $InferUnionOutput, $ZodCheckLessThanDef, $ZodCheckGreaterThanDef, $ZodC
35
35
  * const schema = z.object({ id: z.string() })
36
36
  * .merge(z.object({ name: z.string() }));
37
37
  *
38
- * type InferredType = z.infer<typeof schema>; // May show complex type
39
- * type SimplifiedType = Simplify<z.infer<typeof schema>>;
38
+ * type InferredType = z.input<typeof schema>; // May show complex type
39
+ * type SimplifiedType = Simplify<z.input<typeof schema>>;
40
40
  * // Shows clear: { id: string; name: string }
41
41
  * ```
42
42
  *
@@ -45,6 +45,38 @@ import { $InferUnionOutput, $ZodCheckLessThanDef, $ZodCheckGreaterThanDef, $ZodC
45
45
  type Simplify<T> = {
46
46
  [K in keyof T]: T[K];
47
47
  } & {};
48
+ /**
49
+ * Extracts all field keys from a Zod schema's input type.
50
+ *
51
+ * @example
52
+ * ```typescript
53
+ * const schema = z.object({ name: z.string(), age: z.number() });
54
+ * type Keys = DiscriminatorKey<typeof schema>;
55
+ * // "name" | "age"
56
+ * ```
57
+ */
58
+ type DiscriminatorKey<TSchema extends z$1.ZodType> = keyof z$1.input<TSchema> & string;
59
+ /**
60
+ * Extracts the value type for a discriminator field.
61
+ *
62
+ * @example
63
+ * ```typescript
64
+ * const schema = z.discriminatedUnion('mode', [
65
+ * z.object({ mode: z.literal('create'), name: z.string() }),
66
+ * z.object({ mode: z.literal('edit'), id: z.number() }),
67
+ * ]);
68
+ * type Mode = DiscriminatorValue<typeof schema, 'mode'>;
69
+ * // "create" | "edit"
70
+ * ```
71
+ */
72
+ type DiscriminatorValue<TSchema extends z$1.ZodType, TDiscriminatorKey extends DiscriminatorKey<TSchema>> = TDiscriminatorKey extends string ? z$1.input<TSchema>[TDiscriminatorKey] & util.Literal : never;
73
+ /**
74
+ * Discriminator configuration for discriminated union schemas.
75
+ */
76
+ type Discriminator<TSchema extends z$1.ZodType, TDiscriminatorKey extends DiscriminatorKey<TSchema>, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey>> = {
77
+ key: TDiscriminatorKey;
78
+ value: TDiscriminatorValue;
79
+ };
48
80
 
49
81
  /**
50
82
  * Extracts the default value from a Zod field, recursively unwrapping optional, nullable, and union layers.
@@ -64,7 +96,7 @@ type Simplify<T> = {
64
96
  * Basic usage with default value
65
97
  * ```typescript
66
98
  * const field = z.string().default('hello');
67
- * const defaultValue = extractDefault(field);
99
+ * const defaultValue = extractDefaultValue(field);
68
100
  * // Result: 'hello'
69
101
  * ```
70
102
  *
@@ -72,7 +104,7 @@ type Simplify<T> = {
72
104
  * Unwrapping optional/nullable layers
73
105
  * ```typescript
74
106
  * const field = z.string().default('world').optional();
75
- * const defaultValue = extractDefault(field);
107
+ * const defaultValue = extractDefaultValue(field);
76
108
  * // Result: 'world' (unwraps optional to find default)
77
109
  * ```
78
110
  *
@@ -80,7 +112,7 @@ type Simplify<T> = {
80
112
  * Union with only nullish types stripped to single type
81
113
  * ```typescript
82
114
  * const field = z.union([z.string().default('hello'), z.null()]);
83
- * const defaultValue = extractDefault(field);
115
+ * const defaultValue = extractDefaultValue(field);
84
116
  * // Result: 'hello' (null stripped, leaving only string)
85
117
  * ```
86
118
  *
@@ -88,7 +120,7 @@ type Simplify<T> = {
88
120
  * Union with multiple non-nullish types
89
121
  * ```typescript
90
122
  * const field = z.union([z.string().default('hello'), z.number()]);
91
- * const defaultValue = extractDefault(field);
123
+ * const defaultValue = extractDefaultValue(field);
92
124
  * // Result: undefined (multiple non-nullish types - no default extracted)
93
125
  * ```
94
126
  *
@@ -96,7 +128,7 @@ type Simplify<T> = {
96
128
  * Field without default
97
129
  * ```typescript
98
130
  * const field = z.string().optional();
99
- * const defaultValue = extractDefault(field);
131
+ * const defaultValue = extractDefaultValue(field);
100
132
  * // Result: undefined
101
133
  * ```
102
134
  *
@@ -104,7 +136,7 @@ type Simplify<T> = {
104
136
  * @see {@link tryStripNullishOnly} for union nullish stripping logic
105
137
  * @since 0.1.0
106
138
  */
107
- declare function extractDefault<T extends z.ZodTypeAny>(field: T): z.infer<T> | undefined;
139
+ declare function extractDefaultValue<T extends z.ZodType>(field: T): z.input<T> | undefined;
108
140
  /**
109
141
  * Extracts default values from a Zod object schema, returning only fields with explicit `.default()`.
110
142
  *
@@ -174,30 +206,24 @@ declare function extractDefault<T extends z.ZodTypeAny>(field: T): z.infer<T> |
174
206
  * <Input type="number" value={field.value ?? ''} />
175
207
  * ```
176
208
  *
177
- * @see {@link extractDefault} for extracting defaults from individual fields
209
+ * @see {@link extractDefaultValue} for extracting defaults from individual fields
178
210
  * @since 0.1.0
179
211
  */
180
- declare function getSchemaDefaults<TSchema extends z.ZodObject | z.ZodDiscriminatedUnion, TDiscriminatorKey extends keyof z.infer<TSchema> & string, TDiscriminatorValue extends z.infer<TSchema>[TDiscriminatorKey] & util.Literal>(schema: TSchema, options?: {
181
- discriminator?: {
182
- key: TDiscriminatorKey;
183
- value: TDiscriminatorValue;
184
- };
185
- }): Simplify<Partial<z.infer<TSchema>>>;
212
+ declare function getSchemaDefaults<TSchema extends z.ZodType, TDiscriminatorKey extends DiscriminatorKey<TSchema>, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey>>(schema: TSchema, options?: {
213
+ discriminator?: Discriminator<TSchema, TDiscriminatorKey, TDiscriminatorValue>;
214
+ }): Simplify<Partial<z.input<TSchema>>>;
186
215
 
187
- declare function extractFieldFromSchema<TSchema extends z$1.ZodObject | z$1.ZodDiscriminatedUnion, TName extends keyof Extract<Required<z$1.infer<TSchema>>, Record<TDiscriminatorKey, TDiscriminatorValue>>, TDiscriminatorKey extends keyof z$1.infer<TSchema> & string, TDiscriminatorValue extends z$1.infer<TSchema>[TDiscriminatorKey] & util.Literal>({ schema, fieldName, discriminator, }: {
216
+ declare function extractFieldFromSchema<TSchema extends z$1.ZodType, TName extends keyof Extract<Required<z$1.input<TSchema>>, Record<TDiscriminatorKey, TDiscriminatorValue>>, TDiscriminatorKey extends DiscriminatorKey<TSchema>, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey>>({ schema, fieldName, discriminator, }: {
188
217
  schema: TSchema;
189
218
  fieldName: TName;
190
- discriminator?: {
191
- key: TDiscriminatorKey;
192
- value: TDiscriminatorValue;
193
- };
219
+ discriminator?: Discriminator<TSchema, TDiscriminatorKey, TDiscriminatorValue>;
194
220
  }): z$1.ZodType<unknown, unknown, z$1.core.$ZodTypeInternals<unknown, unknown>> | undefined;
195
221
 
196
222
  /**
197
223
  * Type representing a Zod type that has an unwrap method
198
224
  */
199
225
  type Unwrappable = {
200
- unwrap: () => z.ZodTypeAny;
226
+ unwrap: () => z$1.ZodTypeAny;
201
227
  };
202
228
  /**
203
229
  * Type guard to check if a Zod field can be unwrapped (has wrapper types like optional, nullable, default).
@@ -219,7 +245,7 @@ type Unwrappable = {
219
245
  *
220
246
  * @since 0.1.0
221
247
  */
222
- declare function canUnwrap(field: z.ZodTypeAny): field is z.ZodTypeAny & Unwrappable;
248
+ declare function canUnwrap(field: z$1.ZodTypeAny): field is z$1.ZodTypeAny & Unwrappable;
223
249
  /**
224
250
  * Attempts to strip nullish types from a union and return the single remaining type.
225
251
  *
@@ -257,7 +283,7 @@ declare function canUnwrap(field: z.ZodTypeAny): field is z.ZodTypeAny & Unwrapp
257
283
  * @see {@link getPrimitiveType} for unwrapping wrapper types
258
284
  * @since 0.5.0
259
285
  */
260
- declare function tryStripNullishOnly(field: z.ZodTypeAny): z.ZodType | false;
286
+ declare function tryStripNullishOnly(field: z$1.ZodTypeAny): z$1.ZodType | false;
261
287
  /**
262
288
  * Gets the underlying primitive type of a Zod field by recursively unwrapping wrapper types.
263
289
  *
@@ -316,8 +342,8 @@ declare function tryStripNullishOnly(field: z.ZodTypeAny): z.ZodType | false;
316
342
  * @see {@link tryStripNullishOnly} for union nullish stripping logic
317
343
  * @since 0.1.0
318
344
  */
319
- declare const getPrimitiveType: <T extends z.ZodType>(field: T) => z.ZodTypeAny;
320
- type StripZodDefault<T> = T extends z.ZodDefault<infer Inner> ? StripZodDefault<Inner> : T extends z.ZodOptional<infer Inner> ? z.ZodOptional<StripZodDefault<Inner>> : T extends z.ZodNullable<infer Inner> ? z.ZodNullable<StripZodDefault<Inner>> : T;
345
+ declare const getPrimitiveType: <T extends z$1.ZodType>(field: T) => z$1.ZodTypeAny;
346
+ type StripZodDefault<T> = T extends z$1.ZodDefault<infer Inner> ? StripZodDefault<Inner> : T extends z$1.ZodOptional<infer Inner> ? z$1.ZodOptional<StripZodDefault<Inner>> : T extends z$1.ZodNullable<infer Inner> ? z$1.ZodNullable<StripZodDefault<Inner>> : T;
321
347
  /**
322
348
  * Removes default values from a Zod field while preserving other wrapper types.
323
349
  *
@@ -356,7 +382,7 @@ type StripZodDefault<T> = T extends z.ZodDefault<infer Inner> ? StripZodDefault<
356
382
  * @see {@link requiresValidInput} for usage with requirement checking
357
383
  * @since 0.1.0
358
384
  */
359
- declare function removeDefault<T extends z.ZodType>(field: T): StripZodDefault<T>;
385
+ declare function removeDefault<T extends z$1.ZodType>(field: T): StripZodDefault<T>;
360
386
  /**
361
387
  * Determines if a field will show validation errors when the user submits empty or invalid input.
362
388
  *
@@ -434,7 +460,7 @@ declare function removeDefault<T extends z.ZodType>(field: T): StripZodDefault<T
434
460
  * @see {@link getPrimitiveType} for understanding type unwrapping
435
461
  * @since 0.1.0
436
462
  */
437
- declare const requiresValidInput: <T extends z.ZodType>(field: T) => boolean;
463
+ declare const requiresValidInput: <T extends z$1.ZodType>(field: T) => boolean;
438
464
  /**
439
465
  * Union type of all Zod check definition types.
440
466
  *
@@ -524,7 +550,7 @@ type ZodUnionCheck = $ZodCheckLessThanDef | $ZodCheckGreaterThanDef | $ZodCheckM
524
550
  * @see {@link ZodUnionCheck} for all supported check types
525
551
  * @since 0.4.0
526
552
  */
527
- declare function getFieldChecks<T extends z.ZodTypeAny>(field: T): Array<ZodUnionCheck>;
553
+ declare function getFieldChecks<T extends z$1.ZodTypeAny>(field: T): Array<ZodUnionCheck>;
528
554
  /**
529
555
  * Recursively extracts the exact schema type from a discriminated union based on the discriminator value.
530
556
  *
@@ -560,10 +586,10 @@ declare function getFieldChecks<T extends z.ZodTypeAny>(field: T): Array<ZodUnio
560
586
  * type CreateSchema = ExtractZodUnionMember<typeof schema, 'mode', 'create'>;
561
587
  * ```
562
588
  */
563
- type ExtractZodUnionMember<TSchema extends z.ZodUnion | z.ZodDiscriminatedUnion, TDiscriminatorKey extends keyof z.infer<TSchema> & string, TDiscriminatorValue extends z.infer<TSchema>[TDiscriminatorKey] & util.Literal> = TSchema extends z.ZodUnion<infer Options> ? Options extends readonly [
564
- infer First extends z.ZodTypeAny,
565
- ...infer Rest extends z.ZodTypeAny[]
566
- ] ? First extends z.ZodObject<infer Shape> ? TDiscriminatorKey extends keyof Shape ? Shape[TDiscriminatorKey] extends z.ZodLiteral<TDiscriminatorValue> ? First : Rest extends [] ? never : TDiscriminatorValue extends $InferUnionOutput<Rest[number]>[TDiscriminatorKey] ? ExtractZodUnionMember<z.ZodUnion<Rest>, TDiscriminatorKey, TDiscriminatorValue> : never : Rest extends [] ? never : TDiscriminatorValue extends $InferUnionOutput<Rest[number]>[TDiscriminatorKey] ? ExtractZodUnionMember<z.ZodUnion<Rest>, TDiscriminatorKey, TDiscriminatorValue> : never : never : never : never;
589
+ type ExtractZodUnionMember<TSchema extends z$1.ZodUnion | z$1.ZodDiscriminatedUnion, TDiscriminatorKey extends DiscriminatorKey<TSchema>, TDiscriminatorValue extends z$1.input<TSchema>[TDiscriminatorKey] & util.Literal> = TSchema extends z$1.ZodUnion<infer Options> ? Options extends readonly [
590
+ infer First extends z$1.ZodTypeAny,
591
+ ...infer Rest extends z$1.ZodTypeAny[]
592
+ ] ? First extends z$1.ZodObject<infer Shape> ? TDiscriminatorKey extends keyof Shape ? Shape[TDiscriminatorKey] extends z$1.ZodLiteral<TDiscriminatorValue> | z$1.ZodDefault<z$1.ZodLiteral<TDiscriminatorValue>> ? First : Rest extends [] ? never : TDiscriminatorValue extends $InferUnionInput<Rest[number]>[TDiscriminatorKey] ? ExtractZodUnionMember<z$1.ZodUnion<Rest>, TDiscriminatorKey, TDiscriminatorValue> : never : Rest extends [] ? never : TDiscriminatorValue extends $InferUnionInput<Rest[number]>[TDiscriminatorKey] ? ExtractZodUnionMember<z$1.ZodUnion<Rest>, TDiscriminatorKey, TDiscriminatorValue> : never : never : never : never;
567
593
  /**
568
594
  * Extracts a specific schema option from a discriminated union based on the discriminator field value.
569
595
  *
@@ -682,10 +708,8 @@ type ExtractZodUnionMember<TSchema extends z.ZodUnion | z.ZodDiscriminatedUnion,
682
708
  * @see {@link ExtractZodUnionMember} for the type-level extraction logic
683
709
  * @since 0.6.0
684
710
  */
685
- declare const extractDiscriminatedSchema: <TSchema extends z.ZodUnion | z.ZodDiscriminatedUnion, TDiscriminatorKey extends keyof z.infer<TSchema> & string, TDiscriminatorValue extends z.infer<TSchema>[TDiscriminatorKey] & util.Literal>({ schema, key, value, }: {
711
+ declare const extractDiscriminatedSchema: <TSchema extends z$1.ZodType, TDiscriminatorKey extends DiscriminatorKey<TSchema>, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey>, ReturnType extends TSchema extends z$1.ZodDiscriminatedUnion ? ExtractZodUnionMember<TSchema, TDiscriminatorKey, TDiscriminatorValue> : never>({ schema, key, value, }: {
686
712
  schema: TSchema;
687
- key: TDiscriminatorKey;
688
- value: TDiscriminatorValue;
689
- }) => ExtractZodUnionMember<TSchema, TDiscriminatorKey, TDiscriminatorValue> | undefined;
713
+ } & Discriminator<TSchema, TDiscriminatorKey, TDiscriminatorValue>) => ReturnType;
690
714
 
691
- export { type Simplify, type ZodUnionCheck, canUnwrap, extractDefault, extractDiscriminatedSchema, extractFieldFromSchema, getFieldChecks, getPrimitiveType, getSchemaDefaults, removeDefault, requiresValidInput, tryStripNullishOnly };
715
+ export { type Discriminator, type DiscriminatorKey, type DiscriminatorValue, type Simplify, type ZodUnionCheck, canUnwrap, extractDefaultValue, extractDiscriminatedSchema, extractFieldFromSchema, getFieldChecks, getPrimitiveType, getSchemaDefaults, removeDefault, requiresValidInput, tryStripNullishOnly };