@zod-utils/core 7.1.0 → 7.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 +144 -0
- package/dist/index.d.mts +115 -1
- package/dist/index.d.ts +115 -1
- package/dist/index.js +93 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +90 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -23,6 +23,8 @@ npm install @zod-utils/core zod
|
|
|
23
23
|
## Features
|
|
24
24
|
|
|
25
25
|
- **Extract defaults** - Get default values from Zod schemas
|
|
26
|
+
- **Extract meta** - Get `.meta()` property values from schema fields
|
|
27
|
+
- **Merge defaults with meta** - Combine defaults and meta into a single result
|
|
26
28
|
- **Check validation requirements** - Determine if fields will error on empty input
|
|
27
29
|
- **Extract validation checks** - Get all validation constraints (min/max, formats, patterns, etc.)
|
|
28
30
|
- **Schema utilities** - Unwrap and manipulate schema types
|
|
@@ -83,6 +85,9 @@ npm install @zod-utils/core zod
|
|
|
83
85
|
- [With Transforms](#with-transforms-2)
|
|
84
86
|
- [`extendWithMeta(field, transform)`](#extendwithmetafield-transform)
|
|
85
87
|
- [Use Case: Shared Field Definitions with i18n](#use-case-shared-field-definitions-with-i18n)
|
|
88
|
+
- [`extractMeta(field, metaKey)`](#extractmetafield-metakey)
|
|
89
|
+
- [`getSchemaMeta(params, metaKey)`](#getschemametaparams-metakey)
|
|
90
|
+
- [`getMergedSchemaDefaults(params, metaKey)`](#getmergedschemadefaultsparams-metakey)
|
|
86
91
|
- [`toFieldSelector(props)`](#tofieldselectorprops)
|
|
87
92
|
- [Type Utilities](#type-utilities)
|
|
88
93
|
- [`Simplify<T>`](#simplifyt)
|
|
@@ -1004,6 +1009,145 @@ const editFormSchema = z.object({
|
|
|
1004
1009
|
|
|
1005
1010
|
---
|
|
1006
1011
|
|
|
1012
|
+
### `extractMeta(field, metaKey)`
|
|
1013
|
+
|
|
1014
|
+
Extract the value of a specific meta key from a Zod field. Recursively unwraps optional, nullable, default, transform, and union layers.
|
|
1015
|
+
|
|
1016
|
+
For `ZodObject` fields: if the object's own meta contains the target key, returns that value directly. Otherwise, recurses into shape children and returns a nested record.
|
|
1017
|
+
|
|
1018
|
+
```typescript
|
|
1019
|
+
import { extractMeta } from "@zod-utils/core";
|
|
1020
|
+
import { z } from "zod";
|
|
1021
|
+
|
|
1022
|
+
// Basic extraction
|
|
1023
|
+
const field = z.string().meta({ label: "Name" });
|
|
1024
|
+
extractMeta(field, "label"); // 'Name'
|
|
1025
|
+
|
|
1026
|
+
// Unwraps through optional/nullable/default
|
|
1027
|
+
extractMeta(z.string().meta({ label: "Name" }).optional(), "label"); // 'Name'
|
|
1028
|
+
extractMeta(z.string().meta({ label: "Name" }).nullable(), "label"); // 'Name'
|
|
1029
|
+
extractMeta(z.string().meta({ label: "Name" }).default("hi"), "label"); // 'Name'
|
|
1030
|
+
|
|
1031
|
+
// Nested objects: recurses when parent lacks the key
|
|
1032
|
+
const address = z.object({
|
|
1033
|
+
city: z.string().meta({ label: "City" }),
|
|
1034
|
+
zip: z.string().meta({ label: "Zip" }),
|
|
1035
|
+
});
|
|
1036
|
+
extractMeta(address, "label"); // { city: 'City', zip: 'Zip' }
|
|
1037
|
+
|
|
1038
|
+
// Object's own meta stops recursion
|
|
1039
|
+
const labeled = address.meta({ label: "Address" });
|
|
1040
|
+
extractMeta(labeled, "label"); // 'Address'
|
|
1041
|
+
|
|
1042
|
+
// Arrays: wraps element meta in an array
|
|
1043
|
+
extractMeta(z.array(z.string().meta({ label: "Tag" })), "label"); // ['Tag']
|
|
1044
|
+
|
|
1045
|
+
// No meta returns undefined
|
|
1046
|
+
extractMeta(z.string(), "label"); // undefined
|
|
1047
|
+
```
|
|
1048
|
+
|
|
1049
|
+
---
|
|
1050
|
+
|
|
1051
|
+
### `getSchemaMeta(params, metaKey)`
|
|
1052
|
+
|
|
1053
|
+
Extract the value of a specific meta key from all fields in a Zod schema. Supports discriminated unions via the `discriminator` parameter.
|
|
1054
|
+
|
|
1055
|
+
```typescript
|
|
1056
|
+
import { getSchemaMeta } from "@zod-utils/core";
|
|
1057
|
+
import { z } from "zod";
|
|
1058
|
+
|
|
1059
|
+
const schema = z.object({
|
|
1060
|
+
name: z.string().meta({ label: "Name" }),
|
|
1061
|
+
age: z.number().meta({ label: "Age" }),
|
|
1062
|
+
bio: z.string(), // no meta
|
|
1063
|
+
});
|
|
1064
|
+
|
|
1065
|
+
getSchemaMeta({ schema }, "label");
|
|
1066
|
+
// { name: 'Name', age: 'Age' }
|
|
1067
|
+
|
|
1068
|
+
// With discriminated union
|
|
1069
|
+
const formSchema = z.discriminatedUnion("mode", [
|
|
1070
|
+
z.object({
|
|
1071
|
+
mode: z.literal("create"),
|
|
1072
|
+
name: z.string().meta({ label: "Name" }),
|
|
1073
|
+
}),
|
|
1074
|
+
z.object({
|
|
1075
|
+
mode: z.literal("edit"),
|
|
1076
|
+
id: z.number().meta({ label: "ID" }),
|
|
1077
|
+
}),
|
|
1078
|
+
]);
|
|
1079
|
+
|
|
1080
|
+
getSchemaMeta(
|
|
1081
|
+
{ schema: formSchema, discriminator: { key: "mode", value: "create" } },
|
|
1082
|
+
"label"
|
|
1083
|
+
);
|
|
1084
|
+
// { name: 'Name' }
|
|
1085
|
+
|
|
1086
|
+
// With transforms
|
|
1087
|
+
const transformed = z
|
|
1088
|
+
.object({ name: z.string().meta({ label: "Name" }) })
|
|
1089
|
+
.transform((data) => ({ ...data, computed: true }));
|
|
1090
|
+
|
|
1091
|
+
getSchemaMeta({ schema: transformed }, "label");
|
|
1092
|
+
// { name: 'Name' }
|
|
1093
|
+
```
|
|
1094
|
+
|
|
1095
|
+
---
|
|
1096
|
+
|
|
1097
|
+
### `getMergedSchemaDefaults(params, metaKey)`
|
|
1098
|
+
|
|
1099
|
+
Combines schema defaults and meta values into a single result. Meta values take precedence over defaults where both exist. Performs a deep merge for nested objects.
|
|
1100
|
+
|
|
1101
|
+
```typescript
|
|
1102
|
+
import { getMergedSchemaDefaults } from "@zod-utils/core";
|
|
1103
|
+
import { z } from "zod";
|
|
1104
|
+
|
|
1105
|
+
const schema = z.object({
|
|
1106
|
+
name: z.string().meta({ label: "Name" }).default("hello"),
|
|
1107
|
+
age: z.number().meta({ label: "Age" }),
|
|
1108
|
+
bio: z.string().default(""),
|
|
1109
|
+
});
|
|
1110
|
+
|
|
1111
|
+
getMergedSchemaDefaults({ schema }, "label");
|
|
1112
|
+
// { name: 'Name', age: 'Age', bio: '' }
|
|
1113
|
+
// 'name': meta 'Name' wins over default 'hello'
|
|
1114
|
+
// 'age': only meta exists
|
|
1115
|
+
// 'bio': only default exists
|
|
1116
|
+
|
|
1117
|
+
// Deep merge preserves nested fields from both sources
|
|
1118
|
+
const nested = z.object({
|
|
1119
|
+
address: z.object({
|
|
1120
|
+
city: z.string().meta({ label: "City" }).default("NYC"),
|
|
1121
|
+
zip: z.string().default("00000"),
|
|
1122
|
+
state: z.string().meta({ label: "State" }),
|
|
1123
|
+
}),
|
|
1124
|
+
});
|
|
1125
|
+
|
|
1126
|
+
getMergedSchemaDefaults({ schema: nested }, "label");
|
|
1127
|
+
// { address: { city: 'City', zip: '00000', state: 'State' } }
|
|
1128
|
+
|
|
1129
|
+
// Works with discriminated unions
|
|
1130
|
+
const formSchema = z.discriminatedUnion("mode", [
|
|
1131
|
+
z.object({
|
|
1132
|
+
mode: z.literal("create"),
|
|
1133
|
+
name: z.string().meta({ label: "Name" }).default("New User"),
|
|
1134
|
+
count: z.number().default(0),
|
|
1135
|
+
}),
|
|
1136
|
+
z.object({
|
|
1137
|
+
mode: z.literal("edit"),
|
|
1138
|
+
id: z.number().meta({ label: "ID" }),
|
|
1139
|
+
}),
|
|
1140
|
+
]);
|
|
1141
|
+
|
|
1142
|
+
getMergedSchemaDefaults(
|
|
1143
|
+
{ schema: formSchema, discriminator: { key: "mode", value: "create" } },
|
|
1144
|
+
"label"
|
|
1145
|
+
);
|
|
1146
|
+
// { name: 'Name', count: 0 }
|
|
1147
|
+
```
|
|
1148
|
+
|
|
1149
|
+
---
|
|
1150
|
+
|
|
1007
1151
|
### `toFieldSelector(props)`
|
|
1008
1152
|
|
|
1009
1153
|
Extracts a `FieldSelector` from props containing schema, name, and optional discriminator. Encapsulates type assertion so callers don't need eslint-disable.
|
package/dist/index.d.mts
CHANGED
|
@@ -365,6 +365,11 @@ type NameAndDiscriminatorProps<TSchema extends z$1.ZodType, TDiscriminatorKey ex
|
|
|
365
365
|
*/
|
|
366
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>;
|
|
367
367
|
|
|
368
|
+
/**
|
|
369
|
+
* Type-safe helper to iterate over ZodObject shape entries.
|
|
370
|
+
* @internal
|
|
371
|
+
*/
|
|
372
|
+
declare function getShapeEntries(schema: z.ZodObject): Array<[string, z.ZodType | undefined]>;
|
|
368
373
|
/**
|
|
369
374
|
* Extracts the default value from a Zod field, recursively unwrapping optional, nullable, and union layers.
|
|
370
375
|
*
|
|
@@ -710,6 +715,115 @@ declare function extractFieldFromSchema<TSchema extends z$1.ZodType, TDiscrimina
|
|
|
710
715
|
*/
|
|
711
716
|
declare function extendWithMeta<T extends z$1.ZodType, R extends z$1.ZodType>(field: T, transform: (f: T) => R): R;
|
|
712
717
|
|
|
718
|
+
/**
|
|
719
|
+
* Extracts the value of a specific meta key from a Zod field, recursively unwrapping
|
|
720
|
+
* optional, nullable, default, transform, and union layers.
|
|
721
|
+
*
|
|
722
|
+
* For `ZodObject` fields: if the object's own meta contains the target key, returns that
|
|
723
|
+
* value directly (stops recursion). If not, recurses into shape children and returns
|
|
724
|
+
* a nested record of meta values.
|
|
725
|
+
*
|
|
726
|
+
* @param field - The Zod field to extract meta from
|
|
727
|
+
* @param metaKey - The meta property key to extract
|
|
728
|
+
* @returns The meta value if found, undefined otherwise
|
|
729
|
+
*
|
|
730
|
+
* @example
|
|
731
|
+
* ```typescript
|
|
732
|
+
* const field = z.string().meta({ label: 'Name' });
|
|
733
|
+
* extractMeta(field, 'label'); // 'Name'
|
|
734
|
+
* ```
|
|
735
|
+
*
|
|
736
|
+
* @example
|
|
737
|
+
* ```typescript
|
|
738
|
+
* const field = z.string().meta({ label: 'Name' }).optional();
|
|
739
|
+
* extractMeta(field, 'label'); // 'Name' (unwraps optional)
|
|
740
|
+
* ```
|
|
741
|
+
*
|
|
742
|
+
* @see {@link getSchemaMeta} for extracting meta from entire schemas
|
|
743
|
+
* @since 0.3.0
|
|
744
|
+
*/
|
|
745
|
+
declare function extractMeta(field: z.ZodType, metaKey: string): unknown;
|
|
746
|
+
/**
|
|
747
|
+
* Extracts the value of a specific meta key from all fields in a Zod schema.
|
|
748
|
+
*
|
|
749
|
+
* Returns a nested record where each key maps to the meta value for that field.
|
|
750
|
+
* Fields without the specified meta key are omitted.
|
|
751
|
+
*
|
|
752
|
+
* For `ZodObject` fields: if the object has the target meta key in its own meta,
|
|
753
|
+
* returns that value directly. Otherwise, recurses into shape children.
|
|
754
|
+
*
|
|
755
|
+
* Supports discriminated unions via the `discriminator` parameter.
|
|
756
|
+
*
|
|
757
|
+
* @param params - Schema and optional discriminator configuration
|
|
758
|
+
* @param metaKey - The meta property key to extract
|
|
759
|
+
* @returns Record mapping field names to their meta values
|
|
760
|
+
*
|
|
761
|
+
* To get typed meta keys with autocomplete, augment Zod's `GlobalMeta` interface:
|
|
762
|
+
*
|
|
763
|
+
* ```typescript
|
|
764
|
+
* // types/zod.ts
|
|
765
|
+
* import * as z from 'zod';
|
|
766
|
+
*
|
|
767
|
+
* declare module 'zod' {
|
|
768
|
+
* interface GlobalMeta {
|
|
769
|
+
* label: string; // static type for all schemas
|
|
770
|
+
* type: z.$output; // resolves to the schema's output type
|
|
771
|
+
* inputType: z.$input; // resolves to the schema's input type
|
|
772
|
+
* placeholder?: string;
|
|
773
|
+
* }
|
|
774
|
+
* }
|
|
775
|
+
* ```
|
|
776
|
+
*
|
|
777
|
+
* With `z.$output` / `z.$input`, Zod automatically types `.meta()` values
|
|
778
|
+
* based on each schema's inferred type:
|
|
779
|
+
*
|
|
780
|
+
* ```typescript
|
|
781
|
+
* z.string().meta({ type: 'hello' }) // ✅ type is string
|
|
782
|
+
* z.string().meta({ type: 42 }) // ❌ TypeScript error — expects string
|
|
783
|
+
* z.number().meta({ type: 42 }) // ✅ type is number
|
|
784
|
+
* ```
|
|
785
|
+
*
|
|
786
|
+
* @example
|
|
787
|
+
* ```typescript
|
|
788
|
+
* const schema = z.object({
|
|
789
|
+
* name: z.string().meta({ label: 'Name' }),
|
|
790
|
+
* age: z.number().meta({ label: 'Age' }),
|
|
791
|
+
* });
|
|
792
|
+
* getSchemaMeta({ schema }, 'label');
|
|
793
|
+
* // { name: 'Name', age: 'Age' }
|
|
794
|
+
* ```
|
|
795
|
+
*
|
|
796
|
+
* @see {@link extractMeta} for extracting meta from individual fields
|
|
797
|
+
* @since 0.3.0
|
|
798
|
+
*/
|
|
799
|
+
declare function getSchemaMeta<TSchema extends z.ZodType, TDiscriminatorKey extends DiscriminatorKey<TSchema>, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey>>(params: SchemaAndDiscriminatorProps<TSchema, TDiscriminatorKey, TDiscriminatorValue>, metaKey: string): Simplify<Partial<z.input<TSchema>>>;
|
|
800
|
+
/**
|
|
801
|
+
* Combines schema defaults and meta values into a single result.
|
|
802
|
+
* Meta values take precedence over defaults where both exist.
|
|
803
|
+
* For nested objects, performs a deep merge so that fields from both
|
|
804
|
+
* sources are preserved at every level.
|
|
805
|
+
*
|
|
806
|
+
* @param params - Schema and optional discriminator configuration
|
|
807
|
+
* @param metaKey - The meta property key to extract
|
|
808
|
+
* @returns Deep-merged record of defaults and meta values (meta wins on conflict)
|
|
809
|
+
*
|
|
810
|
+
* @example
|
|
811
|
+
* ```typescript
|
|
812
|
+
* const schema = z.object({
|
|
813
|
+
* name: z.string().meta({ label: 'Name' }).default('hello'),
|
|
814
|
+
* age: z.number().meta({ label: 'Age' }),
|
|
815
|
+
* bio: z.string().default(''),
|
|
816
|
+
* });
|
|
817
|
+
* getMergedSchemaDefaults({ schema }, 'label');
|
|
818
|
+
* // { name: 'Name', age: 'Age', bio: '' }
|
|
819
|
+
* ```
|
|
820
|
+
*
|
|
821
|
+
* @see {@link getSchemaDefaults} for extracting only defaults
|
|
822
|
+
* @see {@link getSchemaMeta} for extracting only meta
|
|
823
|
+
* @since 0.5.0
|
|
824
|
+
*/
|
|
825
|
+
declare function getMergedSchemaDefaults<TSchema extends z.ZodType, TDiscriminatorKey extends DiscriminatorKey<TSchema>, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey>>(params: SchemaAndDiscriminatorProps<TSchema, TDiscriminatorKey, TDiscriminatorValue>, metaKey: string): Simplify<Partial<z.input<TSchema>>>;
|
|
826
|
+
|
|
713
827
|
/**
|
|
714
828
|
* Type representing a Zod type that has an unwrap method
|
|
715
829
|
*/
|
|
@@ -1053,4 +1167,4 @@ type ZodUnionCheck = $ZodCheckLessThanDef | $ZodCheckGreaterThanDef | $ZodCheckM
|
|
|
1053
1167
|
declare function getFieldChecks<T extends z$1.ZodType | $ZodType>(field: T): Array<ZodUnionCheck>;
|
|
1054
1168
|
declare function extractCheck(check: z$1.core.$ZodCheck<never>): ZodUnionCheck[];
|
|
1055
1169
|
|
|
1056
|
-
export { type CommonFields, type DiscriminatedInput, type DiscriminatorKey, type DiscriminatorProps, type DiscriminatorValue, type ExtractZodByPath, type ExtractZodUnionMember, 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, extractCheck, extractDefaultValue, extractDiscriminatedSchema, extractFieldFromSchema, getFieldChecks, getPrimitiveType, getSchemaDefaults, isPipeWithZodInput, removeDefault, requiresValidInput, tryStripNullishOnly };
|
|
1170
|
+
export { type CommonFields, type DiscriminatedInput, type DiscriminatorKey, type DiscriminatorProps, type DiscriminatorValue, type ExtractZodByPath, type ExtractZodUnionMember, 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, extractCheck, extractDefaultValue, extractDiscriminatedSchema, extractFieldFromSchema, extractMeta, getFieldChecks, getMergedSchemaDefaults, getPrimitiveType, getSchemaDefaults, getSchemaMeta, getShapeEntries, isPipeWithZodInput, removeDefault, requiresValidInput, tryStripNullishOnly };
|
package/dist/index.d.ts
CHANGED
|
@@ -365,6 +365,11 @@ type NameAndDiscriminatorProps<TSchema extends z$1.ZodType, TDiscriminatorKey ex
|
|
|
365
365
|
*/
|
|
366
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>;
|
|
367
367
|
|
|
368
|
+
/**
|
|
369
|
+
* Type-safe helper to iterate over ZodObject shape entries.
|
|
370
|
+
* @internal
|
|
371
|
+
*/
|
|
372
|
+
declare function getShapeEntries(schema: z.ZodObject): Array<[string, z.ZodType | undefined]>;
|
|
368
373
|
/**
|
|
369
374
|
* Extracts the default value from a Zod field, recursively unwrapping optional, nullable, and union layers.
|
|
370
375
|
*
|
|
@@ -710,6 +715,115 @@ declare function extractFieldFromSchema<TSchema extends z$1.ZodType, TDiscrimina
|
|
|
710
715
|
*/
|
|
711
716
|
declare function extendWithMeta<T extends z$1.ZodType, R extends z$1.ZodType>(field: T, transform: (f: T) => R): R;
|
|
712
717
|
|
|
718
|
+
/**
|
|
719
|
+
* Extracts the value of a specific meta key from a Zod field, recursively unwrapping
|
|
720
|
+
* optional, nullable, default, transform, and union layers.
|
|
721
|
+
*
|
|
722
|
+
* For `ZodObject` fields: if the object's own meta contains the target key, returns that
|
|
723
|
+
* value directly (stops recursion). If not, recurses into shape children and returns
|
|
724
|
+
* a nested record of meta values.
|
|
725
|
+
*
|
|
726
|
+
* @param field - The Zod field to extract meta from
|
|
727
|
+
* @param metaKey - The meta property key to extract
|
|
728
|
+
* @returns The meta value if found, undefined otherwise
|
|
729
|
+
*
|
|
730
|
+
* @example
|
|
731
|
+
* ```typescript
|
|
732
|
+
* const field = z.string().meta({ label: 'Name' });
|
|
733
|
+
* extractMeta(field, 'label'); // 'Name'
|
|
734
|
+
* ```
|
|
735
|
+
*
|
|
736
|
+
* @example
|
|
737
|
+
* ```typescript
|
|
738
|
+
* const field = z.string().meta({ label: 'Name' }).optional();
|
|
739
|
+
* extractMeta(field, 'label'); // 'Name' (unwraps optional)
|
|
740
|
+
* ```
|
|
741
|
+
*
|
|
742
|
+
* @see {@link getSchemaMeta} for extracting meta from entire schemas
|
|
743
|
+
* @since 0.3.0
|
|
744
|
+
*/
|
|
745
|
+
declare function extractMeta(field: z.ZodType, metaKey: string): unknown;
|
|
746
|
+
/**
|
|
747
|
+
* Extracts the value of a specific meta key from all fields in a Zod schema.
|
|
748
|
+
*
|
|
749
|
+
* Returns a nested record where each key maps to the meta value for that field.
|
|
750
|
+
* Fields without the specified meta key are omitted.
|
|
751
|
+
*
|
|
752
|
+
* For `ZodObject` fields: if the object has the target meta key in its own meta,
|
|
753
|
+
* returns that value directly. Otherwise, recurses into shape children.
|
|
754
|
+
*
|
|
755
|
+
* Supports discriminated unions via the `discriminator` parameter.
|
|
756
|
+
*
|
|
757
|
+
* @param params - Schema and optional discriminator configuration
|
|
758
|
+
* @param metaKey - The meta property key to extract
|
|
759
|
+
* @returns Record mapping field names to their meta values
|
|
760
|
+
*
|
|
761
|
+
* To get typed meta keys with autocomplete, augment Zod's `GlobalMeta` interface:
|
|
762
|
+
*
|
|
763
|
+
* ```typescript
|
|
764
|
+
* // types/zod.ts
|
|
765
|
+
* import * as z from 'zod';
|
|
766
|
+
*
|
|
767
|
+
* declare module 'zod' {
|
|
768
|
+
* interface GlobalMeta {
|
|
769
|
+
* label: string; // static type for all schemas
|
|
770
|
+
* type: z.$output; // resolves to the schema's output type
|
|
771
|
+
* inputType: z.$input; // resolves to the schema's input type
|
|
772
|
+
* placeholder?: string;
|
|
773
|
+
* }
|
|
774
|
+
* }
|
|
775
|
+
* ```
|
|
776
|
+
*
|
|
777
|
+
* With `z.$output` / `z.$input`, Zod automatically types `.meta()` values
|
|
778
|
+
* based on each schema's inferred type:
|
|
779
|
+
*
|
|
780
|
+
* ```typescript
|
|
781
|
+
* z.string().meta({ type: 'hello' }) // ✅ type is string
|
|
782
|
+
* z.string().meta({ type: 42 }) // ❌ TypeScript error — expects string
|
|
783
|
+
* z.number().meta({ type: 42 }) // ✅ type is number
|
|
784
|
+
* ```
|
|
785
|
+
*
|
|
786
|
+
* @example
|
|
787
|
+
* ```typescript
|
|
788
|
+
* const schema = z.object({
|
|
789
|
+
* name: z.string().meta({ label: 'Name' }),
|
|
790
|
+
* age: z.number().meta({ label: 'Age' }),
|
|
791
|
+
* });
|
|
792
|
+
* getSchemaMeta({ schema }, 'label');
|
|
793
|
+
* // { name: 'Name', age: 'Age' }
|
|
794
|
+
* ```
|
|
795
|
+
*
|
|
796
|
+
* @see {@link extractMeta} for extracting meta from individual fields
|
|
797
|
+
* @since 0.3.0
|
|
798
|
+
*/
|
|
799
|
+
declare function getSchemaMeta<TSchema extends z.ZodType, TDiscriminatorKey extends DiscriminatorKey<TSchema>, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey>>(params: SchemaAndDiscriminatorProps<TSchema, TDiscriminatorKey, TDiscriminatorValue>, metaKey: string): Simplify<Partial<z.input<TSchema>>>;
|
|
800
|
+
/**
|
|
801
|
+
* Combines schema defaults and meta values into a single result.
|
|
802
|
+
* Meta values take precedence over defaults where both exist.
|
|
803
|
+
* For nested objects, performs a deep merge so that fields from both
|
|
804
|
+
* sources are preserved at every level.
|
|
805
|
+
*
|
|
806
|
+
* @param params - Schema and optional discriminator configuration
|
|
807
|
+
* @param metaKey - The meta property key to extract
|
|
808
|
+
* @returns Deep-merged record of defaults and meta values (meta wins on conflict)
|
|
809
|
+
*
|
|
810
|
+
* @example
|
|
811
|
+
* ```typescript
|
|
812
|
+
* const schema = z.object({
|
|
813
|
+
* name: z.string().meta({ label: 'Name' }).default('hello'),
|
|
814
|
+
* age: z.number().meta({ label: 'Age' }),
|
|
815
|
+
* bio: z.string().default(''),
|
|
816
|
+
* });
|
|
817
|
+
* getMergedSchemaDefaults({ schema }, 'label');
|
|
818
|
+
* // { name: 'Name', age: 'Age', bio: '' }
|
|
819
|
+
* ```
|
|
820
|
+
*
|
|
821
|
+
* @see {@link getSchemaDefaults} for extracting only defaults
|
|
822
|
+
* @see {@link getSchemaMeta} for extracting only meta
|
|
823
|
+
* @since 0.5.0
|
|
824
|
+
*/
|
|
825
|
+
declare function getMergedSchemaDefaults<TSchema extends z.ZodType, TDiscriminatorKey extends DiscriminatorKey<TSchema>, TDiscriminatorValue extends DiscriminatorValue<TSchema, TDiscriminatorKey>>(params: SchemaAndDiscriminatorProps<TSchema, TDiscriminatorKey, TDiscriminatorValue>, metaKey: string): Simplify<Partial<z.input<TSchema>>>;
|
|
826
|
+
|
|
713
827
|
/**
|
|
714
828
|
* Type representing a Zod type that has an unwrap method
|
|
715
829
|
*/
|
|
@@ -1053,4 +1167,4 @@ type ZodUnionCheck = $ZodCheckLessThanDef | $ZodCheckGreaterThanDef | $ZodCheckM
|
|
|
1053
1167
|
declare function getFieldChecks<T extends z$1.ZodType | $ZodType>(field: T): Array<ZodUnionCheck>;
|
|
1054
1168
|
declare function extractCheck(check: z$1.core.$ZodCheck<never>): ZodUnionCheck[];
|
|
1055
1169
|
|
|
1056
|
-
export { type CommonFields, type DiscriminatedInput, type DiscriminatorKey, type DiscriminatorProps, type DiscriminatorValue, type ExtractZodByPath, type ExtractZodUnionMember, 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, extractCheck, extractDefaultValue, extractDiscriminatedSchema, extractFieldFromSchema, getFieldChecks, getPrimitiveType, getSchemaDefaults, isPipeWithZodInput, removeDefault, requiresValidInput, tryStripNullishOnly };
|
|
1170
|
+
export { type CommonFields, type DiscriminatedInput, type DiscriminatorKey, type DiscriminatorProps, type DiscriminatorValue, type ExtractZodByPath, type ExtractZodUnionMember, 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, extractCheck, extractDefaultValue, extractDiscriminatedSchema, extractFieldFromSchema, extractMeta, getFieldChecks, getMergedSchemaDefaults, getPrimitiveType, getSchemaDefaults, getSchemaMeta, getShapeEntries, isPipeWithZodInput, removeDefault, requiresValidInput, tryStripNullishOnly };
|
package/dist/index.js
CHANGED
|
@@ -302,6 +302,95 @@ function extendWithMeta(field, transform) {
|
|
|
302
302
|
}
|
|
303
303
|
return transformedField.meta(__spreadValues({}, meta));
|
|
304
304
|
}
|
|
305
|
+
function extractMeta(field, metaKey) {
|
|
306
|
+
const meta = field.meta();
|
|
307
|
+
if (meta && metaKey in meta) {
|
|
308
|
+
return meta[metaKey];
|
|
309
|
+
}
|
|
310
|
+
if (field instanceof z3__namespace.ZodObject) {
|
|
311
|
+
const nested = {};
|
|
312
|
+
let hasAny = false;
|
|
313
|
+
for (const [key, nestedField] of getShapeEntries(field)) {
|
|
314
|
+
if (!nestedField) continue;
|
|
315
|
+
const value = extractMeta(nestedField, metaKey);
|
|
316
|
+
if (value !== void 0) {
|
|
317
|
+
nested[key] = value;
|
|
318
|
+
hasAny = true;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
if (hasAny) {
|
|
322
|
+
return nested;
|
|
323
|
+
}
|
|
324
|
+
return void 0;
|
|
325
|
+
}
|
|
326
|
+
if (field instanceof z3__namespace.ZodArray) {
|
|
327
|
+
const elementMeta = extractMeta(field.unwrap(), metaKey);
|
|
328
|
+
if (elementMeta !== void 0) {
|
|
329
|
+
return [elementMeta];
|
|
330
|
+
}
|
|
331
|
+
return void 0;
|
|
332
|
+
}
|
|
333
|
+
if (canUnwrap(field)) {
|
|
334
|
+
return extractMeta(field.unwrap(), metaKey);
|
|
335
|
+
}
|
|
336
|
+
if (field instanceof z3__namespace.ZodUnion) {
|
|
337
|
+
const unwrapped = tryStripNullishOnly(field);
|
|
338
|
+
if (unwrapped !== false) {
|
|
339
|
+
return extractMeta(unwrapped, metaKey);
|
|
340
|
+
}
|
|
341
|
+
return void 0;
|
|
342
|
+
}
|
|
343
|
+
if (isPipeWithZodInput(field)) {
|
|
344
|
+
return extractMeta(field.def.in, metaKey);
|
|
345
|
+
}
|
|
346
|
+
return void 0;
|
|
347
|
+
}
|
|
348
|
+
function getSchemaMeta(params, metaKey) {
|
|
349
|
+
const primitiveSchemaParams = __spreadProps(__spreadValues({}, params), {
|
|
350
|
+
schema: getPrimitiveType(params.schema)
|
|
351
|
+
});
|
|
352
|
+
let targetSchema;
|
|
353
|
+
if (primitiveSchemaParams.schema instanceof z3__namespace.ZodDiscriminatedUnion) {
|
|
354
|
+
targetSchema = extractDiscriminatedSchema(primitiveSchemaParams);
|
|
355
|
+
} else if (primitiveSchemaParams.schema instanceof z3__namespace.ZodObject) {
|
|
356
|
+
targetSchema = primitiveSchemaParams.schema;
|
|
357
|
+
}
|
|
358
|
+
const result = {};
|
|
359
|
+
if (targetSchema) {
|
|
360
|
+
for (const [key, field] of getShapeEntries(targetSchema)) {
|
|
361
|
+
if (!field) continue;
|
|
362
|
+
const value = extractMeta(field, metaKey);
|
|
363
|
+
if (value !== void 0) {
|
|
364
|
+
result[key] = value;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
return result;
|
|
369
|
+
}
|
|
370
|
+
function isPlainObject(value) {
|
|
371
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
372
|
+
}
|
|
373
|
+
function deepMerge(target, source) {
|
|
374
|
+
const result = __spreadValues({}, target);
|
|
375
|
+
for (const key of Object.keys(source)) {
|
|
376
|
+
const targetVal = target[key];
|
|
377
|
+
const sourceVal = source[key];
|
|
378
|
+
if (isPlainObject(targetVal) && isPlainObject(sourceVal)) {
|
|
379
|
+
result[key] = deepMerge(targetVal, sourceVal);
|
|
380
|
+
} else {
|
|
381
|
+
result[key] = sourceVal;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
return result;
|
|
385
|
+
}
|
|
386
|
+
function getMergedSchemaDefaults(params, metaKey) {
|
|
387
|
+
const defaults = getSchemaDefaults(params);
|
|
388
|
+
const meta = getSchemaMeta(params, metaKey);
|
|
389
|
+
return deepMerge(
|
|
390
|
+
defaults,
|
|
391
|
+
meta
|
|
392
|
+
);
|
|
393
|
+
}
|
|
305
394
|
|
|
306
395
|
exports.canUnwrap = canUnwrap;
|
|
307
396
|
exports.extendWithMeta = extendWithMeta;
|
|
@@ -309,9 +398,13 @@ exports.extractCheck = extractCheck;
|
|
|
309
398
|
exports.extractDefaultValue = extractDefaultValue;
|
|
310
399
|
exports.extractDiscriminatedSchema = extractDiscriminatedSchema;
|
|
311
400
|
exports.extractFieldFromSchema = extractFieldFromSchema;
|
|
401
|
+
exports.extractMeta = extractMeta;
|
|
312
402
|
exports.getFieldChecks = getFieldChecks;
|
|
403
|
+
exports.getMergedSchemaDefaults = getMergedSchemaDefaults;
|
|
313
404
|
exports.getPrimitiveType = getPrimitiveType;
|
|
314
405
|
exports.getSchemaDefaults = getSchemaDefaults;
|
|
406
|
+
exports.getSchemaMeta = getSchemaMeta;
|
|
407
|
+
exports.getShapeEntries = getShapeEntries;
|
|
315
408
|
exports.isPipeWithZodInput = isPipeWithZodInput;
|
|
316
409
|
exports.removeDefault = removeDefault;
|
|
317
410
|
exports.requiresValidInput = requiresValidInput;
|