zod 4.0.8 → 4.0.9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zod",
3
- "version": "4.0.8",
3
+ "version": "4.0.9",
4
4
  "type": "module",
5
5
  "author": "Colin McDonnell <zod@colinhacks.com>",
6
6
  "description": "TypeScript-first schema declaration and validation library with static type inference",
@@ -134,3 +134,48 @@ test("non-aborted errors", () => {
134
134
  }
135
135
  `);
136
136
  });
137
+
138
+ test("surface continuable errors only if they exist", () => {
139
+ const schema = z.union([z.boolean(), z.uuid(), z.jwt()]);
140
+
141
+ expect(schema.safeParse("asdf")).toMatchInlineSnapshot(`
142
+ {
143
+ "error": [ZodError: [
144
+ {
145
+ "code": "invalid_union",
146
+ "errors": [
147
+ [
148
+ {
149
+ "expected": "boolean",
150
+ "code": "invalid_type",
151
+ "path": [],
152
+ "message": "Invalid input: expected boolean, received string"
153
+ }
154
+ ],
155
+ [
156
+ {
157
+ "origin": "string",
158
+ "code": "invalid_format",
159
+ "format": "uuid",
160
+ "pattern": "/^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000)$/",
161
+ "path": [],
162
+ "message": "Invalid UUID"
163
+ }
164
+ ],
165
+ [
166
+ {
167
+ "code": "invalid_format",
168
+ "format": "jwt",
169
+ "path": [],
170
+ "message": "Invalid JWT"
171
+ }
172
+ ]
173
+ ],
174
+ "path": [],
175
+ "message": "Invalid input"
176
+ }
177
+ ]],
178
+ "success": false,
179
+ }
180
+ `);
181
+ });
@@ -1911,7 +1911,7 @@ function handleUnionResults(results: ParsePayload[], final: ParsePayload, inst:
1911
1911
  }
1912
1912
 
1913
1913
  const nonaborted = results.filter((r) => !util.aborted(r));
1914
- if (nonaborted.length > 0) {
1914
+ if (nonaborted.length === 1) {
1915
1915
  final.value = nonaborted[0].value;
1916
1916
  return nonaborted[0];
1917
1917
  }
@@ -2781,6 +2781,9 @@ export const $ZodLiteral: core.$constructor<$ZodLiteral> = /*@__PURE__*/ core.$c
2781
2781
  "$ZodLiteral",
2782
2782
  (inst, def) => {
2783
2783
  $ZodType.init(inst, def);
2784
+ if (def.values.length === 0) {
2785
+ throw new Error("Cannot create literal schema with no valid values");
2786
+ }
2784
2787
 
2785
2788
  inst._zod.values = new Set<util.Literal>(def.values);
2786
2789
  inst._zod.pattern = new RegExp(
@@ -1,5 +1,5 @@
1
1
  export const version = {
2
2
  major: 4,
3
3
  minor: 0,
4
- patch: 8 as number,
4
+ patch: 9 as number,
5
5
  } as const;
@@ -863,7 +863,7 @@ function handleUnionResults(results, final, inst, ctx) {
863
863
  }
864
864
  }
865
865
  const nonaborted = results.filter((r) => !util.aborted(r));
866
- if (nonaborted.length > 0) {
866
+ if (nonaborted.length === 1) {
867
867
  final.value = nonaborted[0].value;
868
868
  return nonaborted[0];
869
869
  }
@@ -1354,6 +1354,9 @@ exports.$ZodEnum = core.$constructor("$ZodEnum", (inst, def) => {
1354
1354
  });
1355
1355
  exports.$ZodLiteral = core.$constructor("$ZodLiteral", (inst, def) => {
1356
1356
  exports.$ZodType.init(inst, def);
1357
+ if (def.values.length === 0) {
1358
+ throw new Error("Cannot create literal schema with no valid values");
1359
+ }
1357
1360
  inst._zod.values = new Set(def.values);
1358
1361
  inst._zod.pattern = new RegExp(`^(${def.values
1359
1362
  .map((o) => (typeof o === "string" ? util.escapeRegex(o) : o ? util.escapeRegex(o.toString()) : String(o)))
@@ -832,7 +832,7 @@ function handleUnionResults(results, final, inst, ctx) {
832
832
  }
833
833
  }
834
834
  const nonaborted = results.filter((r) => !util.aborted(r));
835
- if (nonaborted.length > 0) {
835
+ if (nonaborted.length === 1) {
836
836
  final.value = nonaborted[0].value;
837
837
  return nonaborted[0];
838
838
  }
@@ -1323,6 +1323,9 @@ export const $ZodEnum = /*@__PURE__*/ core.$constructor("$ZodEnum", (inst, def)
1323
1323
  });
1324
1324
  export const $ZodLiteral = /*@__PURE__*/ core.$constructor("$ZodLiteral", (inst, def) => {
1325
1325
  $ZodType.init(inst, def);
1326
+ if (def.values.length === 0) {
1327
+ throw new Error("Cannot create literal schema with no valid values");
1328
+ }
1326
1329
  inst._zod.values = new Set(def.values);
1327
1330
  inst._zod.pattern = new RegExp(`^(${def.values
1328
1331
  .map((o) => (typeof o === "string" ? util.escapeRegex(o) : o ? util.escapeRegex(o.toString()) : String(o)))
@@ -4,5 +4,5 @@ exports.version = void 0;
4
4
  exports.version = {
5
5
  major: 4,
6
6
  minor: 0,
7
- patch: 8,
7
+ patch: 9,
8
8
  };
@@ -1,5 +1,5 @@
1
1
  export const version = {
2
2
  major: 4,
3
3
  minor: 0,
4
- patch: 8,
4
+ patch: 9,
5
5
  };