Shawn
Shawn

Reputation: 351

Formik and Yup : TypeError: Cannot read property 'object' of undefined

I am new to React and was trying out formik with yup for the validation. I am currently getting the error below:

TypeError: Cannot read property 'object' of undefined

with this code:

validationSchema: Yup.object().shape({
  firstName: Yup.string().required()
}),

I am using all the latest versions of formik,react and yup. The versions are

"yup": "^0.25.1" "formik": "^0.11.11", "react": "^16.4.0", "react-dom": "^16.4.0",

Could some one help me to resolve this issue?

It is replicated here https://codesandbox.io/s/lrowpj8pq7

Thanks!

Upvotes: 20

Views: 20158

Answers (4)

Rafdro
Rafdro

Reputation: 790

For me registering desired field helped:

{...register('birthDate')}

Upvotes: 0

Mohd Qasim
Mohd Qasim

Reputation: 1010

in my case I forgot to export validationSchema function, and access in other file which makes to cause undefined for erros.userName

Upvotes: 0

ranjit kumar Patel
ranjit kumar Patel

Reputation: 21

I had the same problem and then I realised I was using yup in the code instead of Yup. Below is the code sample and it works perfectly :)

import * as Yup from "yup";

const formSchema = Yup.object().shape({
  email: Yup.string().required().email(),
  password: Yup.string().required().min(6),
});

Upvotes: 1

Panos Bechlivanos
Panos Bechlivanos

Reputation: 1366

The correct answer is not to downgrade, but to change how you import it.

Try import * as Yup from 'yup' instead of import Yup from 'yup'.

// wrong
import Yup from 'yup';

// correct
import * as Yup from 'yup';

Here is your example working: https://codesandbox.io/s/xlnw2x0kk4.

Upvotes: 135

Related Questions