use-zod-default 1.0.21 → 1.0.22

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 (2) hide show
  1. package/dist/v4.js +11 -2
  2. package/package.json +3 -3
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];
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.22"
43
43
  }