use-zod-default 1.0.18 → 1.0.20

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 +64 -5
  2. package/package.json +6 -5
package/dist/v4.js CHANGED
@@ -23,12 +23,30 @@ const defaultInstance = (schema, source = {}) => {
23
23
  if (schema instanceof z.ZodArray) {
24
24
  return [];
25
25
  }
26
+ if (schema instanceof z.ZodBigInt) {
27
+ if (schema._zod.bag?.minimum && schema._zod.bag?.minimum > 0) {
28
+ return BigInt(schema._zod.bag?.minimum);
29
+ }
30
+ return BigInt(0);
31
+ }
26
32
  if (schema instanceof z.ZodBoolean) {
27
33
  return false;
28
34
  }
29
35
  if (schema instanceof z.ZodDate) {
30
36
  return null;
31
37
  }
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
+ }
32
50
  if (schema instanceof z.ZodDiscriminatedUnion) {
33
51
  const firstOption = schema._zod.def.options[0];
34
52
  return defaultInstance(cast(firstOption));
@@ -57,8 +75,11 @@ const defaultInstance = (schema, source = {}) => {
57
75
  if (schema instanceof z.ZodNullable) {
58
76
  return null;
59
77
  }
60
- if (schema instanceof z.ZodNumber || schema instanceof z.ZodBigInt) {
61
- return schema._zod.bag?.minimum ?? 0;
78
+ if (schema instanceof z.ZodNumber) {
79
+ if (schema._zod.bag?.minimum && schema._zod.bag?.minimum > 0) {
80
+ return schema._zod.bag?.minimum;
81
+ }
82
+ return 0;
62
83
  }
63
84
  if (schema instanceof z.ZodObject) {
64
85
  return defaultInstance(schema, {});
@@ -68,7 +89,7 @@ const defaultInstance = (schema, source = {}) => {
68
89
  const defaultInValue = getDefaultValue(cast(schema._zod.def.in));
69
90
  return getDefaultValue(cast(schema._zod.def.out), defaultInValue);
70
91
  }
71
- return getDefaultValue(cast(schema._zod.def.in), getDefaultValue(cast(schema._zod.def.out)));
92
+ return getDefaultValue(cast(schema._zod.def.out));
72
93
  }
73
94
  if (schema instanceof z.ZodPromise) {
74
95
  return Promise.resolve(getDefaultValue(cast(schema._zod.def.innerType)));
@@ -79,7 +100,27 @@ const defaultInstance = (schema, source = {}) => {
79
100
  if (schema instanceof z.ZodSet) {
80
101
  return new Set();
81
102
  }
82
- if (schema instanceof z.ZodString) {
103
+ if (schema instanceof z.ZodBase64 ||
104
+ schema instanceof z.ZodBase64URL ||
105
+ schema instanceof z.ZodCIDRv4 ||
106
+ schema instanceof z.ZodCIDRv6 ||
107
+ schema instanceof z.ZodCUID ||
108
+ schema instanceof z.ZodCUID2 ||
109
+ schema instanceof z.ZodCustomStringFormat ||
110
+ schema instanceof z.ZodE164 ||
111
+ schema instanceof z.ZodEmail ||
112
+ schema instanceof z.ZodEmoji ||
113
+ schema instanceof z.ZodGUID ||
114
+ schema instanceof z.ZodIPv4 ||
115
+ schema instanceof z.ZodIPv6 ||
116
+ schema instanceof z.ZodJWT ||
117
+ schema instanceof z.ZodKSUID ||
118
+ schema instanceof z.ZodNanoID ||
119
+ schema instanceof z.ZodString ||
120
+ schema instanceof z.ZodULID ||
121
+ schema instanceof z.ZodURL ||
122
+ schema instanceof z.ZodUUID ||
123
+ schema instanceof z.ZodXID) {
83
124
  return '';
84
125
  }
85
126
  if (schema instanceof z.ZodSymbol) {
@@ -150,6 +191,12 @@ const defaultInstance = (schema, source = {}) => {
150
191
  if (schema instanceof z.ZodArray) {
151
192
  return processArray(schema, value);
152
193
  }
194
+ if (schema instanceof z.ZodBigInt) {
195
+ if (typeof value === 'bigint') {
196
+ return value;
197
+ }
198
+ return typeof value === 'number' ? BigInt(value) : BigInt(0);
199
+ }
153
200
  if (schema instanceof z.ZodBoolean) {
154
201
  return isBoolean(value) ? value : false;
155
202
  }
@@ -159,13 +206,25 @@ const defaultInstance = (schema, source = {}) => {
159
206
  if (schema instanceof z.ZodDefault) {
160
207
  return processValue(cast(schema._zod.def.innerType), value);
161
208
  }
209
+ if (schema instanceof z.ZodISODate) {
210
+ return isString(value) ? value : new Date().toISOString().split('T')[0];
211
+ }
212
+ if (schema instanceof z.ZodISODateTime) {
213
+ return isString(value) ? value : new Date().toISOString();
214
+ }
215
+ if (schema instanceof z.ZodISODuration) {
216
+ return isString(value) ? value : 'P0D';
217
+ }
218
+ if (schema instanceof z.ZodISOTime) {
219
+ return isString(value) ? value : new Date().toISOString().split('T')[1];
220
+ }
162
221
  if (schema instanceof z.ZodMap) {
163
222
  return processMap(schema, value);
164
223
  }
165
224
  if (schema instanceof z.ZodNullable) {
166
225
  return isNil(value) ? null : processValue(schema._zod.def.innerType, value);
167
226
  }
168
- if (schema instanceof z.ZodNumber || schema instanceof z.ZodBigInt) {
227
+ if (schema instanceof z.ZodNumber) {
169
228
  return isNumber(value) ? value : (schema._zod.bag?.minimum ?? 0);
170
229
  }
171
230
  if (schema instanceof z.ZodObject) {
package/package.json CHANGED
@@ -2,14 +2,14 @@
2
2
  "devDependencies": {
3
3
  "@types/lodash": "^4.17.12",
4
4
  "@types/node": "^22.7.7",
5
- "@vitest/coverage-v8": "^3.0.5",
5
+ "@vitest/coverage-v8": "^3.2.4",
6
6
  "eslint": "^9.26.0",
7
7
  "lodash": "^4.17.21",
8
8
  "prettier": "^3.3.3",
9
9
  "tsx": "^4.20.3",
10
10
  "typescript": "^5.6.3",
11
11
  "typescript-eslint": "^8.32.1",
12
- "vitest": "^2.1.3",
12
+ "vitest": "^3.2.4",
13
13
  "zod": "^4.0.17"
14
14
  },
15
15
  "peerDependencies": {
@@ -34,9 +34,10 @@
34
34
  "scripts": {
35
35
  "build": "rm -rf dist && yarn lint && tsc -p tsconfig.json",
36
36
  "lint": "prettier --write . && eslint . && yarn tsc",
37
- "npm:publish": "yarn test --run && yarn build && yarn version --patch --no-git-tag-version && yarn publish --non-interactive",
38
- "test": "vitest",
37
+ "npm:publish": "yarn test && yarn build && yarn version --patch --no-git-tag-version && yarn publish --non-interactive",
38
+ "test": "vitest --run",
39
+ "test:watch": "vitest",
39
40
  "test:coverage": "rm -rf coverage && vitest --run --coverage"
40
41
  },
41
- "version": "1.0.18"
42
+ "version": "1.0.20"
42
43
  }