@zod-utils/core 4.0.0 → 6.0.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/dist/index.d.ts CHANGED
@@ -70,11 +70,42 @@ type DiscriminatorKey<TSchema extends z$1.ZodType> = keyof z$1.input<TSchema> &
70
70
  */
71
71
  type DiscriminatorValue<TSchema extends z$1.ZodType, TDiscriminatorKey extends DiscriminatorKey<TSchema>> = TDiscriminatorKey extends string ? z$1.input<TSchema>[TDiscriminatorKey] & util.Literal : never;
72
72
  /**
73
- * Discriminator configuration for discriminated union schemas.
73
+ * Recursively unwraps Zod wrapper types to get the core schema type.
74
+ * Handles: ZodPipe (transform), ZodOptional, ZodNullable, ZodDefault
75
+ */
76
+ type UnwrapZodType<T> = T extends z$1.ZodPipe<infer In, SomeType> ? UnwrapZodType<In> : T extends z$1.ZodOptional<infer Inner> ? UnwrapZodType<Inner> : T extends z$1.ZodNullable<infer Inner> ? UnwrapZodType<Inner> : T extends z$1.ZodDefault<infer Inner> ? UnwrapZodType<Inner> : T;
77
+ /**
78
+ * Checks if the core (unwrapped) type is a ZodDiscriminatedUnion.
79
+ */
80
+ type IsDiscriminatedUnion<T> = UnwrapZodType<T> extends z$1.ZodDiscriminatedUnion ? true : false;
81
+ /**
82
+ * Conditional discriminator prop type for schemas.
83
+ *
84
+ * For discriminated unions: returns `{ discriminator: { key, value } }` (required)
85
+ * For non-unions: returns `{ discriminator?: never }` (prohibited)
86
+ *
87
+ * This enables type-safe props where discriminator is only required when the schema
88
+ * is actually a discriminated union.
89
+ *
90
+ * @example
91
+ * ```typescript
92
+ * // Use as props intersection:
93
+ * type Props<TSchema> = { schema: TSchema; name: string } & DiscriminatorProps<TSchema, TKey, TValue>;
94
+ *
95
+ * // For discriminated unions - discriminator is required
96
+ * // For regular objects - discriminator is prohibited
97
+ *
98
+ * // To extract the { key, value } object:
99
+ * DiscriminatorProps<Schema, Key, Value>['discriminator']
100
+ * ```
74
101
  */
75
- type Discriminator<TSchema extends z$1.ZodType, TDiscriminatorKey extends DiscriminatorKey<TSchema>, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey>> = {
76
- key: TDiscriminatorKey;
77
- value: TDiscriminatorValue;
102
+ type DiscriminatorProps<TSchema extends z$1.ZodType, TDiscriminatorKey extends DiscriminatorKey<TSchema> = never, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey> = never> = IsDiscriminatedUnion<TSchema> extends true ? {
103
+ discriminator: {
104
+ key: TDiscriminatorKey;
105
+ value: TDiscriminatorValue;
106
+ };
107
+ } : {
108
+ discriminator?: never;
78
109
  };
79
110
  interface FileList {
80
111
  readonly length: number;
@@ -88,15 +119,17 @@ type IsTuple<T extends ReadonlyArray<unknown>> = number extends T['length'] ? fa
88
119
  type TupleKeys<T extends ReadonlyArray<unknown>> = Exclude<keyof T, keyof unknown[]>;
89
120
  type IsEqual<T1, T2> = T1 extends T2 ? (<G>() => G extends T1 ? 1 : 2) extends <G>() => G extends T2 ? 1 : 2 ? true : false : false;
90
121
  type AnyIsEqual<T1, T2> = T1 extends T2 ? IsEqual<T1, T2> extends true ? true : never : never;
91
- type CheckFilter<V, FilterType, Strict extends boolean> = Strict extends true ? [V] extends [FilterType] ? true : false : V extends FilterType ? true : never;
122
+ type HasMatch<V, TFilterType> = V extends TFilterType ? true : false;
123
+ type IsNullable<V> = null extends V ? true : undefined extends V ? true : false;
124
+ type CheckFilter<V, TFilterType, TStrict extends boolean> = TStrict extends true ? [V] extends [TFilterType] ? true : false : true extends HasMatch<V, TFilterType> ? true : false;
92
125
  type ArrayPaths = '${number}' | `${number}`;
93
- type PathImpl<K extends string | number, V, TraversedTypes, FilterType = unknown, Strict extends boolean = true> = [V] extends [Primitive | BrowserNativeObject] ? CheckFilter<V, FilterType, Strict> extends true ? `${K}` : never : true extends AnyIsEqual<TraversedTypes, V> ? CheckFilter<V, FilterType, Strict> extends true ? `${K}` : never : K extends number ? (CheckFilter<V, FilterType, Strict> extends true ? ArrayPaths : never) | `${ArrayPaths}.${PathInternal<V, TraversedTypes | V, FilterType, Strict>}` : (CheckFilter<V, FilterType, Strict> extends true ? `${K}` : never) | `${K}.${PathInternal<V, TraversedTypes | V, FilterType, Strict>}`;
94
- type PathInternal<T, TraversedTypes = T, FilterType = unknown, Strict extends boolean = true> = T extends ReadonlyArray<infer V> ? IsTuple<T> extends true ? {
95
- [K in TupleKeys<T>]-?: PathImpl<K & string, T[K], TraversedTypes, FilterType, Strict>;
96
- }[TupleKeys<T>] : PathImpl<ArrayKey, V, TraversedTypes, FilterType, Strict> : {
97
- [K in keyof T]-?: PathImpl<K & string, T[K], TraversedTypes, FilterType, Strict>;
126
+ type PathImpl<K extends string | number, V, TraversedTypes, TFilterType = unknown, TStrict extends boolean = true> = [V] extends [Primitive | BrowserNativeObject] ? CheckFilter<V, TFilterType, TStrict> extends true ? K extends number ? ArrayPaths : `${K}` : never : true extends AnyIsEqual<TraversedTypes, V> ? CheckFilter<V, TFilterType, TStrict> extends true ? `${K}` : never : K extends number ? (CheckFilter<V, TFilterType, TStrict> extends true ? ArrayPaths : never) | `${ArrayPaths}.${PathInternal<V, TraversedTypes | V, TFilterType, TStrict>}` : (CheckFilter<V, TFilterType, TStrict> extends true ? `${K}` : never) | (unknown extends TFilterType ? `${K}.${PathInternal<V, TraversedTypes | V, TFilterType, TStrict>}` : IsNullable<V> extends true ? TStrict extends true ? never : `${K}.${PathInternal<V, TraversedTypes | V, TFilterType, TStrict>}` : `${K}.${PathInternal<V, TraversedTypes | V, TFilterType, TStrict>}`);
127
+ type PathInternal<T, TraversedTypes = T, TFilterType = unknown, TStrict extends boolean = true> = T extends ReadonlyArray<infer V> ? IsTuple<T> extends true ? {
128
+ [K in TupleKeys<T>]-?: PathImpl<K & string, T[K], TraversedTypes, TFilterType, TStrict>;
129
+ }[TupleKeys<T>] : PathImpl<ArrayKey, V, TraversedTypes, TFilterType, TStrict> : {
130
+ [K in keyof T]-?: PathImpl<K & string, T[K], TraversedTypes, TFilterType, TStrict>;
98
131
  }[keyof T];
99
- type Paths<T, FilterType = unknown, Strict extends boolean = true> = PathInternal<T, T, FilterType, Strict>;
132
+ type Paths<T, TFilterType = unknown, TStrict extends boolean = true> = PathInternal<T, T, TFilterType, TStrict>;
100
133
  /**
101
134
  * Extracts fields common to all variants in a union type.
102
135
  *
@@ -112,15 +145,6 @@ type Paths<T, FilterType = unknown, Strict extends boolean = true> = PathInterna
112
145
  * ```
113
146
  */
114
147
  type CommonFields<T> = Pick<T, keyof T>;
115
- /**
116
- * Recursively unwraps Zod wrapper types to get the core schema type.
117
- * Handles: ZodPipe (transform), ZodOptional, ZodNullable, ZodDefault
118
- */
119
- type UnwrapZodType<T> = T extends z$1.ZodPipe<infer In, SomeType> ? UnwrapZodType<In> : T extends z$1.ZodOptional<infer Inner> ? UnwrapZodType<Inner> : T extends z$1.ZodNullable<infer Inner> ? UnwrapZodType<Inner> : T extends z$1.ZodDefault<infer Inner> ? UnwrapZodType<Inner> : T;
120
- /**
121
- * Checks if the core (unwrapped) type is a ZodDiscriminatedUnion.
122
- */
123
- type IsDiscriminatedUnion<T> = UnwrapZodType<T> extends z$1.ZodDiscriminatedUnion ? true : false;
124
148
  /**
125
149
  * Extracts the input type from a discriminated union variant.
126
150
  *
@@ -170,24 +194,176 @@ type DiscriminatedInput<TSchema extends z$1.ZodType, TDiscriminatorKey extends D
170
194
  * ```
171
195
  */
172
196
  type ValidPaths<TSchema extends z$1.ZodType, TDiscriminatorKey extends DiscriminatorKey<TSchema> = never, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey> = never, TFilterType = unknown, TStrict extends boolean = true> = Paths<DiscriminatedInput<TSchema, TDiscriminatorKey, TDiscriminatorValue>, TFilterType, TStrict>;
173
- type InnerFieldSelector<TSchema extends z$1.ZodType, TPath extends ValidPaths<TSchema, TDiscriminatorKey, TDiscriminatorValue, TFilterType, TStrict>, TDiscriminatorKey extends DiscriminatorKey<TSchema> = never, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey> = never, TFilterType = unknown, TStrict extends boolean = true> = TSchema extends z$1.ZodPipe<infer In> ? In extends z$1.ZodDiscriminatedUnion ? {
174
- schema: TSchema;
175
- name: TPath;
176
- discriminator: Discriminator<TSchema, TDiscriminatorKey, TDiscriminatorValue>;
177
- } : {
178
- schema: TSchema;
179
- name: TPath;
180
- discriminator?: never;
181
- } : TSchema extends z$1.ZodDiscriminatedUnion ? {
182
- schema: TSchema;
183
- name: TPath;
184
- discriminator: Discriminator<TSchema, TDiscriminatorKey, TDiscriminatorValue>;
185
- } : {
197
+ /**
198
+ * Type for props containing a Zod schema.
199
+ *
200
+ * A simple wrapper type useful for factory functions and component props
201
+ * that need to accept a schema parameter.
202
+ *
203
+ * @template TSchema - The Zod schema type
204
+ *
205
+ * @example
206
+ * Factory function accepting schema
207
+ * ```typescript
208
+ * function createFormField<TSchema extends z.ZodType>(
209
+ * props: SchemaProps<TSchema>
210
+ * ) {
211
+ * return (childProps: NameProps<TSchema>) => {
212
+ * // Use props.schema and childProps.name
213
+ * };
214
+ * }
215
+ * ```
216
+ *
217
+ * @example
218
+ * Component props with schema
219
+ * ```typescript
220
+ * type MyComponentProps<TSchema extends z.ZodType> =
221
+ * SchemaProps<TSchema> & { label: string };
222
+ * ```
223
+ */
224
+ type SchemaProps<TSchema extends z$1.ZodType> = {
186
225
  schema: TSchema;
187
- name: TPath;
188
- discriminator?: never;
189
226
  };
190
- type FieldSelector<TSchema extends z$1.ZodType, TPath extends ValidPaths<TSchema, TDiscriminatorKey, TDiscriminatorValue, TFilterType, TStrict>, TDiscriminatorKey extends DiscriminatorKey<TSchema> = never, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey> = never, TFilterType = unknown, TStrict extends boolean = true> = InnerFieldSelector<TSchema, TPath, TDiscriminatorKey, TDiscriminatorValue, TFilterType, TStrict>;
227
+ /**
228
+ * Type for props containing a schema and optional discriminator.
229
+ *
230
+ * Combines SchemaProps and DiscriminatorProps for functions that need
231
+ * schema + discriminator but don't take a field name path.
232
+ *
233
+ * @template TSchema - The Zod schema type
234
+ * @template TDiscriminatorKey - The discriminator key for discriminated unions
235
+ * @template TDiscriminatorValue - The discriminator value for discriminated unions
236
+ *
237
+ * @example
238
+ * Function accepting schema with discriminator
239
+ * ```typescript
240
+ * function getDefaults<
241
+ * TSchema extends z.ZodType,
242
+ * TDiscriminatorKey extends DiscriminatorKey<TSchema>,
243
+ * TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey>,
244
+ * >(
245
+ * params: SchemaAndDiscriminatorProps<TSchema, TDiscriminatorKey, TDiscriminatorValue>
246
+ * ) {
247
+ * // Use params.schema and params.discriminator
248
+ * }
249
+ * ```
250
+ */
251
+ type SchemaAndDiscriminatorProps<TSchema extends z$1.ZodType, TDiscriminatorKey extends DiscriminatorKey<TSchema> = never, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey> = never> = SchemaProps<TSchema> & DiscriminatorProps<TSchema, TDiscriminatorKey, TDiscriminatorValue>;
252
+ /**
253
+ * Type for props containing a field name path.
254
+ *
255
+ * Supports discriminated union narrowing via TDiscriminatorKey/TDiscriminatorValue.
256
+ * The name is constrained to valid paths within the schema.
257
+ *
258
+ * @template TSchema - The Zod schema type
259
+ * @template TPath - The path type (must extend ValidPaths)
260
+ * @template TDiscriminatorKey - The discriminator key for discriminated unions
261
+ * @template TDiscriminatorValue - The discriminator value for discriminated unions
262
+ * @template TFilterType - Optional type filter for paths
263
+ * @template TStrict - Whether to use strict path matching
264
+ *
265
+ * @example
266
+ * Basic usage with object schema
267
+ * ```typescript
268
+ * const schema = z.object({ name: z.string(), age: z.number() });
269
+ *
270
+ * type Props = NameProps<typeof schema, 'name'>;
271
+ * // { name: 'name' }
272
+ * ```
273
+ *
274
+ * @example
275
+ * With discriminated union
276
+ * ```typescript
277
+ * const schema = z.discriminatedUnion('mode', [
278
+ * z.object({ mode: z.literal('create'), name: z.string() }),
279
+ * z.object({ mode: z.literal('edit'), id: z.number() }),
280
+ * ]);
281
+ *
282
+ * type CreateProps = NameProps<typeof schema, 'name', 'mode', 'create'>;
283
+ * // { name: 'name' }
284
+ * ```
285
+ */
286
+ type NameProps<TSchema extends z$1.ZodType, TDiscriminatorKey extends DiscriminatorKey<TSchema> = never, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey> = never, TFilterType = unknown, TStrict extends boolean = true> = {
287
+ name: ValidPaths<TSchema, TDiscriminatorKey, TDiscriminatorValue, TFilterType, TStrict>;
288
+ };
289
+ /**
290
+ * Type for props containing a field name path and discriminator.
291
+ *
292
+ * Combines `NameProps` and `DiscriminatorProps` for functions that need
293
+ * a field path with discriminator support but don't take a schema directly
294
+ * (e.g., factory-returned components where schema is already bound).
295
+ *
296
+ * @template TSchema - The Zod schema type
297
+ * @template TPath - The path type (must extend ValidPaths)
298
+ * @template TDiscriminatorKey - The discriminator key for discriminated unions
299
+ * @template TDiscriminatorValue - The discriminator value for discriminated unions
300
+ * @template TFilterType - Optional type filter for paths
301
+ * @template TStrict - Whether to use strict path matching
302
+ *
303
+ * @example
304
+ * Factory-returned component props
305
+ * ```typescript
306
+ * function createFormField<TSchema extends z.ZodType>(factoryProps: SchemaProps<TSchema>) {
307
+ * return function BoundFormField<
308
+ * TPath extends ValidPaths<TSchema, TDiscriminatorKey, TDiscriminatorValue>,
309
+ * TDiscriminatorKey extends DiscriminatorKey<TSchema> = never,
310
+ * TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey> = never,
311
+ * >(
312
+ * props: NameAndDiscriminatorProps<TSchema, TPath, TDiscriminatorKey, TDiscriminatorValue>
313
+ * ) {
314
+ * // props has { name, discriminator? } - schema comes from factoryProps
315
+ * };
316
+ * }
317
+ * ```
318
+ */
319
+ type NameAndDiscriminatorProps<TSchema extends z$1.ZodType, TDiscriminatorKey extends DiscriminatorKey<TSchema> = never, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey> = never, TFilterType = unknown, TStrict extends boolean = true> = NameProps<TSchema, TDiscriminatorKey, TDiscriminatorValue, TFilterType, TStrict> & DiscriminatorProps<TSchema, TDiscriminatorKey, TDiscriminatorValue>;
320
+ /**
321
+ * Complete field selector type combining schema, name path, and discriminator.
322
+ *
323
+ * This is the main type for selecting fields from a Zod schema with full
324
+ * discriminated union support. Combines:
325
+ * - `SchemaProps<TSchema>` - the schema prop
326
+ * - `NameProps<...>` - the name path prop
327
+ * - `DiscriminatorProps<...>` - conditional discriminator (required for unions, prohibited otherwise)
328
+ *
329
+ * @template TSchema - The Zod schema type
330
+ * @template TDiscriminatorKey - The discriminator key for discriminated unions
331
+ * @template TDiscriminatorValue - The discriminator value for discriminated unions
332
+ * @template TFilterType - Optional type filter for paths
333
+ * @template TStrict - Whether to use strict path matching
334
+ * @template TPath - The path type (must extend ValidPaths)
335
+ *
336
+ * @example
337
+ * Basic usage with object schema
338
+ * ```typescript
339
+ * const schema = z.object({ name: z.string(), age: z.number() });
340
+ *
341
+ * type Props = FieldSelectorProps<typeof schema>;
342
+ * // { schema: typeof schema; name: 'name' | 'age'; discriminator?: never }
343
+ * ```
344
+ *
345
+ * @example
346
+ * With discriminated union
347
+ * ```typescript
348
+ * const schema = z.discriminatedUnion('mode', [
349
+ * z.object({ mode: z.literal('create'), name: z.string() }),
350
+ * z.object({ mode: z.literal('edit'), id: z.number() }),
351
+ * ]);
352
+ *
353
+ * type CreateProps = FieldSelectorProps<typeof schema, 'mode', 'create'>;
354
+ * // { schema: typeof schema; name: 'mode' | 'name'; discriminator: { key: 'mode'; value: 'create' } }
355
+ * ```
356
+ *
357
+ * @example
358
+ * With type filtering
359
+ * ```typescript
360
+ * const schema = z.object({ name: z.string(), age: z.number(), count: z.number() });
361
+ *
362
+ * type NumberFieldProps = FieldSelectorProps<typeof schema, never, never, number>;
363
+ * // { schema: typeof schema; name: 'age' | 'count'; discriminator?: never }
364
+ * ```
365
+ */
366
+ type FieldSelectorProps<TSchema extends z$1.ZodType, TDiscriminatorKey extends DiscriminatorKey<TSchema> = never, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey> = never, TFilterType = unknown, TStrict extends boolean = true> = SchemaProps<TSchema> & NameAndDiscriminatorProps<TSchema, TDiscriminatorKey, TDiscriminatorValue, TFilterType, TStrict>;
191
367
 
192
368
  /**
193
369
  * Extracts the default value from a Zod field, recursively unwrapping optional, nullable, and union layers.
@@ -320,9 +496,7 @@ declare function extractDefaultValue<T extends z.ZodType>(field: T): z.input<T>
320
496
  * @see {@link extractDefaultValue} for extracting defaults from individual fields
321
497
  * @since 0.1.0
322
498
  */
323
- declare function getSchemaDefaults<TSchema extends z.ZodType, TDiscriminatorKey extends DiscriminatorKey<TSchema>, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey>>(schema: TSchema, options?: {
324
- discriminator?: Discriminator<TSchema, TDiscriminatorKey, TDiscriminatorValue>;
325
- }): Simplify<Partial<z.input<TSchema>>>;
499
+ declare function getSchemaDefaults<TSchema extends z.ZodType, TDiscriminatorKey extends DiscriminatorKey<TSchema>, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey>>(params: SchemaAndDiscriminatorProps<TSchema, TDiscriminatorKey, TDiscriminatorValue>): Simplify<Partial<z.input<TSchema>>>;
326
500
 
327
501
  /**
328
502
  * Recursively extracts the exact schema type from a discriminated union based on the discriminator value.
@@ -387,7 +561,7 @@ type ExtractZodUnionMember<TSchema extends z$1.ZodUnion | z$1.ZodDiscriminatedUn
387
561
  * @param params.schema - The discriminated union schema to search
388
562
  * @param params.discriminatorKey - The discriminator field name (e.g., "mode", "type")
389
563
  * @param params.discriminatorValue - The discriminator value to match (e.g., "create", "edit")
390
- * @returns The exact matching schema option (with precise type), or `undefined` if not found
564
+ * @returns The exact matching schema option (with precise type), or `undefined` if not found or schema is not a discriminated union
391
565
  *
392
566
  * @example
393
567
  * Basic discriminated union - create/edit mode
@@ -408,16 +582,14 @@ type ExtractZodUnionMember<TSchema extends z$1.ZodUnion | z$1.ZodDiscriminatedUn
408
582
  * // Extract the "create" schema
409
583
  * const createSchema = extractDiscriminatedSchema({
410
584
  * schema: userSchema,
411
- * discriminatorKey: 'mode',
412
- * discriminatorValue: 'create',
585
+ * discriminator: { key: 'mode', value: 'create' },
413
586
  * });
414
587
  * // Result: z.object({ mode: z.literal('create'), name: z.string(), age: z.number().optional() })
415
588
  *
416
589
  * // Extract the "edit" schema
417
590
  * const editSchema = extractDiscriminatedSchema({
418
591
  * schema: userSchema,
419
- * discriminatorKey: 'mode',
420
- * discriminatorValue: 'edit',
592
+ * discriminator: { key: 'mode', value: 'edit' },
421
593
  * });
422
594
  * // Result: z.object({ mode: z.literal('edit'), id: z.number(), name: z.string().optional() })
423
595
  * ```
@@ -432,8 +604,7 @@ type ExtractZodUnionMember<TSchema extends z$1.ZodUnion | z$1.ZodDiscriminatedUn
432
604
  *
433
605
  * const clickSchema = extractDiscriminatedSchema({
434
606
  * schema: eventSchema,
435
- * discriminatorKey: 'type',
436
- * discriminatorValue: 'click',
607
+ * discriminator: { key: 'type', value: 'click' },
437
608
  * });
438
609
  * // Result: z.object({ type: z.literal('click'), x: z.number(), y: z.number() })
439
610
  * ```
@@ -447,8 +618,7 @@ type ExtractZodUnionMember<TSchema extends z$1.ZodUnion | z$1.ZodDiscriminatedUn
447
618
  *
448
619
  * const result = extractDiscriminatedSchema({
449
620
  * schema,
450
- * discriminatorKey: 'mode',
451
- * discriminatorValue: 'invalid', // doesn't match any option
621
+ * discriminator: { key: 'mode', value: 'invalid' }, // doesn't match any option
452
622
  * });
453
623
  * // Result: undefined
454
624
  * ```
@@ -463,8 +633,7 @@ type ExtractZodUnionMember<TSchema extends z$1.ZodUnion | z$1.ZodDiscriminatedUn
463
633
  *
464
634
  * const createSchema = extractDiscriminatedSchema({
465
635
  * schema,
466
- * discriminatorKey: 'mode',
467
- * discriminatorValue: 'create',
636
+ * discriminator: { key: 'mode', value: 'create' },
468
637
  * });
469
638
  *
470
639
  * // Type is EXACTLY: z.object({ mode: z.literal('create'), name: z.string(), age: z.number() })
@@ -481,9 +650,7 @@ type ExtractZodUnionMember<TSchema extends z$1.ZodUnion | z$1.ZodDiscriminatedUn
481
650
  * @see {@link ExtractZodUnionMember} for the type-level extraction logic
482
651
  * @since 0.6.0
483
652
  */
484
- 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, }: {
485
- schema: TSchema;
486
- } & Discriminator<TSchema, TDiscriminatorKey, TDiscriminatorValue>) => ReturnType;
653
+ 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, discriminator, }: SchemaAndDiscriminatorProps<TSchema, TDiscriminatorKey, TDiscriminatorValue>) => ReturnType | undefined;
487
654
 
488
655
  type Split<S extends string> = S extends `${infer Head}.${infer Tail}` ? [Head, ...Split<Tail>] : [S];
489
656
  type IsNumeric<S extends string> = S extends `${number}` ? true : false;
@@ -493,7 +660,9 @@ type NavigateZod<T, Path extends string[]> = Path extends [
493
660
  ...infer Rest extends string[]
494
661
  ] ? Unwrap<T> extends z$1.ZodObject<infer Shape> ? First extends keyof Shape ? Rest extends [] ? Shape[First] : NavigateZod<Shape[First], Rest> : never : Unwrap<T> extends z$1.ZodArray<infer Element> ? IsNumeric<First> extends true ? Rest extends [] ? Element : NavigateZod<Element, Rest> : never : never : T;
495
662
  type ExtractZodByPath<Schema, Path extends string> = NavigateZod<Schema, Split<Path>>;
496
- declare function extractFieldFromSchema<TSchema extends z$1.ZodType, TPath extends ValidPaths<TSchema, TDiscriminatorKey, TDiscriminatorValue, TFilterType, TStrict>, TDiscriminatorKey extends DiscriminatorKey<TSchema>, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey>, TFilterType = unknown, TStrict extends boolean = true>({ schema, name, discriminator, }: FieldSelector<TSchema, TPath, TDiscriminatorKey, TDiscriminatorValue, TFilterType, TStrict>): (ExtractZodByPath<TSchema, TPath> & z$1.ZodType) | undefined;
663
+ declare function extractFieldFromSchema<TSchema extends z$1.ZodType, TDiscriminatorKey extends DiscriminatorKey<TSchema> = never, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey> = never, TFilterType = unknown, TStrict extends boolean = true, TName extends string = string>(params: FieldSelectorProps<TSchema, TDiscriminatorKey, TDiscriminatorValue, TFilterType, TStrict> & {
664
+ name: TName;
665
+ }): (ExtractZodByPath<TSchema, TName> & z$1.ZodType) | undefined;
497
666
  /**
498
667
  * Extends a Zod field with a transformation while preserving its metadata.
499
668
  *
@@ -514,26 +683,6 @@ declare function extractFieldFromSchema<TSchema extends z$1.ZodType, TPath exten
514
683
  * ```
515
684
  */
516
685
  declare function extendWithMeta<T extends z$1.ZodType, R extends z$1.ZodType>(field: T, transform: (f: T) => R): R;
517
- /**
518
- * Extracts a FieldSelector from props containing schema, name, and optional discriminator.
519
- * Encapsulates type assertion so callers don't need eslint-disable.
520
- *
521
- * @param props - Object containing schema, name, and optional discriminator
522
- * @returns Properly typed FieldSelector
523
- *
524
- * @example
525
- * ```typescript
526
- * const selectorProps = toFieldSelector<TSchema, TPath, ...>(props);
527
- * ```
528
- */
529
- declare function toFieldSelector<TSchema extends z$1.ZodType, TPath extends ValidPaths<TSchema, TDiscriminatorKey, TDiscriminatorValue, TFilterType, TStrict>, TDiscriminatorKey extends DiscriminatorKey<TSchema>, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey>, TFilterType = unknown, TStrict extends boolean = true>(props: {
530
- schema: z$1.ZodType;
531
- name: string;
532
- discriminator?: {
533
- key: string;
534
- value: unknown;
535
- };
536
- }): FieldSelector<TSchema, TPath, TDiscriminatorKey, TDiscriminatorValue, TFilterType, TStrict>;
537
686
 
538
687
  /**
539
688
  * Type representing a Zod type that has an unwrap method
@@ -562,6 +711,15 @@ type Unwrappable = {
562
711
  * @since 0.1.0
563
712
  */
564
713
  declare function canUnwrap(field: z$1.ZodTypeAny): field is z$1.ZodTypeAny & Unwrappable;
714
+ /**
715
+ * Type guard that checks if a Zod field is a ZodPipe with a ZodType input.
716
+ *
717
+ * Used to safely access `field.def.in` on pipe types for unwrapping transformations.
718
+ *
719
+ * @param field - The Zod field to check
720
+ * @returns True if field is a ZodPipe with a ZodType input
721
+ */
722
+ declare function isPipeWithZodInput(field: z$1.ZodTypeAny): field is z$1.ZodPipe<z$1.ZodType, z$1.ZodTypeAny>;
565
723
  /**
566
724
  * Attempts to strip nullish types from a union and return the single remaining type.
567
725
  *
@@ -868,4 +1026,4 @@ type ZodUnionCheck = $ZodCheckLessThanDef | $ZodCheckGreaterThanDef | $ZodCheckM
868
1026
  */
869
1027
  declare function getFieldChecks<T extends z$1.ZodTypeAny>(field: T): Array<ZodUnionCheck>;
870
1028
 
871
- export { type CommonFields, type DiscriminatedInput, type Discriminator, type DiscriminatorKey, type DiscriminatorValue, type ExtractZodByPath, type FieldSelector, type InnerFieldSelector, type IsDiscriminatedUnion, type PathImpl, type PathInternal, type Paths, type Simplify, type UnwrapZodType, type ValidPaths, type ZodUnionCheck, canUnwrap, extendWithMeta, extractDefaultValue, extractDiscriminatedSchema, extractFieldFromSchema, getFieldChecks, getPrimitiveType, getSchemaDefaults, removeDefault, requiresValidInput, toFieldSelector, tryStripNullishOnly };
1029
+ export { type CommonFields, type DiscriminatedInput, type DiscriminatorKey, type DiscriminatorProps, type DiscriminatorValue, type ExtractZodByPath, type FieldSelectorProps, type FileList, type IsDiscriminatedUnion, type NameAndDiscriminatorProps, type NameProps, type PathImpl, type PathInternal, type Paths, type SchemaAndDiscriminatorProps, type SchemaProps, type Simplify, type UnwrapZodType, type ValidPaths, type ZodUnionCheck, canUnwrap, extendWithMeta, extractDefaultValue, extractDiscriminatedSchema, extractFieldFromSchema, getFieldChecks, getPrimitiveType, getSchemaDefaults, isPipeWithZodInput, removeDefault, requiresValidInput, tryStripNullishOnly };
package/dist/index.js CHANGED
@@ -23,6 +23,8 @@ function _interopNamespace(e) {
23
23
  var z3__namespace = /*#__PURE__*/_interopNamespace(z3);
24
24
 
25
25
  var __defProp = Object.defineProperty;
26
+ var __defProps = Object.defineProperties;
27
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
26
28
  var __getOwnPropSymbols = Object.getOwnPropertySymbols;
27
29
  var __hasOwnProp = Object.prototype.hasOwnProperty;
28
30
  var __propIsEnum = Object.prototype.propertyIsEnumerable;
@@ -38,27 +40,13 @@ var __spreadValues = (a, b) => {
38
40
  }
39
41
  return a;
40
42
  };
41
- var extractDiscriminatedSchema = ({
42
- schema,
43
- key,
44
- value
45
- }) => {
46
- if (!(schema instanceof z3.z.ZodDiscriminatedUnion)) {
47
- return void 0;
48
- }
49
- return schema.options.find((option) => {
50
- if (option instanceof z3.z.ZodObject) {
51
- const targetField = option.shape[String(key)];
52
- if (!targetField) return false;
53
- const parseResult = targetField.safeParse(value);
54
- return parseResult.success;
55
- }
56
- return false;
57
- });
58
- };
43
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
59
44
  function canUnwrap(field) {
60
45
  return "unwrap" in field && typeof field.unwrap === "function";
61
46
  }
47
+ function isPipeWithZodInput(field) {
48
+ return field instanceof z3.z.ZodPipe && field.def.in instanceof z3.z.ZodType;
49
+ }
62
50
  function tryStripNullishOnly(field) {
63
51
  if (field instanceof z3.z.ZodUnion) {
64
52
  const unionOptions = [...field.def.options];
@@ -79,6 +67,9 @@ var getPrimitiveType = (field) => {
79
67
  if (canUnwrap(field)) {
80
68
  return getPrimitiveType(field.unwrap());
81
69
  }
70
+ if (field instanceof z3.z.ZodDiscriminatedUnion) {
71
+ return field;
72
+ }
82
73
  if (field instanceof z3.z.ZodUnion) {
83
74
  const unwrapped = tryStripNullishOnly(field);
84
75
  if (unwrapped !== false) {
@@ -86,7 +77,7 @@ var getPrimitiveType = (field) => {
86
77
  }
87
78
  return field;
88
79
  }
89
- if (field instanceof z3.z.ZodPipe && field.def.in instanceof z3.z.ZodType) {
80
+ if (isPipeWithZodInput(field)) {
90
81
  return getPrimitiveType(field.def.in);
91
82
  }
92
83
  return field;
@@ -124,6 +115,27 @@ function getFieldChecks(field) {
124
115
  return ((_a = primitiveType.def.checks) == null ? void 0 : _a.map((check) => check._zod.def)) || [];
125
116
  }
126
117
 
118
+ // src/discriminatedSchema.ts
119
+ var extractDiscriminatedSchema = ({
120
+ schema,
121
+ discriminator
122
+ }) => {
123
+ const primitiveSchema = getPrimitiveType(schema);
124
+ if (!(primitiveSchema instanceof z3.z.ZodDiscriminatedUnion) || !discriminator) {
125
+ return void 0;
126
+ }
127
+ const { key, value } = discriminator;
128
+ return primitiveSchema.options.find((option) => {
129
+ if (option instanceof z3.z.ZodObject) {
130
+ const targetField = option.shape[String(key)];
131
+ if (!targetField) return false;
132
+ const parseResult = targetField.safeParse(value);
133
+ return parseResult.success;
134
+ }
135
+ return false;
136
+ });
137
+ };
138
+
127
139
  // src/defaults.ts
128
140
  function extractDefaultValue(field) {
129
141
  if (field instanceof z3__namespace.ZodDefault) {
@@ -139,22 +151,20 @@ function extractDefaultValue(field) {
139
151
  }
140
152
  return void 0;
141
153
  }
142
- if (field instanceof z3__namespace.ZodPipe && field.def.in instanceof z3__namespace.ZodType) {
154
+ if (isPipeWithZodInput(field)) {
143
155
  return extractDefaultValue(field.def.in);
144
156
  }
145
157
  return void 0;
146
158
  }
147
- function getSchemaDefaults(schema, options) {
148
- const primitiveSchema = getPrimitiveType(schema);
159
+ function getSchemaDefaults(params) {
160
+ const primitiveSchemaParams = __spreadProps(__spreadValues({}, params), {
161
+ schema: getPrimitiveType(params.schema)
162
+ });
149
163
  let targetSchema;
150
- if (primitiveSchema instanceof z3__namespace.ZodDiscriminatedUnion) {
151
- if (options == null ? void 0 : options.discriminator) {
152
- targetSchema = extractDiscriminatedSchema(__spreadValues({
153
- schema: primitiveSchema
154
- }, options.discriminator));
155
- }
156
- } else if (primitiveSchema instanceof z3__namespace.ZodObject) {
157
- targetSchema = primitiveSchema;
164
+ if (primitiveSchemaParams.schema instanceof z3__namespace.ZodDiscriminatedUnion) {
165
+ targetSchema = extractDiscriminatedSchema(primitiveSchemaParams);
166
+ } else if (primitiveSchemaParams.schema instanceof z3__namespace.ZodObject) {
167
+ targetSchema = primitiveSchemaParams.schema;
158
168
  }
159
169
  const defaults = {};
160
170
  if (targetSchema) {
@@ -169,24 +179,20 @@ function getSchemaDefaults(schema, options) {
169
179
  }
170
180
  return defaults;
171
181
  }
172
- function extractFieldFromSchema({
173
- schema,
174
- name,
175
- discriminator
176
- }) {
182
+ function extractFieldFromSchema(params) {
177
183
  let currentSchema;
178
- const primitiveSchema = getPrimitiveType(schema);
179
- if (primitiveSchema instanceof z3.z.ZodDiscriminatedUnion) {
180
- if (discriminator) {
181
- currentSchema = extractDiscriminatedSchema(__spreadValues({
182
- schema: primitiveSchema
183
- }, discriminator));
184
+ const newParams = __spreadProps(__spreadValues({}, params), {
185
+ schema: getPrimitiveType(params.schema)
186
+ });
187
+ if (newParams.schema instanceof z3.z.ZodDiscriminatedUnion) {
188
+ if (newParams.discriminator) {
189
+ currentSchema = extractDiscriminatedSchema(newParams);
184
190
  }
185
- } else if (primitiveSchema instanceof z3.z.ZodObject) {
186
- currentSchema = primitiveSchema;
191
+ } else if (newParams.schema instanceof z3.z.ZodObject) {
192
+ currentSchema = newParams.schema;
187
193
  }
188
194
  if (!currentSchema) return void 0;
189
- const segments = String(name).split(".");
195
+ const segments = String(newParams.name).split(".");
190
196
  for (const segment of segments) {
191
197
  if (!currentSchema) return void 0;
192
198
  const unwrapped = getPrimitiveType(currentSchema);
@@ -212,10 +218,6 @@ function extendWithMeta(field, transform) {
212
218
  }
213
219
  return transformedField.meta(__spreadValues({}, meta));
214
220
  }
215
- function toFieldSelector(props) {
216
- const { schema, name, discriminator } = props;
217
- return { schema, name, discriminator };
218
- }
219
221
 
220
222
  exports.canUnwrap = canUnwrap;
221
223
  exports.extendWithMeta = extendWithMeta;
@@ -225,9 +227,9 @@ exports.extractFieldFromSchema = extractFieldFromSchema;
225
227
  exports.getFieldChecks = getFieldChecks;
226
228
  exports.getPrimitiveType = getPrimitiveType;
227
229
  exports.getSchemaDefaults = getSchemaDefaults;
230
+ exports.isPipeWithZodInput = isPipeWithZodInput;
228
231
  exports.removeDefault = removeDefault;
229
232
  exports.requiresValidInput = requiresValidInput;
230
- exports.toFieldSelector = toFieldSelector;
231
233
  exports.tryStripNullishOnly = tryStripNullishOnly;
232
234
  //# sourceMappingURL=index.js.map
233
235
  //# sourceMappingURL=index.js.map