react-hook-form resolver type error using Yup

kevin

I'm creating an auth form and I'm referencing the react-hook-form docs on how to use yup to validate my inputs. As far as I can tell, everything is pretty much identical to the implementation provided in the docs. However, I'm getting the error on the useForm resolver (show below). Any idea where I'm going wrong?

  • "react-hook-form": "^7.15.2"
  • "yup": "^0.32.9"
  • "@hookform/resolvers": "^2.8.1"

error

enter image description here

code

interface IFormInputs {
  email: string;
  password: string;
  passwordConfirm: string;
}

const schema = yup.object().shape({
  email: yup.string().required(),
  password: yup.string().required(),
  passwordConfirm: yup.string().required(),
});

const SignUp = (): JSX.Element => {
  const {
    control,
    handleSubmit,
    getValues,
    formState: { errors },
  } = useForm<IFormInputs>({ resolver: yupResolver(schema) });

  const onSubmit: SubmitHandler<IFormInputs> = () => {
    const values = getValues();
    console.log('values', values);
  };

  return (
    <div>
      <StyledPaper>
        <StyledTypography>Sign Up</StyledTypography>
        <form onSubmit={handleSubmit(onSubmit)}>
          <Controller
            name="email"
            rules={{ required: 'this field is required' }}
            defaultValue=""
            control={control}
            render={({ field }) => (
              <TextField
                fullWidth
                label="Email"
                variant="filled"
                value={field.value}
                error={!!errors.email}
                helperText="Provide an email"
                {...field}
              />
            )}
          />
          <StyledButton type="submit">Submit</StyledButton>
        </form>
      </StyledPaper>
      <div>
        <StyledTypography>Already have an account? Log in.</StyledTypography>
      </div>
    </div>
  );
};

export default SignUp;
NearHuscarl

This looks like a library bug, downgrade @hookform/resolvers to 2.8.0 seems to fix the issue.

Codesandbox Demo

Edit: You can remove the error on version 2.8.1 by forcing yubResolver to use the yub.AnyObjectSchema generic. Thanks to Mihai-github for figuring that out.

useForm<IFormInputs>({
  resolver: yupResolver<yup.AnyObjectSchema>(schema),
});

Codesandbox Demo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

react-hook-form yup resolver, reactStrap problem resolving error from child component

Validating File using React-Hook-Form and Yup (<x> must be a `object` type, but the final value was: `null`)

Using the MUI DatePicker with yup and react-hook-form - the error prop doesn't work as intended

Validating a child input type file with react-hook-form and Yup

How to use yup with react hook form?

React yup validation with and react-hook-form, array of different type of objects depends from one of object attribute

How can I validate an Autocomplete multiple TextField using React Hook Form and Yup?

How to solve the issue of unfocused Input created dynamically using react-hook-form with yup

Validation using react hook form results in an error

Yup & React Hook Form: How to validate onChange rather than onSubmit

Validation with yup failed for generic react hook form component

Reactjs and Yup, problem with react-hook-form integration

Conditional Validation with react-hook-form and yup for a radio button

Yup validation on Select field (dropdown) react-hook-form not working

React hook form useFormContext generating "argument of type 'string | undefined'" error

How to show error message using react-hook-form in react

Accessing error from react-hook-form using reactstrap

react-hook-form, error when using for data update

TypeScript error when using register function of react-hook-form

react-hook-form custom resolver only checking after submit

How to show error messages in react yup form validations without formik

How to call a method on form submission if there is a validation error using Formik + Yup?

How to show Form error summary using Formik with yup validationSchema

How to use Yup validation schema with the Controller component in `react-hook-form` lib

React-hook-form validation when input is empty. Conflict with Yup.matches

react-hook-form register type conflict

Simple react form validation with yup

React Hook Form - Typescript issues and error handling?

Trouble with error message in react-hook-form