@zod-utils/core 2.0.0 → 2.0.2
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 +118 -0
- package/dist/index.d.mts +72 -2
- package/dist/index.d.ts +72 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -519,6 +519,124 @@ type Simple = Simplify<Complex>;
|
|
|
519
519
|
|
|
520
520
|
---
|
|
521
521
|
|
|
522
|
+
### `DiscriminatedInput<TSchema, TDiscriminatorKey, TDiscriminatorValue>`
|
|
523
|
+
|
|
524
|
+
Extracts the input type from a discriminated union variant. For discriminated unions, narrows to the variant matching the discriminator value and returns its input type. For regular schemas, returns the full input type.
|
|
525
|
+
|
|
526
|
+
```typescript
|
|
527
|
+
import type { DiscriminatedInput } from "@zod-utils/core";
|
|
528
|
+
import { z } from "zod";
|
|
529
|
+
|
|
530
|
+
const schema = z.discriminatedUnion("mode", [
|
|
531
|
+
z.object({ mode: z.literal("create"), name: z.string() }),
|
|
532
|
+
z.object({ mode: z.literal("edit"), id: z.number() }),
|
|
533
|
+
]);
|
|
534
|
+
|
|
535
|
+
type CreateInput = DiscriminatedInput<typeof schema, "mode", "create">;
|
|
536
|
+
// { mode: 'create'; name: string }
|
|
537
|
+
|
|
538
|
+
type EditInput = DiscriminatedInput<typeof schema, "mode", "edit">;
|
|
539
|
+
// { mode: 'edit'; id: number }
|
|
540
|
+
```
|
|
541
|
+
|
|
542
|
+
---
|
|
543
|
+
|
|
544
|
+
### `ValidPaths<TSchema, TDiscriminatorKey, TDiscriminatorValue>`
|
|
545
|
+
|
|
546
|
+
Generates all valid dot-notation paths for a schema. For discriminated unions, narrows to the specific variant first.
|
|
547
|
+
|
|
548
|
+
```typescript
|
|
549
|
+
import type { ValidPaths } from "@zod-utils/core";
|
|
550
|
+
import { z } from "zod";
|
|
551
|
+
|
|
552
|
+
const schema = z.discriminatedUnion("mode", [
|
|
553
|
+
z.object({ mode: z.literal("create"), name: z.string() }),
|
|
554
|
+
z.object({ mode: z.literal("edit"), id: z.number() }),
|
|
555
|
+
]);
|
|
556
|
+
|
|
557
|
+
type CreatePaths = ValidPaths<typeof schema, "mode", "create">;
|
|
558
|
+
// "mode" | "name"
|
|
559
|
+
|
|
560
|
+
type EditPaths = ValidPaths<typeof schema, "mode", "edit">;
|
|
561
|
+
// "mode" | "id"
|
|
562
|
+
```
|
|
563
|
+
|
|
564
|
+
---
|
|
565
|
+
|
|
566
|
+
### `ValidPathsOfType<TSchema, TValueConstraint, TDiscriminatorKey?, TDiscriminatorValue?>`
|
|
567
|
+
|
|
568
|
+
Extracts field paths from a schema where the field value type matches a constraint. Filters schema keys to only those whose input type (with nullish stripped) extends the given `TValueConstraint`.
|
|
569
|
+
|
|
570
|
+
```typescript
|
|
571
|
+
import type { ValidPathsOfType } from "@zod-utils/core";
|
|
572
|
+
import { z } from "zod";
|
|
573
|
+
|
|
574
|
+
const schema = z.object({
|
|
575
|
+
name: z.string(),
|
|
576
|
+
age: z.number(),
|
|
577
|
+
email: z.string().optional(),
|
|
578
|
+
count: z.number().nullable(),
|
|
579
|
+
active: z.boolean(),
|
|
580
|
+
});
|
|
581
|
+
|
|
582
|
+
// Get all string field paths
|
|
583
|
+
type StringPaths = ValidPathsOfType<typeof schema, string>;
|
|
584
|
+
// "name" | "email"
|
|
585
|
+
|
|
586
|
+
// Get all number field paths
|
|
587
|
+
type NumberPaths = ValidPathsOfType<typeof schema, number>;
|
|
588
|
+
// "age" | "count"
|
|
589
|
+
|
|
590
|
+
// Get all boolean field paths
|
|
591
|
+
type BooleanPaths = ValidPathsOfType<typeof schema, boolean>;
|
|
592
|
+
// "active"
|
|
593
|
+
```
|
|
594
|
+
|
|
595
|
+
**With discriminated unions:**
|
|
596
|
+
|
|
597
|
+
```typescript
|
|
598
|
+
const formSchema = z.discriminatedUnion("mode", [
|
|
599
|
+
z.object({ mode: z.literal("create"), name: z.string(), age: z.number() }),
|
|
600
|
+
z.object({ mode: z.literal("edit"), id: z.number(), title: z.string() }),
|
|
601
|
+
]);
|
|
602
|
+
|
|
603
|
+
// Get number paths for 'edit' variant only
|
|
604
|
+
type EditNumberPaths = ValidPathsOfType<
|
|
605
|
+
typeof formSchema,
|
|
606
|
+
number,
|
|
607
|
+
"mode",
|
|
608
|
+
"edit"
|
|
609
|
+
>;
|
|
610
|
+
// "id"
|
|
611
|
+
|
|
612
|
+
// Get string paths for 'create' variant
|
|
613
|
+
type CreateStringPaths = ValidPathsOfType<
|
|
614
|
+
typeof formSchema,
|
|
615
|
+
string,
|
|
616
|
+
"mode",
|
|
617
|
+
"create"
|
|
618
|
+
>;
|
|
619
|
+
// "name"
|
|
620
|
+
```
|
|
621
|
+
|
|
622
|
+
**Array and object filtering:**
|
|
623
|
+
|
|
624
|
+
```typescript
|
|
625
|
+
const schema = z.object({
|
|
626
|
+
tags: z.array(z.string()),
|
|
627
|
+
scores: z.array(z.number()),
|
|
628
|
+
profile: z.object({ bio: z.string() }),
|
|
629
|
+
});
|
|
630
|
+
|
|
631
|
+
type StringArrayPaths = ValidPathsOfType<typeof schema, string[]>;
|
|
632
|
+
// "tags"
|
|
633
|
+
|
|
634
|
+
type ProfilePaths = ValidPathsOfType<typeof schema, { bio: string }>;
|
|
635
|
+
// "profile"
|
|
636
|
+
```
|
|
637
|
+
|
|
638
|
+
---
|
|
639
|
+
|
|
522
640
|
## License
|
|
523
641
|
|
|
524
642
|
MIT
|
package/dist/index.d.mts
CHANGED
|
@@ -113,6 +113,33 @@ type Paths<T> = PathsHint<T> | PathsLoose<T>;
|
|
|
113
113
|
* ```
|
|
114
114
|
*/
|
|
115
115
|
type CommonFields<T> = Pick<T, keyof T>;
|
|
116
|
+
/**
|
|
117
|
+
* Extracts the input type from a discriminated union variant.
|
|
118
|
+
*
|
|
119
|
+
* For discriminated unions, narrows to the variant matching the discriminator value
|
|
120
|
+
* and returns its input type. For regular schemas, returns the full input type.
|
|
121
|
+
*
|
|
122
|
+
* @template TSchema - The Zod schema type
|
|
123
|
+
* @template TDiscriminatorKey - The discriminator key
|
|
124
|
+
* @template TDiscriminatorValue - The discriminator value to match
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```typescript
|
|
128
|
+
* const schema = z.discriminatedUnion('mode', [
|
|
129
|
+
* z.object({ mode: z.literal('create'), name: z.string() }),
|
|
130
|
+
* z.object({ mode: z.literal('edit'), id: z.number() }),
|
|
131
|
+
* ]);
|
|
132
|
+
*
|
|
133
|
+
* type CreateInput = DiscriminatedInput<typeof schema, 'mode', 'create'>;
|
|
134
|
+
* // { mode: 'create'; name: string }
|
|
135
|
+
*
|
|
136
|
+
* type EditInput = DiscriminatedInput<typeof schema, 'mode', 'edit'>;
|
|
137
|
+
* // { mode: 'edit'; id: number }
|
|
138
|
+
* ```
|
|
139
|
+
*
|
|
140
|
+
* @since 0.5.0
|
|
141
|
+
*/
|
|
142
|
+
type DiscriminatedInput<TSchema extends z$1.ZodType, TDiscriminatorKey extends DiscriminatorKey<TSchema>, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey>> = CommonFields<Extract<Required<z$1.input<TSchema>>, TDiscriminatorKey extends never ? z$1.input<TSchema> : TDiscriminatorValue extends never ? z$1.input<TSchema> : Simplify<Record<TDiscriminatorKey, TDiscriminatorValue>>>>;
|
|
116
143
|
/**
|
|
117
144
|
* Generates valid dot-notation paths for fields in a discriminated union variant.
|
|
118
145
|
*
|
|
@@ -133,7 +160,50 @@ type CommonFields<T> = Pick<T, keyof T>;
|
|
|
133
160
|
* // "mode" | "id"
|
|
134
161
|
* ```
|
|
135
162
|
*/
|
|
136
|
-
type ValidPaths<TSchema extends z$1.ZodType, TDiscriminatorKey extends DiscriminatorKey<TSchema>, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey>> = Paths<
|
|
163
|
+
type ValidPaths<TSchema extends z$1.ZodType, TDiscriminatorKey extends DiscriminatorKey<TSchema>, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey>> = Paths<DiscriminatedInput<TSchema, TDiscriminatorKey, TDiscriminatorValue>>;
|
|
164
|
+
/**
|
|
165
|
+
* Extracts field paths from a schema where the field value type matches a constraint.
|
|
166
|
+
*
|
|
167
|
+
* Filters schema keys to only those whose input type (with nullish stripped via `NonNullable`)
|
|
168
|
+
* extends the given `TValueConstraint`. For discriminated unions, first narrows to the
|
|
169
|
+
* specific variant before filtering.
|
|
170
|
+
*
|
|
171
|
+
* @template TSchema - The Zod schema type
|
|
172
|
+
* @template TValueConstraint - The value type to filter by
|
|
173
|
+
* @template TDiscriminatorKey - The discriminator key (for discriminated unions)
|
|
174
|
+
* @template TDiscriminatorValue - The discriminator value (for discriminated unions)
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* Get all string field paths
|
|
178
|
+
* ```typescript
|
|
179
|
+
* const schema = z.object({
|
|
180
|
+
* name: z.string(),
|
|
181
|
+
* age: z.number(),
|
|
182
|
+
* email: z.string().optional(),
|
|
183
|
+
* });
|
|
184
|
+
*
|
|
185
|
+
* type StringPaths = ValidPathsOfType<typeof schema, string>;
|
|
186
|
+
* // "name" | "email"
|
|
187
|
+
* ```
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* With discriminated union - filter by type within a variant
|
|
191
|
+
* ```typescript
|
|
192
|
+
* const schema = z.discriminatedUnion('mode', [
|
|
193
|
+
* z.object({ mode: z.literal('create'), name: z.string(), age: z.number() }),
|
|
194
|
+
* z.object({ mode: z.literal('edit'), id: z.number(), name: z.string() }),
|
|
195
|
+
* ]);
|
|
196
|
+
*
|
|
197
|
+
* type EditNumberPaths = ValidPathsOfType<typeof schema, number, 'mode', 'edit'>;
|
|
198
|
+
* // "id" - only number fields in 'edit' variant
|
|
199
|
+
* ```
|
|
200
|
+
*
|
|
201
|
+
* @see {@link ValidPaths} for discriminated union path filtering
|
|
202
|
+
* @since 0.5.0
|
|
203
|
+
*/
|
|
204
|
+
type ValidPathsOfType<TSchema extends z$1.ZodType, TValueConstraint, TDiscriminatorKey extends DiscriminatorKey<TSchema> = DiscriminatorKey<TSchema>, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey> = DiscriminatorValue<TSchema, TDiscriminatorKey>, TVariant = DiscriminatedInput<TSchema, TDiscriminatorKey, TDiscriminatorValue>> = NonNullable<Extract<{
|
|
205
|
+
[K in keyof TVariant]: NonNullable<TVariant[K]> extends TValueConstraint ? K : never;
|
|
206
|
+
}[keyof TVariant], string> & ValidPaths<TSchema, TDiscriminatorKey, TDiscriminatorValue>>;
|
|
137
207
|
|
|
138
208
|
/**
|
|
139
209
|
* Extracts the default value from a Zod field, recursively unwrapping optional, nullable, and union layers.
|
|
@@ -798,4 +868,4 @@ type ZodUnionCheck = $ZodCheckLessThanDef | $ZodCheckGreaterThanDef | $ZodCheckM
|
|
|
798
868
|
*/
|
|
799
869
|
declare function getFieldChecks<T extends z$1.ZodTypeAny>(field: T): Array<ZodUnionCheck>;
|
|
800
870
|
|
|
801
|
-
export { type CommonFields, type Discriminator, type DiscriminatorKey, type DiscriminatorValue, type ExtractZodByPath, type Paths, type Simplify, type ValidPaths, type ZodUnionCheck, canUnwrap, extendWithMeta, extractDefaultValue, extractDiscriminatedSchema, extractFieldFromSchema, getFieldChecks, getPrimitiveType, getSchemaDefaults, removeDefault, requiresValidInput, tryStripNullishOnly };
|
|
871
|
+
export { type CommonFields, type DiscriminatedInput, type Discriminator, type DiscriminatorKey, type DiscriminatorValue, type ExtractZodByPath, type Paths, type Simplify, type ValidPaths, type ValidPathsOfType, type ZodUnionCheck, canUnwrap, extendWithMeta, extractDefaultValue, extractDiscriminatedSchema, extractFieldFromSchema, getFieldChecks, getPrimitiveType, getSchemaDefaults, removeDefault, requiresValidInput, tryStripNullishOnly };
|
package/dist/index.d.ts
CHANGED
|
@@ -113,6 +113,33 @@ type Paths<T> = PathsHint<T> | PathsLoose<T>;
|
|
|
113
113
|
* ```
|
|
114
114
|
*/
|
|
115
115
|
type CommonFields<T> = Pick<T, keyof T>;
|
|
116
|
+
/**
|
|
117
|
+
* Extracts the input type from a discriminated union variant.
|
|
118
|
+
*
|
|
119
|
+
* For discriminated unions, narrows to the variant matching the discriminator value
|
|
120
|
+
* and returns its input type. For regular schemas, returns the full input type.
|
|
121
|
+
*
|
|
122
|
+
* @template TSchema - The Zod schema type
|
|
123
|
+
* @template TDiscriminatorKey - The discriminator key
|
|
124
|
+
* @template TDiscriminatorValue - The discriminator value to match
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```typescript
|
|
128
|
+
* const schema = z.discriminatedUnion('mode', [
|
|
129
|
+
* z.object({ mode: z.literal('create'), name: z.string() }),
|
|
130
|
+
* z.object({ mode: z.literal('edit'), id: z.number() }),
|
|
131
|
+
* ]);
|
|
132
|
+
*
|
|
133
|
+
* type CreateInput = DiscriminatedInput<typeof schema, 'mode', 'create'>;
|
|
134
|
+
* // { mode: 'create'; name: string }
|
|
135
|
+
*
|
|
136
|
+
* type EditInput = DiscriminatedInput<typeof schema, 'mode', 'edit'>;
|
|
137
|
+
* // { mode: 'edit'; id: number }
|
|
138
|
+
* ```
|
|
139
|
+
*
|
|
140
|
+
* @since 0.5.0
|
|
141
|
+
*/
|
|
142
|
+
type DiscriminatedInput<TSchema extends z$1.ZodType, TDiscriminatorKey extends DiscriminatorKey<TSchema>, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey>> = CommonFields<Extract<Required<z$1.input<TSchema>>, TDiscriminatorKey extends never ? z$1.input<TSchema> : TDiscriminatorValue extends never ? z$1.input<TSchema> : Simplify<Record<TDiscriminatorKey, TDiscriminatorValue>>>>;
|
|
116
143
|
/**
|
|
117
144
|
* Generates valid dot-notation paths for fields in a discriminated union variant.
|
|
118
145
|
*
|
|
@@ -133,7 +160,50 @@ type CommonFields<T> = Pick<T, keyof T>;
|
|
|
133
160
|
* // "mode" | "id"
|
|
134
161
|
* ```
|
|
135
162
|
*/
|
|
136
|
-
type ValidPaths<TSchema extends z$1.ZodType, TDiscriminatorKey extends DiscriminatorKey<TSchema>, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey>> = Paths<
|
|
163
|
+
type ValidPaths<TSchema extends z$1.ZodType, TDiscriminatorKey extends DiscriminatorKey<TSchema>, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey>> = Paths<DiscriminatedInput<TSchema, TDiscriminatorKey, TDiscriminatorValue>>;
|
|
164
|
+
/**
|
|
165
|
+
* Extracts field paths from a schema where the field value type matches a constraint.
|
|
166
|
+
*
|
|
167
|
+
* Filters schema keys to only those whose input type (with nullish stripped via `NonNullable`)
|
|
168
|
+
* extends the given `TValueConstraint`. For discriminated unions, first narrows to the
|
|
169
|
+
* specific variant before filtering.
|
|
170
|
+
*
|
|
171
|
+
* @template TSchema - The Zod schema type
|
|
172
|
+
* @template TValueConstraint - The value type to filter by
|
|
173
|
+
* @template TDiscriminatorKey - The discriminator key (for discriminated unions)
|
|
174
|
+
* @template TDiscriminatorValue - The discriminator value (for discriminated unions)
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* Get all string field paths
|
|
178
|
+
* ```typescript
|
|
179
|
+
* const schema = z.object({
|
|
180
|
+
* name: z.string(),
|
|
181
|
+
* age: z.number(),
|
|
182
|
+
* email: z.string().optional(),
|
|
183
|
+
* });
|
|
184
|
+
*
|
|
185
|
+
* type StringPaths = ValidPathsOfType<typeof schema, string>;
|
|
186
|
+
* // "name" | "email"
|
|
187
|
+
* ```
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* With discriminated union - filter by type within a variant
|
|
191
|
+
* ```typescript
|
|
192
|
+
* const schema = z.discriminatedUnion('mode', [
|
|
193
|
+
* z.object({ mode: z.literal('create'), name: z.string(), age: z.number() }),
|
|
194
|
+
* z.object({ mode: z.literal('edit'), id: z.number(), name: z.string() }),
|
|
195
|
+
* ]);
|
|
196
|
+
*
|
|
197
|
+
* type EditNumberPaths = ValidPathsOfType<typeof schema, number, 'mode', 'edit'>;
|
|
198
|
+
* // "id" - only number fields in 'edit' variant
|
|
199
|
+
* ```
|
|
200
|
+
*
|
|
201
|
+
* @see {@link ValidPaths} for discriminated union path filtering
|
|
202
|
+
* @since 0.5.0
|
|
203
|
+
*/
|
|
204
|
+
type ValidPathsOfType<TSchema extends z$1.ZodType, TValueConstraint, TDiscriminatorKey extends DiscriminatorKey<TSchema> = DiscriminatorKey<TSchema>, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey> = DiscriminatorValue<TSchema, TDiscriminatorKey>, TVariant = DiscriminatedInput<TSchema, TDiscriminatorKey, TDiscriminatorValue>> = NonNullable<Extract<{
|
|
205
|
+
[K in keyof TVariant]: NonNullable<TVariant[K]> extends TValueConstraint ? K : never;
|
|
206
|
+
}[keyof TVariant], string> & ValidPaths<TSchema, TDiscriminatorKey, TDiscriminatorValue>>;
|
|
137
207
|
|
|
138
208
|
/**
|
|
139
209
|
* Extracts the default value from a Zod field, recursively unwrapping optional, nullable, and union layers.
|
|
@@ -798,4 +868,4 @@ type ZodUnionCheck = $ZodCheckLessThanDef | $ZodCheckGreaterThanDef | $ZodCheckM
|
|
|
798
868
|
*/
|
|
799
869
|
declare function getFieldChecks<T extends z$1.ZodTypeAny>(field: T): Array<ZodUnionCheck>;
|
|
800
870
|
|
|
801
|
-
export { type CommonFields, type Discriminator, type DiscriminatorKey, type DiscriminatorValue, type ExtractZodByPath, type Paths, type Simplify, type ValidPaths, type ZodUnionCheck, canUnwrap, extendWithMeta, extractDefaultValue, extractDiscriminatedSchema, extractFieldFromSchema, getFieldChecks, getPrimitiveType, getSchemaDefaults, removeDefault, requiresValidInput, tryStripNullishOnly };
|
|
871
|
+
export { type CommonFields, type DiscriminatedInput, type Discriminator, type DiscriminatorKey, type DiscriminatorValue, type ExtractZodByPath, type Paths, type Simplify, type ValidPaths, type ValidPathsOfType, type ZodUnionCheck, canUnwrap, extendWithMeta, extractDefaultValue, extractDiscriminatedSchema, extractFieldFromSchema, getFieldChecks, getPrimitiveType, getSchemaDefaults, removeDefault, requiresValidInput, tryStripNullishOnly };
|