why zod doesn't make no empty string by default for required fields? #2874
Replies: 4 comments
-
A required field just means that the value must exist and must not be If you found my answer satisfactory, please consider supporting me. Even a small amount is greatly appreciated. Thanks friend! 🙏 |
Beta Was this translation helpful? Give feedback.
-
Actually I know I can use min(1, "Required") to validate it. it's just that I'm confused why it's not made by default because in case the default value of an input is an empty string and the required validation in zod allows it to pass. if we don't set the default value of an input as an empty string, this warning will occur: A component changed an uncontrolled input to a controlled one. This is likely caused by changing a value from undefined to a defined value, which should not happen. Decide between using controlled or uncontrolled input elements during the lifetime of the component. |
Beta Was this translation helpful? Give feedback.
-
Zod's purpose was not to help with React Hook Form. So it's not Zod's job to cater to how another library works. Zod's purpose was to be TypeScript compatible. So I don't imagine Zod will ever consider an empty string to be the same as If you are looking for an explanation of the reasoning, then I'll move this to discussions. If you are wanting Zod to change how it handles this, then I'll leave this as an open issue. Which would you like? |
Beta Was this translation helpful? Give feedback.
-
Actually, this applies not only to react-hook forms but also to controlled inputs (inputs with values set via state). Yes, I understand your explanation. And I also already know the solution by using min(1, "Required"). I just felt that it would be more effective if done by default. It's fine if you want to keep this as an open issue or move it to the discussion. i am fine with either one. |
Beta Was this translation helpful? Give feedback.
-
I am using react hook form and zod as a validator resolver.
SignInValidator is passes even if the password is an empty string. the default value is empty string, if I don't create an empty string at the default value, a Warning will occur: A component changed an uncontrolled input to a controlled one. This is likely caused by changing a value from undefined to a defined value, which should not happen. Decide between using controlled or uncontrolled input elements during the lifetime of the component.
why zod doesn't make noEmptystring by default for required fields?
Beta Was this translation helpful? Give feedback.
All reactions