@zod-utils/core 0.1.0 → 0.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 +68 -76
- package/dist/index.d.mts +285 -50
- package/dist/index.d.ts +285 -50
- package/dist/index.js +26 -33
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +26 -33
- package/dist/index.mjs.map +1 -1
- package/package.json +13 -5
package/README.md
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
[](https://opensource.org/licenses/MIT)
|
|
6
6
|
[](https://www.typescriptlang.org/)
|
|
7
7
|
[](https://github.com/thu-san/zod-utils/actions)
|
|
8
|
+
[](https://codecov.io/gh/thu-san/zod-utils)
|
|
8
9
|
|
|
9
10
|
Pure TypeScript utilities for Zod schema manipulation and default extraction. No React dependencies.
|
|
10
11
|
|
|
@@ -26,20 +27,22 @@ npm install @zod-utils/core zod
|
|
|
26
27
|
|
|
27
28
|
### `getSchemaDefaults(schema)`
|
|
28
29
|
|
|
29
|
-
Extract all default values from a Zod object schema.
|
|
30
|
+
Extract all default values from a Zod object schema. Only extracts fields that explicitly have `.default()` on them.
|
|
30
31
|
|
|
31
32
|
```typescript
|
|
32
|
-
import { getSchemaDefaults } from
|
|
33
|
-
import { z } from
|
|
33
|
+
import { getSchemaDefaults } from "@zod-utils/core";
|
|
34
|
+
import { z } from "zod";
|
|
34
35
|
|
|
35
36
|
const schema = z.object({
|
|
36
|
-
name: z.string().default(
|
|
37
|
+
name: z.string().default("John Doe"),
|
|
37
38
|
age: z.number().default(25),
|
|
38
39
|
email: z.string().email(), // no default - skipped
|
|
39
|
-
settings: z
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
40
|
+
settings: z
|
|
41
|
+
.object({
|
|
42
|
+
theme: z.string().default("light"),
|
|
43
|
+
notifications: z.boolean().default(true),
|
|
44
|
+
})
|
|
45
|
+
.default({}), // must have explicit .default() to be extracted
|
|
43
46
|
tags: z.array(z.string()).default([]),
|
|
44
47
|
});
|
|
45
48
|
|
|
@@ -47,53 +50,85 @@ const defaults = getSchemaDefaults(schema);
|
|
|
47
50
|
// {
|
|
48
51
|
// name: 'John Doe',
|
|
49
52
|
// age: 25,
|
|
50
|
-
// settings: {
|
|
53
|
+
// settings: {},
|
|
51
54
|
// tags: []
|
|
52
55
|
// }
|
|
53
56
|
```
|
|
54
57
|
|
|
58
|
+
**Important:** Only fields with explicit `.default()` are extracted. Nested object fields without an explicit default on the parent field are not extracted, even if they contain defaults internally.
|
|
59
|
+
|
|
55
60
|
**Handles:**
|
|
56
|
-
|
|
61
|
+
|
|
57
62
|
- Optional fields with defaults: `.optional().default(value)`
|
|
63
|
+
- Nullable fields with defaults: `.nullable().default(value)`
|
|
58
64
|
- Arrays with defaults: `.array().default([])`
|
|
59
|
-
-
|
|
65
|
+
- Objects with defaults: `.object({...}).default({})`
|
|
66
|
+
- Skips fields without explicit defaults
|
|
60
67
|
|
|
61
68
|
---
|
|
62
69
|
|
|
63
70
|
### `checkIfFieldIsRequired(field)`
|
|
64
71
|
|
|
65
|
-
Check if a Zod field is required
|
|
72
|
+
Check if a Zod field is required. Returns `false` if the field accepts any of:
|
|
73
|
+
|
|
74
|
+
- `undefined` (via `.optional()` or `.default()`)
|
|
75
|
+
- `null` (via `.nullable()`)
|
|
76
|
+
- Empty string (plain `z.string()` without `.min(1)` or `.nonempty()`)
|
|
77
|
+
- Empty array (plain `z.array()` without `.min(1)` or `.nonempty()`)
|
|
66
78
|
|
|
67
79
|
```typescript
|
|
68
|
-
import { checkIfFieldIsRequired } from
|
|
69
|
-
import { z } from
|
|
80
|
+
import { checkIfFieldIsRequired } from "@zod-utils/core";
|
|
81
|
+
import { z } from "zod";
|
|
82
|
+
|
|
83
|
+
// Required fields - return true
|
|
84
|
+
const requiredString = z.string().min(1);
|
|
85
|
+
const nonemptyString = z.string().nonempty();
|
|
86
|
+
const requiredArray = z.array(z.string()).min(1);
|
|
87
|
+
const nonemptyArray = z.array(z.string()).nonempty();
|
|
70
88
|
|
|
71
|
-
|
|
89
|
+
checkIfFieldIsRequired(requiredString); // true
|
|
90
|
+
checkIfFieldIsRequired(nonemptyString); // true
|
|
91
|
+
checkIfFieldIsRequired(requiredArray); // true
|
|
92
|
+
checkIfFieldIsRequired(nonemptyArray); // true
|
|
93
|
+
|
|
94
|
+
// Fields accepting undefined - return false
|
|
72
95
|
const optionalField = z.string().optional();
|
|
73
|
-
const
|
|
96
|
+
const fieldWithDefault = z.string().default("hello");
|
|
97
|
+
|
|
98
|
+
checkIfFieldIsRequired(optionalField); // false
|
|
99
|
+
checkIfFieldIsRequired(fieldWithDefault); // false
|
|
100
|
+
|
|
101
|
+
// Fields accepting empty values - return false
|
|
102
|
+
const emptyStringAllowed = z.string();
|
|
103
|
+
const emptyArrayAllowed = z.array(z.string());
|
|
74
104
|
|
|
75
|
-
checkIfFieldIsRequired(requiredField); // true
|
|
76
|
-
checkIfFieldIsRequired(optionalField); // false
|
|
77
105
|
checkIfFieldIsRequired(emptyStringAllowed); // false
|
|
106
|
+
checkIfFieldIsRequired(emptyArrayAllowed); // false
|
|
107
|
+
|
|
108
|
+
// Fields accepting null - return false
|
|
109
|
+
const nullableField = z.string().nullable();
|
|
110
|
+
|
|
111
|
+
checkIfFieldIsRequired(nullableField); // false
|
|
78
112
|
```
|
|
79
113
|
|
|
80
114
|
---
|
|
81
115
|
|
|
82
|
-
### `getPrimitiveType(field
|
|
116
|
+
### `getPrimitiveType(field)`
|
|
83
117
|
|
|
84
118
|
Get the primitive type of a Zod field by unwrapping optional/nullable wrappers.
|
|
119
|
+
Stops at arrays without unwrapping them.
|
|
85
120
|
|
|
86
121
|
```typescript
|
|
87
|
-
import { getPrimitiveType } from
|
|
88
|
-
import { z } from
|
|
122
|
+
import { getPrimitiveType } from "@zod-utils/core";
|
|
123
|
+
import { z } from "zod";
|
|
89
124
|
|
|
90
125
|
const field = z.string().optional().nullable();
|
|
91
126
|
const primitive = getPrimitiveType(field);
|
|
92
127
|
// Returns the underlying string schema
|
|
93
128
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
129
|
+
const arrayField = z.array(z.string()).optional();
|
|
130
|
+
const arrayPrimitive = getPrimitiveType(arrayField);
|
|
131
|
+
// Returns the ZodArray (stops at arrays)
|
|
97
132
|
```
|
|
98
133
|
|
|
99
134
|
---
|
|
@@ -103,14 +138,14 @@ getPrimitiveType(z.array(z.string()), { unwrapArrays: true }); // Continues unw
|
|
|
103
138
|
Remove default values from a Zod field.
|
|
104
139
|
|
|
105
140
|
```typescript
|
|
106
|
-
import { removeDefault } from
|
|
107
|
-
import { z } from
|
|
141
|
+
import { removeDefault } from "@zod-utils/core";
|
|
142
|
+
import { z } from "zod";
|
|
108
143
|
|
|
109
|
-
const withDefault = z.string().default(
|
|
144
|
+
const withDefault = z.string().default("hello");
|
|
110
145
|
const withoutDefault = removeDefault(withDefault);
|
|
111
146
|
|
|
112
|
-
withDefault.parse(undefined);
|
|
113
|
-
withoutDefault.parse(undefined);
|
|
147
|
+
withDefault.parse(undefined); // 'hello'
|
|
148
|
+
withoutDefault.parse(undefined); // throws error
|
|
114
149
|
```
|
|
115
150
|
|
|
116
151
|
---
|
|
@@ -120,10 +155,10 @@ withoutDefault.parse(undefined); // throws error
|
|
|
120
155
|
Extract the default value from a Zod field (recursively unwraps optional/nullable).
|
|
121
156
|
|
|
122
157
|
```typescript
|
|
123
|
-
import { extractDefault } from
|
|
124
|
-
import { z } from
|
|
158
|
+
import { extractDefault } from "@zod-utils/core";
|
|
159
|
+
import { z } from "zod";
|
|
125
160
|
|
|
126
|
-
const field = z.string().optional().default(
|
|
161
|
+
const field = z.string().optional().default("hello");
|
|
127
162
|
extractDefault(field); // 'hello'
|
|
128
163
|
|
|
129
164
|
const noDefault = z.string();
|
|
@@ -132,63 +167,20 @@ extractDefault(noDefault); // undefined
|
|
|
132
167
|
|
|
133
168
|
---
|
|
134
169
|
|
|
135
|
-
### `getUnwrappedType(field)`
|
|
136
|
-
|
|
137
|
-
Get the unwrapped type without going through defaults. Useful for detecting nested objects/arrays.
|
|
138
|
-
|
|
139
|
-
```typescript
|
|
140
|
-
import { getUnwrappedType } from '@zod-utils/core';
|
|
141
|
-
import { z } from 'zod';
|
|
142
|
-
|
|
143
|
-
const field = z.object({ name: z.string() }).optional().default({});
|
|
144
|
-
const unwrapped = getUnwrappedType(field);
|
|
145
|
-
// Returns the ZodObject (preserves the default wrapper)
|
|
146
|
-
```
|
|
147
|
-
|
|
148
|
-
---
|
|
149
|
-
|
|
150
170
|
## Type Utilities
|
|
151
171
|
|
|
152
|
-
### `MakeOptionalAndNullable<T>`
|
|
153
|
-
|
|
154
|
-
Make all properties optional and nullable. Useful for form input types.
|
|
155
|
-
|
|
156
|
-
```typescript
|
|
157
|
-
import type { MakeOptionalAndNullable } from '@zod-utils/core';
|
|
158
|
-
|
|
159
|
-
type User = {
|
|
160
|
-
name: string;
|
|
161
|
-
age: number;
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
type FormInput = MakeOptionalAndNullable<User>;
|
|
165
|
-
// { name?: string | null; age?: number | null; }
|
|
166
|
-
```
|
|
167
|
-
|
|
168
172
|
### `Simplify<T>`
|
|
169
173
|
|
|
170
174
|
Simplify complex types for better IDE hints.
|
|
171
175
|
|
|
172
176
|
```typescript
|
|
173
|
-
import type { Simplify } from
|
|
177
|
+
import type { Simplify } from "@zod-utils/core";
|
|
174
178
|
|
|
175
179
|
type Complex = { a: string } & { b: number };
|
|
176
180
|
type Simple = Simplify<Complex>;
|
|
177
181
|
// { a: string; b: number }
|
|
178
182
|
```
|
|
179
183
|
|
|
180
|
-
### `PickArrayObject<T>`
|
|
181
|
-
|
|
182
|
-
Extract the element type from an array.
|
|
183
|
-
|
|
184
|
-
```typescript
|
|
185
|
-
import type { PickArrayObject } from '@zod-utils/core';
|
|
186
|
-
|
|
187
|
-
type Users = Array<{ name: string }>;
|
|
188
|
-
type User = PickArrayObject<Users>;
|
|
189
|
-
// { name: string }
|
|
190
|
-
```
|
|
191
|
-
|
|
192
184
|
---
|
|
193
185
|
|
|
194
186
|
## License
|
package/dist/index.d.mts
CHANGED
|
@@ -1,79 +1,314 @@
|
|
|
1
1
|
import * as z from 'zod';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
*
|
|
21
|
-
* @
|
|
22
|
-
*
|
|
4
|
+
* Simplifies complex TypeScript types for better IDE hover tooltips and error messages.
|
|
5
|
+
*
|
|
6
|
+
* This utility type flattens intersections and complex type expressions into a single,
|
|
7
|
+
* readable object type. This is especially useful when working with mapped types,
|
|
8
|
+
* conditional types, or type intersections that produce hard-to-read IDE hints.
|
|
9
|
+
*
|
|
10
|
+
* @template T - The type to simplify
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* Simplifying intersection types
|
|
14
|
+
* ```typescript
|
|
15
|
+
* type A = { name: string };
|
|
16
|
+
* type B = { age: number };
|
|
17
|
+
* type C = A & B; // Shows as "A & B" in IDE
|
|
18
|
+
* type D = Simplify<A & B>; // Shows as "{ name: string; age: number }" in IDE
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* Simplifying Partial<> results
|
|
23
|
+
* ```typescript
|
|
24
|
+
* type User = { name: string; age: number; email: string };
|
|
25
|
+
* type PartialUser = Partial<User>; // Shows as "Partial<User>" in IDE
|
|
26
|
+
* type SimplifiedPartialUser = Simplify<Partial<User>>;
|
|
27
|
+
* // Shows as "{ name?: string; age?: number; email?: string }" in IDE
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* Usage with zod schema inference
|
|
32
|
+
* ```typescript
|
|
33
|
+
* const schema = z.object({ id: z.string() })
|
|
34
|
+
* .merge(z.object({ name: z.string() }));
|
|
35
|
+
*
|
|
36
|
+
* type InferredType = z.infer<typeof schema>; // May show complex type
|
|
37
|
+
* type SimplifiedType = Simplify<z.infer<typeof schema>>;
|
|
38
|
+
* // Shows clear: { id: string; name: string }
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* @since 0.1.0
|
|
23
42
|
*/
|
|
24
|
-
|
|
43
|
+
type Simplify<T> = {
|
|
44
|
+
[K in keyof T]: T[K];
|
|
45
|
+
} & {};
|
|
25
46
|
|
|
26
47
|
/**
|
|
27
|
-
*
|
|
48
|
+
* Extracts the default value from a Zod field, recursively unwrapping optional and nullable layers.
|
|
49
|
+
*
|
|
50
|
+
* This function traverses through wrapper types (like `ZodOptional`, `ZodNullable`) to find
|
|
51
|
+
* the underlying `ZodDefault` and returns its default value. If no default is found, returns `undefined`.
|
|
52
|
+
*
|
|
53
|
+
* @template T - The Zod type to extract default from
|
|
28
54
|
* @param field - The Zod field to extract default from
|
|
29
55
|
* @returns The default value if present, undefined otherwise
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* Basic usage with default value
|
|
59
|
+
* ```typescript
|
|
60
|
+
* const field = z.string().default('hello');
|
|
61
|
+
* const defaultValue = extractDefault(field);
|
|
62
|
+
* // Result: 'hello'
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* Unwrapping optional/nullable layers
|
|
67
|
+
* ```typescript
|
|
68
|
+
* const field = z.string().default('world').optional();
|
|
69
|
+
* const defaultValue = extractDefault(field);
|
|
70
|
+
* // Result: 'world' (unwraps optional to find default)
|
|
71
|
+
* ```
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* Field without default
|
|
75
|
+
* ```typescript
|
|
76
|
+
* const field = z.string().optional();
|
|
77
|
+
* const defaultValue = extractDefault(field);
|
|
78
|
+
* // Result: undefined
|
|
79
|
+
* ```
|
|
80
|
+
*
|
|
81
|
+
* @see {@link getSchemaDefaults} for extracting defaults from entire schemas
|
|
82
|
+
* @since 0.1.0
|
|
30
83
|
*/
|
|
31
|
-
declare function extractDefault(field:
|
|
32
|
-
/**
|
|
33
|
-
* Get the unwrapped type without going through defaults
|
|
34
|
-
* Useful for detecting nested objects/arrays while preserving defaults
|
|
35
|
-
* @param field - The Zod field to unwrap
|
|
36
|
-
* @returns The unwrapped type
|
|
37
|
-
*/
|
|
38
|
-
declare function getUnwrappedType(field: z.ZodTypeAny): z.ZodTypeAny;
|
|
84
|
+
declare function extractDefault<T extends z.ZodTypeAny>(field: T): z.infer<T> | undefined;
|
|
39
85
|
/**
|
|
40
|
-
*
|
|
41
|
-
*
|
|
86
|
+
* Extracts default values from a Zod object schema while skipping fields without defaults.
|
|
87
|
+
*
|
|
88
|
+
* This function recursively traverses the schema and collects all fields that have
|
|
89
|
+
* explicit default values defined. Fields without defaults are excluded from the result.
|
|
90
|
+
*
|
|
91
|
+
* **Important:** Nested defaults are NOT extracted unless the parent object also has
|
|
92
|
+
* an explicit `.default()`. This is by design to match Zod's default value behavior.
|
|
93
|
+
*
|
|
94
|
+
* @template T - The Zod object schema type
|
|
42
95
|
* @param schema - The Zod object schema to extract defaults from
|
|
43
|
-
* @returns
|
|
96
|
+
* @returns A partial object containing only fields with default values
|
|
44
97
|
*
|
|
45
98
|
* @example
|
|
46
|
-
*
|
|
99
|
+
* Basic usage
|
|
100
|
+
* ```typescript
|
|
47
101
|
* const schema = z.object({
|
|
48
102
|
* name: z.string().default('John'),
|
|
49
|
-
* age: z.number(), // no default - skipped
|
|
103
|
+
* age: z.number(), // no default - will be skipped
|
|
104
|
+
* email: z.string().email().optional(),
|
|
105
|
+
* });
|
|
106
|
+
*
|
|
107
|
+
* const defaults = getSchemaDefaults(schema);
|
|
108
|
+
* // Result: { name: 'John' }
|
|
109
|
+
* ```
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* Nested objects with defaults
|
|
113
|
+
* ```typescript
|
|
114
|
+
* const schema = z.object({
|
|
115
|
+
* user: z.object({
|
|
116
|
+
* name: z.string().default('Guest')
|
|
117
|
+
* }).default({ name: 'Guest' }), // ✅ Extracted because parent has .default()
|
|
118
|
+
*
|
|
50
119
|
* settings: z.object({
|
|
51
120
|
* theme: z.string().default('light')
|
|
52
|
-
* })
|
|
121
|
+
* }), // ❌ NOT extracted - parent has no .default()
|
|
53
122
|
* });
|
|
54
123
|
*
|
|
55
|
-
* getSchemaDefaults(schema);
|
|
56
|
-
* //
|
|
124
|
+
* const defaults = getSchemaDefaults(schema);
|
|
125
|
+
* // Result: { user: { name: 'Guest' } }
|
|
57
126
|
* ```
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* Unwrapping optional/nullable fields
|
|
130
|
+
* ```typescript
|
|
131
|
+
* const schema = z.object({
|
|
132
|
+
* title: z.string().default('Untitled').optional(),
|
|
133
|
+
* count: z.number().default(0).nullable(),
|
|
134
|
+
* });
|
|
135
|
+
*
|
|
136
|
+
* const defaults = getSchemaDefaults(schema);
|
|
137
|
+
* // Result: { title: 'Untitled', count: 0 }
|
|
138
|
+
* ```
|
|
139
|
+
*
|
|
140
|
+
* @see {@link extractDefault} for extracting defaults from individual fields
|
|
141
|
+
* @since 0.1.0
|
|
58
142
|
*/
|
|
59
|
-
declare function getSchemaDefaults<T extends z.ZodObject
|
|
143
|
+
declare function getSchemaDefaults<T extends z.ZodObject>(schema: T): Simplify<Partial<z.infer<T>>>;
|
|
60
144
|
|
|
61
145
|
/**
|
|
62
|
-
*
|
|
146
|
+
* Type representing a Zod type that has an unwrap method
|
|
147
|
+
*/
|
|
148
|
+
type Unwrappable = {
|
|
149
|
+
unwrap: () => z.ZodTypeAny;
|
|
150
|
+
};
|
|
151
|
+
/**
|
|
152
|
+
* Type guard to check if a Zod field can be unwrapped (has wrapper types like optional, nullable, default).
|
|
153
|
+
*
|
|
154
|
+
* This checks whether a Zod type has an `unwrap()` method, which is present on wrapper types
|
|
155
|
+
* like `ZodOptional`, `ZodNullable`, `ZodDefault`, and others.
|
|
156
|
+
*
|
|
157
|
+
* @param field - The Zod field to check
|
|
158
|
+
* @returns True if the field has an unwrap method, false otherwise
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* ```typescript
|
|
162
|
+
* const optionalField = z.string().optional();
|
|
163
|
+
* console.log(canUnwrap(optionalField)); // true
|
|
164
|
+
*
|
|
165
|
+
* const plainField = z.string();
|
|
166
|
+
* console.log(canUnwrap(plainField)); // false
|
|
167
|
+
* ```
|
|
168
|
+
*
|
|
169
|
+
* @since 0.1.0
|
|
170
|
+
*/
|
|
171
|
+
declare function canUnwrap(field: z.ZodTypeAny): field is z.ZodTypeAny & Unwrappable;
|
|
172
|
+
/**
|
|
173
|
+
* Gets the underlying primitive type of a Zod field by recursively unwrapping wrapper types.
|
|
174
|
+
*
|
|
175
|
+
* This function removes wrapper layers (optional, nullable, default) to reveal the base type.
|
|
176
|
+
* **Important:** It stops at array types without unwrapping them, treating arrays as primitives.
|
|
177
|
+
*
|
|
178
|
+
* @template T - The Zod type to unwrap
|
|
179
|
+
* @param field - The Zod field to unwrap
|
|
180
|
+
* @returns The unwrapped primitive Zod type
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* Unwrapping to string primitive
|
|
184
|
+
* ```typescript
|
|
185
|
+
* const field = z.string().optional().nullable();
|
|
186
|
+
* const primitive = getPrimitiveType(field);
|
|
187
|
+
* // Result: z.string() (unwrapped all wrappers)
|
|
188
|
+
* ```
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* Stopping at array type
|
|
192
|
+
* ```typescript
|
|
193
|
+
* const field = z.array(z.string()).optional();
|
|
194
|
+
* const primitive = getPrimitiveType(field);
|
|
195
|
+
* // Result: z.array(z.string()) (stops at array, doesn't unwrap it)
|
|
196
|
+
* ```
|
|
197
|
+
*
|
|
198
|
+
* @example
|
|
199
|
+
* Unwrapping defaults
|
|
200
|
+
* ```typescript
|
|
201
|
+
* const field = z.number().default(0).optional();
|
|
202
|
+
* const primitive = getPrimitiveType(field);
|
|
203
|
+
* // Result: z.number()
|
|
204
|
+
* ```
|
|
205
|
+
*
|
|
206
|
+
* @see {@link canUnwrap} for checking if a field can be unwrapped
|
|
207
|
+
* @since 0.1.0
|
|
63
208
|
*/
|
|
64
|
-
|
|
209
|
+
declare const getPrimitiveType: <T extends z.ZodTypeAny>(field: T) => z.ZodTypeAny;
|
|
210
|
+
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;
|
|
65
211
|
/**
|
|
66
|
-
*
|
|
212
|
+
* Removes default values from a Zod field while preserving other wrapper types.
|
|
213
|
+
*
|
|
214
|
+
* This function recursively removes `ZodDefault` wrappers from a field, while maintaining
|
|
215
|
+
* `optional()` and `nullable()` wrappers. Useful for scenarios where you want to check
|
|
216
|
+
* field requirements without considering default values.
|
|
217
|
+
*
|
|
218
|
+
* @template T - The Zod type to process
|
|
219
|
+
* @param field - The Zod field to remove defaults from
|
|
220
|
+
* @returns The field without defaults but with optional/nullable preserved
|
|
221
|
+
*
|
|
222
|
+
* @example
|
|
223
|
+
* Removing simple default
|
|
224
|
+
* ```typescript
|
|
225
|
+
* const field = z.string().default('hello');
|
|
226
|
+
* const withoutDefault = removeDefault(field);
|
|
227
|
+
* // Result: z.string()
|
|
228
|
+
* ```
|
|
229
|
+
*
|
|
230
|
+
* @example
|
|
231
|
+
* Preserving optional wrapper
|
|
232
|
+
* ```typescript
|
|
233
|
+
* const field = z.string().default('hello').optional();
|
|
234
|
+
* const withoutDefault = removeDefault(field);
|
|
235
|
+
* // Result: z.string().optional()
|
|
236
|
+
* ```
|
|
237
|
+
*
|
|
238
|
+
* @example
|
|
239
|
+
* Nested defaults
|
|
240
|
+
* ```typescript
|
|
241
|
+
* const field = z.string().default('inner').nullable().default('outer');
|
|
242
|
+
* const withoutDefault = removeDefault(field);
|
|
243
|
+
* // Result: z.string().nullable()
|
|
244
|
+
* ```
|
|
245
|
+
*
|
|
246
|
+
* @see {@link checkIfFieldIsRequired} for usage with requirement checking
|
|
247
|
+
* @since 0.1.0
|
|
67
248
|
*/
|
|
68
|
-
|
|
69
|
-
[K in keyof T]: T[K];
|
|
70
|
-
} & {};
|
|
249
|
+
declare function removeDefault<T extends z.ZodType>(field: T): StripZodDefault<T>;
|
|
71
250
|
/**
|
|
72
|
-
*
|
|
73
|
-
*
|
|
251
|
+
* Checks if a Zod field is truly required by testing multiple acceptance criteria.
|
|
252
|
+
*
|
|
253
|
+
* A field is considered **not required** if it accepts any of the following:
|
|
254
|
+
* - `undefined` (via `.optional()` or `.default()`)
|
|
255
|
+
* - `null` (via `.nullable()`)
|
|
256
|
+
* - Empty string (plain `z.string()` without `.min(1)` or `.nonempty()`)
|
|
257
|
+
* - Empty array (plain `z.array()` without `.min(1)` or `.nonempty()`)
|
|
258
|
+
*
|
|
259
|
+
* **Note:** Fields with `.default()` are considered not required since they'll have a value
|
|
260
|
+
* even if the user doesn't provide one.
|
|
261
|
+
*
|
|
262
|
+
* @template T - The Zod type to check
|
|
263
|
+
* @param field - The Zod field to check for required status
|
|
264
|
+
* @returns True if the field is required, false otherwise
|
|
265
|
+
*
|
|
266
|
+
* @example
|
|
267
|
+
* Required field
|
|
268
|
+
* ```typescript
|
|
269
|
+
* const field = z.string().min(1);
|
|
270
|
+
* console.log(checkIfFieldIsRequired(field)); // true
|
|
271
|
+
* ```
|
|
272
|
+
*
|
|
273
|
+
* @example
|
|
274
|
+
* Optional field (not required)
|
|
275
|
+
* ```typescript
|
|
276
|
+
* const field = z.string().optional();
|
|
277
|
+
* console.log(checkIfFieldIsRequired(field)); // false
|
|
278
|
+
* ```
|
|
279
|
+
*
|
|
280
|
+
* @example
|
|
281
|
+
* Field with default (not required)
|
|
282
|
+
* ```typescript
|
|
283
|
+
* const field = z.string().default('hello');
|
|
284
|
+
* console.log(checkIfFieldIsRequired(field)); // false
|
|
285
|
+
* ```
|
|
286
|
+
*
|
|
287
|
+
* @example
|
|
288
|
+
* String without min length (not required - accepts empty string)
|
|
289
|
+
* ```typescript
|
|
290
|
+
* const field = z.string();
|
|
291
|
+
* console.log(checkIfFieldIsRequired(field)); // false
|
|
292
|
+
* ```
|
|
293
|
+
*
|
|
294
|
+
* @example
|
|
295
|
+
* String with nonempty (required)
|
|
296
|
+
* ```typescript
|
|
297
|
+
* const field = z.string().nonempty();
|
|
298
|
+
* console.log(checkIfFieldIsRequired(field)); // true
|
|
299
|
+
* ```
|
|
300
|
+
*
|
|
301
|
+
* @example
|
|
302
|
+
* Nullable field (not required)
|
|
303
|
+
* ```typescript
|
|
304
|
+
* const field = z.number().nullable();
|
|
305
|
+
* console.log(checkIfFieldIsRequired(field)); // false
|
|
306
|
+
* ```
|
|
307
|
+
*
|
|
308
|
+
* @see {@link removeDefault} for understanding how defaults are handled
|
|
309
|
+
* @see {@link getPrimitiveType} for understanding type unwrapping
|
|
310
|
+
* @since 0.1.0
|
|
74
311
|
*/
|
|
75
|
-
|
|
76
|
-
[K in keyof T]?: T[K] | null;
|
|
77
|
-
};
|
|
312
|
+
declare const checkIfFieldIsRequired: <T extends z.ZodType>(field: T) => boolean;
|
|
78
313
|
|
|
79
|
-
export { type
|
|
314
|
+
export { type Simplify, canUnwrap, checkIfFieldIsRequired, extractDefault, getPrimitiveType, getSchemaDefaults, removeDefault };
|