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

number().required().nullable() #1946

Closed
ennoeller opened this issue Mar 16, 2023 · 6 comments
Closed

number().required().nullable() #1946

ennoeller opened this issue Mar 16, 2023 · 6 comments

Comments

@ennoeller
Copy link

Would like some clarification on a possible issue. When I mark my schema property as number().required().nullable() then I can not have the property value as null/no value. I would like to initiate a form with a numeric value "unset" or on an existing form change the value to null (temporarily till I edit the form). It would then be expected, that the form will not validate if a value is null, yet I can have an empty field while initiating or editing the form.

Would it classify as a bug or how could I achieve this?

@ennoeller
Copy link
Author

I have made a workaround:
property: number().nullable()
.test({name: 'required', test: (value) => isPresent(value)})

but I still think this is how number().required().nullable() should work together

@jquense
Copy link
Owner

jquense commented Mar 16, 2023

see the section about nullability: #1906

@jquense jquense closed this as completed Mar 16, 2023
@Manny651
Copy link

see the section about nullability: #1906

@jquense the migration guide only explains a migration path in the case of string cast, which is not the case here. The use case where a field is unset with a null value is fairly common, and I fail to see how the migration guide helps to migrate to the "new patterns". Is the workaround provided by ennoeller (rewrite a required function) the best way to solve that case?

@jquense
Copy link
Owner

jquense commented Mar 20, 2023

@Manny651 did you read the section? it applies to all schema and not just strings, it also outlines a migration path. As far as alternatives, they have been discussed a few times in the issues. You might want to use deepPartial or the outlined cast option, or tweak TS types, or transform the null values into placeholders. Which alternative works for you is very dependent on what you're doing and what your needs are, I can't tell you how to handle it. In my experience if your just trying to to cast a value to use in a form that a user must fill out, deepPartial works great for that.

@Manny651
Copy link

@jquense I think I misstated the problem: the doc section is all about the functions about casting and copying the data, but the issue here is encountered when trying to call the validate function of a schema. The section explains that the validate() will fail in that case, but provides only workaround related to the cast(). I'm aware that validate uses cast internally, but I do not understand how any of this helps when trying to migrate code which only uses validate.

@jquense
Copy link
Owner

jquense commented Mar 21, 2023

Hmm I'm not sure I understand what your asking for then. validating a nullable required schema has always resulted in an error, saying the field is missing.

The only behavior change in v1 is for casting. If what your saying is you want a "this field is required" error instead of "field is the wrong type null" error, then I'd suggest changing the type error on your schema to say the field is required when the value is null

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

3 participants