use-zod-default 1.0.21 → 1.0.23

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.
Files changed (3) hide show
  1. package/dist/v3.js +8 -2
  2. package/dist/v4.js +19 -4
  3. package/package.json +3 -3
package/dist/v3.js CHANGED
@@ -190,10 +190,16 @@ const defaultInstance = (schema, source = {}) => {
190
190
  // Process defined schema properties
191
191
  for (const [key, fieldSchema] of Object.entries(shape)) {
192
192
  if (key in source) {
193
- result[key] = processValue(fieldSchema, source[key]);
193
+ const v = processValue(fieldSchema, source[key]);
194
+ if (v !== undefined) {
195
+ result[key] = v;
196
+ }
194
197
  }
195
198
  else {
196
- result[key] = getDefaultValue(fieldSchema);
199
+ const v = getDefaultValue(fieldSchema);
200
+ if (v !== undefined) {
201
+ result[key] = v;
202
+ }
197
203
  }
198
204
  }
199
205
  // Handle passthrough - preserve additional properties not in schema
package/dist/v4.js CHANGED
@@ -8,7 +8,7 @@ import isString from 'lodash/isString';
8
8
  const cast = (schema) => {
9
9
  return schema;
10
10
  };
11
- const defaultInstance = (schema, source = {}) => {
11
+ const defaultInstance = (schema, source) => {
12
12
  const getDefaultValue = (schema, defaultValue) => {
13
13
  if (schema instanceof z.ZodDefault) {
14
14
  const d = schema._zod.def.defaultValue;
@@ -32,6 +32,9 @@ const defaultInstance = (schema, source = {}) => {
32
32
  if (schema instanceof z.ZodBoolean) {
33
33
  return false;
34
34
  }
35
+ if (schema instanceof z.ZodCodec) {
36
+ return defaultInstance(cast(schema._zod.def.out));
37
+ }
35
38
  if (schema instanceof z.ZodDate) {
36
39
  return null;
37
40
  }
@@ -72,6 +75,9 @@ const defaultInstance = (schema, source = {}) => {
72
75
  if (schema instanceof z.ZodObject) {
73
76
  return defaultInstance(schema, {});
74
77
  }
78
+ if (schema instanceof z.ZodOptional) {
79
+ return undefined;
80
+ }
75
81
  if (schema instanceof z.ZodPipe) {
76
82
  if (schema._zod.def.out instanceof z.ZodTransform) {
77
83
  const defaultInValue = getDefaultValue(cast(schema._zod.def.in));
@@ -192,11 +198,14 @@ const defaultInstance = (schema, source = {}) => {
192
198
  if (schema instanceof z.ZodBoolean) {
193
199
  return isBoolean(value) ? value : false;
194
200
  }
201
+ if (schema instanceof z.ZodCodec) {
202
+ return schema.decode(value);
203
+ }
195
204
  if (schema instanceof z.ZodDiscriminatedUnion) {
196
205
  return processDiscriminatedUnion(schema, value);
197
206
  }
198
207
  if (schema instanceof z.ZodDefault) {
199
- return processValue(cast(schema._zod.def.innerType), value);
208
+ return processValue(cast(schema._zod.def.innerType), !isNil(value) ? value : getDefaultValue(schema));
200
209
  }
201
210
  if (schema instanceof z.ZodISODate) {
202
211
  return isString(value) ? value : new Date().toISOString().split('T')[0];
@@ -264,10 +273,16 @@ const defaultInstance = (schema, source = {}) => {
264
273
  // Create a proper ZodType from the field schema
265
274
  const zodFieldSchema = cast(fieldSchema);
266
275
  if (source && key in source) {
267
- result[key] = processValue(zodFieldSchema, source[key]);
276
+ const v = processValue(zodFieldSchema, source[key]);
277
+ if (v !== undefined) {
278
+ result[key] = v;
279
+ }
268
280
  }
269
281
  else {
270
- result[key] = getDefaultValue(zodFieldSchema);
282
+ const v = getDefaultValue(zodFieldSchema);
283
+ if (v !== undefined) {
284
+ result[key] = v;
285
+ }
271
286
  }
272
287
  }
273
288
  // Handle catchall - preserve additional properties not in schema
package/package.json CHANGED
@@ -10,11 +10,11 @@
10
10
  "typescript": "^5.6.3",
11
11
  "typescript-eslint": "^8.32.1",
12
12
  "vitest": "^3.2.4",
13
- "zod": "^4.0.17"
13
+ "zod": "^4.2.0"
14
14
  },
15
15
  "peerDependencies": {
16
16
  "lodash": "^4.17.21",
17
- "zod": "^4.0.17"
17
+ "zod": "^4.2.0"
18
18
  },
19
19
  "exports": {
20
20
  ".": "./dist/v4.js",
@@ -39,5 +39,5 @@
39
39
  "test:watch": "vitest",
40
40
  "test:coverage": "rm -rf coverage && vitest --run --coverage"
41
41
  },
42
- "version": "1.0.21"
42
+ "version": "1.0.23"
43
43
  }