use-zod-default 1.0.20 → 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 +15 -14
  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,21 +32,12 @@ 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
  }
38
- if (schema instanceof z.ZodISODate) {
39
- return new Date().toISOString().split('T')[0];
40
- }
41
- if (schema instanceof z.ZodISODateTime) {
42
- return new Date().toISOString();
43
- }
44
- if (schema instanceof z.ZodISODuration) {
45
- return 'P0D';
46
- }
47
- if (schema instanceof z.ZodISOTime) {
48
- return new Date().toISOString().split('T')[1];
49
- }
50
41
  if (schema instanceof z.ZodDiscriminatedUnion) {
51
42
  const firstOption = schema._zod.def.options[0];
52
43
  return defaultInstance(cast(firstOption));
@@ -84,6 +75,9 @@ const defaultInstance = (schema, source = {}) => {
84
75
  if (schema instanceof z.ZodObject) {
85
76
  return defaultInstance(schema, {});
86
77
  }
78
+ if (schema instanceof z.ZodOptional) {
79
+ return undefined;
80
+ }
87
81
  if (schema instanceof z.ZodPipe) {
88
82
  if (schema._zod.def.out instanceof z.ZodTransform) {
89
83
  const defaultInValue = getDefaultValue(cast(schema._zod.def.in));
@@ -113,6 +107,10 @@ const defaultInstance = (schema, source = {}) => {
113
107
  schema instanceof z.ZodGUID ||
114
108
  schema instanceof z.ZodIPv4 ||
115
109
  schema instanceof z.ZodIPv6 ||
110
+ schema instanceof z.ZodISODate ||
111
+ schema instanceof z.ZodISODateTime ||
112
+ schema instanceof z.ZodISODuration ||
113
+ schema instanceof z.ZodISOTime ||
116
114
  schema instanceof z.ZodJWT ||
117
115
  schema instanceof z.ZodKSUID ||
118
116
  schema instanceof z.ZodNanoID ||
@@ -200,11 +198,14 @@ const defaultInstance = (schema, source = {}) => {
200
198
  if (schema instanceof z.ZodBoolean) {
201
199
  return isBoolean(value) ? value : false;
202
200
  }
201
+ if (schema instanceof z.ZodCodec) {
202
+ return schema.decode(value);
203
+ }
203
204
  if (schema instanceof z.ZodDiscriminatedUnion) {
204
205
  return processDiscriminatedUnion(schema, value);
205
206
  }
206
207
  if (schema instanceof z.ZodDefault) {
207
- return processValue(cast(schema._zod.def.innerType), value);
208
+ return processValue(cast(schema._zod.def.innerType), !isNil(value) ? value : getDefaultValue(schema));
208
209
  }
209
210
  if (schema instanceof z.ZodISODate) {
210
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.20"
42
+ "version": "1.0.22"
43
43
  }