Yup schema that should allow null default value, and requires object on submit #10618
-
Hello, I have react-select in react-hook-form controller, that should have null as default value and is required in the form and on submit should have object{value, label}. Before version 3.1.1 of @hookform/resolvers this was not a problem, because the form inputs had their own type, where the null value is acceptable for the select, and the schema did not have nullable(). But now since the schema is source of truth TS complains for the default values if I don't have nullable, but I cannot use nullable since the select is required and should output object. Example working with 3.1.0: const schema = yup.object().shape({category: yup
.object()
.shape({
label: yup.string().required("Tag is required"),
value: yup.string().required("Tag is required"),
})
.required()
});
interface SelectOptionType {
label: string;
value: string;
}
interface InputType {
category: SelectOptionType | null;
}
useForm<InputType>({
resolver: yupResolver(schema),
defaultValues: {
category: null,
}}) With Ver. 3.1.1. I cannot use default value null for category, because it should not be nullable in the yup schema, since it is required and should have object for output |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 4 replies
-
You might need to specify that category is nullable and defined: const schema = yup.object().shape({category: yup
.object()
.shape({
label: yup.string().required("Tag is required"),
value: yup.string().required("Tag is required"),
})
.nullable()
.defined(),
}); |
Beta Was this translation helpful? Give feedback.
-
I was able to deal with this using the asegarra suggestion https://github.com/react-hook-form/resolvers/issues/575#issuecomment-1693405121.I just specified the MyInputType here resolver: yupResolver< InputType>(schema), |
Beta Was this translation helpful? Give feedback.
-
I have concluded, from a philosophical viewpoint, that the data types for a form do not have to match the data types for the database fetch that your data came from. Given that these are two completely separate worlds, this makes sense to me. It may or may not in your use case. Besides the particular use case discussed here, this has given me complete freedom to transform the shape of what I get back from the database into what the form needs, and vice versa. For nullable string fields, for example, this leads me to the following behaviors in my RHF form components:
There might be other complexities with things like optional checkboxes (which I handle in my React component UI), but this basic idea has saved me all sorts of angst about trying to map between what the HTML standard "input" component does and what databases do. |
Beta Was this translation helpful? Give feedback.
-
I had a similar Issue, I was able to solve it with this kind of validation:
|
Beta Was this translation helpful? Give feedback.
I was able to deal with this using the asegarra suggestion https://github.com/react-hook-form/resolvers/issues/575#issuecomment-1693405121.I just specified the MyInputType here
resolver: yupResolver< InputType>(schema),