-
Notifications
You must be signed in to change notification settings - Fork 117
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
Zod optional, nullable or nullish don't work when having previous validations #196
Comments
Hi @wladiston, Thank you for bringing up the issue. This is because zod consider a field to be missing only when the value is Unfortunately there is not much Conform can help at the moment. I have tried stripping empty string to match the zod behaviour in the previous version. But it causes another issue with refinement not running on the object schema as well. References: |
I am re-considering stripping empty string on the zod integration to better align with how zod works and I would like to hear more thoughts on this from the community. |
I guess the main problem here is how to interpret the empty string. I'd say that the best scenario I've seen is when empty strings are interpreted as null. It makes sense for me that if a user clears the field and submit the form, to just save the null on the database instead of saving an empty string |
v0.7.4 introduced a new option to strip empty value on |
This patch refactors the sign up page to create a new profile settings page that allows users to preview what their consumer reviews will look like and update their username, name, email, and password accordingly. Eventually, I should also add a profile picture upload input. This patch takes advantage of Conform's new `skipEmptyValues` option to optionally accept the `password` field in this settings page. Ref: https://github.com/edmundhung/conform/releases/tag/v0.7.4 Ref: edmundhung/conform#224 Ref: edmundhung/conform#196
This patch refactors the sign up page to create a new profile settings page that allows users to preview what their consumer reviews will look like and update their username, name, email, and password accordingly. Eventually, I should also add a profile picture upload input. This patch takes advantage of Conform's new `skipEmptyValues` option to optionally accept the `password` field in this settings page. Ref: https://github.com/edmundhung/conform/releases/tag/v0.7.4 Ref: edmundhung/conform#224 Ref: edmundhung/conform#196
Update: The |
Resolved on v0.8.0. |
Is there a way to not strip empty value? @edmundhung I have a case where I want to update a text input field to an empty value. However, I can't do it since the empty values are stripped |
@milhamm Can you share a bit more why you need to have it as empty string? IMO, stripping empty string is necessary to align with how zod works. How about using a transform to cast it to empty string if the value is undefined? Or simply do |
@edmundhung I also have a case where I would like to submit empty strings. Our backend API has fields that accept empty strings, but cannot accept null values. It feels strange that there is no way I can set up the Zod schema to make this work with Conform. To me it feels like there is a difference between: An empty input -> Should submit an empty string (as web forms currently do). You can always use the .min(1) if you don't want to accept the empty string. I believe this is how it works if you're using Zod in other libraries like react-hook-form. Removing the input from the page (or setting the input to I feel like the transform should be the other way around, and we should be casting empty strings to undefined if that is how we want to interpret empty strings in our database. |
The major concern here is how we can utilise zod optional correctly. If we do not strip out empty string, zod will consider the field to be present and continue running the rest of the validation. Imagine a zod schema like this: Zod will report the field to have an invalid email even though it is empty. There are also other impacts if you tried to inspect all the errors. Imagine the same schema above but it becomes required this time. If the field is empty, zod will only report the required error. But if you use min check, it will run both the min length and email check. These are just some of the examples. But my gut feeling right now is that we should align with how zod works to ensure proper support of all its features. |
I do see the concern. I don't think setting email to optional is the correct schema for this case. I believe the way Zod says this should be handled (taken from the current documentation):
I don't think this schema is supported right now with Conform. I think it is up to the user to transform it into a null if that is how their db wants it, but lots of people might not store it like that). |
@edmundhung I know you're probably answering more questions than usual, but I was wondering if you had an opinion on how to handle Zod's recommendation for optional string validation suggested above? |
I was following along with epic stack and decided to try making the password field on login page optional
I'm not sure if this is a problem with the
@conform-to/zod
only or it extends to the@conform-to/react
but it somehow just ignores the optional in favor of the previous rules.I can easily see some situations where nullish, nullable and optional are important.
As a workaround I had to make it like this
The text was updated successfully, but these errors were encountered: