Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(v3): dirty union bug #3727

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

mmkal
Copy link
Contributor

@mmkal mmkal commented Aug 26, 2024

bug repro: https://stackblitz.com/edit/stackblitz-starters-swdj3e?file=index.test.js&startScript=test

Basically, zod v3 wrongly reports a "normal" error rather than a union error for a union of strict objects (and presumably other unions that set status=dirty:

import { test, expect } from 'vitest';
import { z } from 'zod';

test('hmm', () => {
  const union = z.union([
    z.object({ x: z.number() }).strict(),
    z.object({ y: z.number() }).strict(),
  ]);

  expect(union.safeParse({ x: 1, y: 1 })).toMatchInlineSnapshot(`
    {
      "error": [ZodError: [
      {
        "code": "unrecognized_keys",
        "keys": [
          "y"
        ],
        "path": [],
        "message": "Unrecognized key(s) in object: 'y'"
      }
    ]],
      "success": false,
    }
  `);
  expect(union.safeParse({ x: 1, y: 1 })).toMatchObject({
    success: false,
    error: {
      issues: [
        {
          code: 'invalid_union',
          unionErrors: [expect.any(z.ZodError), expect.any(z.ZodError)],
        },
      ],
    },
  });
});

Note: this does not repro in v4. I'm not sure if there's anything going into zod v3 still but opening this against main in hopes that it can get fixed in v3.

@mmkal mmkal changed the title fix: dirty union bug fix(v3): dirty union bug Aug 26, 2024
Copy link

netlify bot commented Aug 26, 2024

Deploy Preview for guileless-rolypoly-866f8a ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 3b3af19
🔍 Latest deploy log https://app.netlify.com/sites/guileless-rolypoly-866f8a/deploys/66ce259bc951de0008a45670
😎 Deploy Preview https://deploy-preview-3727--guileless-rolypoly-866f8a.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant