zod 4.3.0 → 4.3.1

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.3.0",
3
+ "version": "4.3.1",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "author": "Colin McDonnell <zod@colinhacks.com>",
@@ -602,14 +602,31 @@ test("index signature in shape", () => {
602
602
  expectTypeOf<schema>().toEqualTypeOf<Record<string, string>>();
603
603
  });
604
604
 
605
- test("extent() on object with refinements should throw", () => {
605
+ test("extend() on object with refinements should throw when overwriting properties", () => {
606
606
  const schema = z
607
607
  .object({
608
608
  a: z.string(),
609
609
  })
610
610
  .refine(() => true);
611
611
 
612
- expect(() => schema.extend({ b: z.string() })).toThrow();
612
+ expect(() => schema.extend({ a: z.number() })).toThrow();
613
+ });
614
+
615
+ test("extend() on object with refinements should not throw when adding new properties", () => {
616
+ const schema = z
617
+ .object({
618
+ a: z.string(),
619
+ })
620
+ .refine((data) => data.a.length > 0);
621
+
622
+ // Should not throw since 'b' doesn't overlap with 'a'
623
+ const extended = schema.extend({ b: z.number() });
624
+
625
+ // Verify the extended schema works correctly
626
+ expect(extended.parse({ a: "hello", b: 42 })).toEqual({ a: "hello", b: 42 });
627
+
628
+ // Verify the original refinement still applies
629
+ expect(() => extended.parse({ a: "", b: 42 })).toThrow();
613
630
  });
614
631
 
615
632
  test("safeExtend() on object with refinements should not throw", () => {
@@ -656,7 +656,14 @@ export function extend(schema: schemas.$ZodObject, shape: schemas.$ZodShape): an
656
656
  const checks = schema._zod.def.checks;
657
657
  const hasChecks = checks && checks.length > 0;
658
658
  if (hasChecks) {
659
- throw new Error("Object schemas containing refinements cannot be extended. Use `.safeExtend()` instead.");
659
+ // Only throw if new shape overlaps with existing shape
660
+ // Use getOwnPropertyDescriptor to check key existence without accessing values
661
+ const existingShape = schema._zod.def.shape;
662
+ for (const key in shape) {
663
+ if (Object.getOwnPropertyDescriptor(existingShape, key) !== undefined) {
664
+ throw new Error("Cannot overwrite keys on object schemas containing refinements. Use `.safeExtend()` instead.");
665
+ }
666
+ }
660
667
  }
661
668
 
662
669
  const def = mergeDefs(schema._zod.def, {
@@ -665,7 +672,6 @@ export function extend(schema: schemas.$ZodObject, shape: schemas.$ZodShape): an
665
672
  assignProp(this, "shape", _shape); // self-caching
666
673
  return _shape;
667
674
  },
668
- checks: [],
669
675
  });
670
676
  return clone(schema, def) as any;
671
677
  }
@@ -1,5 +1,5 @@
1
1
  export const version = {
2
2
  major: 4,
3
3
  minor: 3,
4
- patch: 0 as number,
4
+ patch: 1 as number,
5
5
  } as const;
package/v4/core/util.cjs CHANGED
@@ -440,7 +440,14 @@ function extend(schema, shape) {
440
440
  const checks = schema._zod.def.checks;
441
441
  const hasChecks = checks && checks.length > 0;
442
442
  if (hasChecks) {
443
- throw new Error("Object schemas containing refinements cannot be extended. Use `.safeExtend()` instead.");
443
+ // Only throw if new shape overlaps with existing shape
444
+ // Use getOwnPropertyDescriptor to check key existence without accessing values
445
+ const existingShape = schema._zod.def.shape;
446
+ for (const key in shape) {
447
+ if (Object.getOwnPropertyDescriptor(existingShape, key) !== undefined) {
448
+ throw new Error("Cannot overwrite keys on object schemas containing refinements. Use `.safeExtend()` instead.");
449
+ }
450
+ }
444
451
  }
445
452
  const def = mergeDefs(schema._zod.def, {
446
453
  get shape() {
@@ -448,7 +455,6 @@ function extend(schema, shape) {
448
455
  assignProp(this, "shape", _shape); // self-caching
449
456
  return _shape;
450
457
  },
451
- checks: [],
452
458
  });
453
459
  return clone(schema, def);
454
460
  }
package/v4/core/util.js CHANGED
@@ -382,7 +382,14 @@ export function extend(schema, shape) {
382
382
  const checks = schema._zod.def.checks;
383
383
  const hasChecks = checks && checks.length > 0;
384
384
  if (hasChecks) {
385
- throw new Error("Object schemas containing refinements cannot be extended. Use `.safeExtend()` instead.");
385
+ // Only throw if new shape overlaps with existing shape
386
+ // Use getOwnPropertyDescriptor to check key existence without accessing values
387
+ const existingShape = schema._zod.def.shape;
388
+ for (const key in shape) {
389
+ if (Object.getOwnPropertyDescriptor(existingShape, key) !== undefined) {
390
+ throw new Error("Cannot overwrite keys on object schemas containing refinements. Use `.safeExtend()` instead.");
391
+ }
392
+ }
386
393
  }
387
394
  const def = mergeDefs(schema._zod.def, {
388
395
  get shape() {
@@ -390,7 +397,6 @@ export function extend(schema, shape) {
390
397
  assignProp(this, "shape", _shape); // self-caching
391
398
  return _shape;
392
399
  },
393
- checks: [],
394
400
  });
395
401
  return clone(schema, def);
396
402
  }
@@ -4,5 +4,5 @@ exports.version = void 0;
4
4
  exports.version = {
5
5
  major: 4,
6
6
  minor: 3,
7
- patch: 0,
7
+ patch: 1,
8
8
  };
@@ -1,5 +1,5 @@
1
1
  export const version = {
2
2
  major: 4,
3
3
  minor: 3,
4
- patch: 0,
4
+ patch: 1,
5
5
  };