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

[Bug] Validation fails when using undefined primitive in non-optional property #239

Closed
tvillaren opened this issue May 12, 2024 · 2 comments · Fixed by #240
Closed

[Bug] Validation fails when using undefined primitive in non-optional property #239

tvillaren opened this issue May 12, 2024 · 2 comments · Fixed by #240
Labels
bug Something isn't working

Comments

@tvillaren
Copy link
Collaborator

tvillaren commented May 12, 2024

Input

export type User = {
  username: string | undefined
}

Expected output

// Generated by ts-to-zod
import { z } from "zod";

export const userSchema = z.object({
  username: z.union([z.string(), z.undefined()]),
});

Actual output

✖ Validating generated types
Error: 'userSchema' is not compatible with 'User':
Argument of type '{ username?: string | undefined; }' is not assignable to parameter of type 'User'.
  Property 'username' is optional in type '{ username?: string | undefined; }' but required in type 'User'.

Originally posted by @johnnyp-gg in #203 (comment)

@tvillaren tvillaren added the bug Something isn't working label May 12, 2024
@tvillaren tvillaren changed the title [Bug] Validation fails when using undefined literal in non-optional property [Bug] Validation fails when using undefined primitive in non-optional property May 13, 2024
@tvillaren
Copy link
Collaborator Author

The validation error you're getting comes from a known bug in Zod inference (colinhacks/zod#2464)
To be checked if we can ignore it (as for #140)

@johnnyp-gg
Copy link

Thanks @tvillaren, its working fine for me now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants