react-hook-form: errors inferred type is incorrect when using yup notRequired
Having a schema like this
const schema = yup.object().shape({
smtpInfo: yup
.object({
host: yup.string().required(),
port: yup.string().required(),
user: yup.string().required(),
password: yup.string().required(),
secure: yup.boolean(),
}).notRequired()
when inferring the form data type
type FormData = yup.InferType<typeof schema>;
the errors type returning from
const { handleSubmit, errors, formState, register, reset, watch } = useForm<
FormData
>({
validationSchema: schema,
});
is incorrect
errors.smtpInfo returns a FieldError | undefined whereas removing the notRequired from the schema it returns a NestDataObject<{...}, FieldError> | undefined
Expected behavior The returned type should still be the nested object with all the fields, since it could be undefined one can add a check for that.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 17 (7 by maintainers)
Yes. This will be fixed in V6. https://codesandbox.io/s/ts-react-hook-form-validated-submit-5yoj7?file=/src/App.tsx
@bluebill1049 Should I apply this fix to V5?
@krish-dev The last version working is
0.28.3OK so I guess I will have to do a mix between the 2 😕. Something Like this
Thanks @kotarella1110 for the response
@tafelito You cannot use
yup.InferTypeif it contains object or array value inFormData. You have to define the type yourself usingNestedValue.awesome! no, let’s roll this out in V6. 👍
I’m using version
6.0.8I have a very simple login form that does not have a nested object or array. However, I’m getting the same errorHere is my simple login form code.