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

Failed to validate union objects #127

Closed
focus7eleven opened this issue Aug 25, 2020 · 1 comment
Closed

Failed to validate union objects #127

focus7eleven opened this issue Aug 25, 2020 · 1 comment

Comments

@focus7eleven
Copy link

First of all: thanks for great contribution to this package, it's really awesome.

However, I ran into an issue when I try to validate a union of objects, which simply looks like this:

const extensions = z.object({
  name: z.string(),
})

const dog = z.object({
  extensions
});

const cat = z.object({
  extensions
});

const animal = z.union([dog, cat]);

// it should output type error because name is ought to be type of string
const validateResult = animal.parse({ extensions: { name: 123 } });  

And I expected the validateResult to contain errors but instead it didn't.

And when I changed the code to:

- const extensions = z.object({
-   name: z.string(),
- })

const dog = z.object(
-  extensions
+  extensions: z.object({
+    name: z.string(),
+  })
});

const cat = z.object({
-  extensions
+  extensions: z.object({
+    name: z.string(),
+  })
});

const animal = z.union([dog, cat]);

// it should output type error because name is ought to be type of string
const validateResult = animal.parse({ extensions: { name: 123 } });  

It surprisingly worked!!

Really need your help. Appreciate it!

@colinhacks
Copy link
Owner

Fixed in zod@1.11. Thanks for reporting this!

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

No branches or pull requests

2 participants