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

Difference between let schema = yup.object().shape({}) and let schema = yup.object({}) #1222

Closed
mattsputnikdigital opened this issue Jan 15, 2021 · 7 comments

Comments

@mattsputnikdigital
Copy link

What is the difference between let schema = yup.object().shape({}) and let schema = yup.object({})

I can do either when setting up schema for React / Formik and both seems to work the same way.

@jquense
Copy link
Owner

jquense commented Jan 15, 2021

there is no difference, the latter is a shortcut for the first one

@jquense jquense closed this as completed Jan 15, 2021
@andrewcbuensalida
Copy link

Then why have the long version?

@lucamartins
Copy link

With the long version yup.object().shape() I get some errors when extracting and dealing with the type extracted by yup.InferType, I don't understand why with the short version those errors disappeared

@stefpiatek
Copy link

I've found that the yup.InferType only works correctly on the yup.object({}), where the inferred type of the longer method ends up being any.

@Ivanrenes
Copy link

there is no difference, the latter is a shortcut for the first one

There is different because InferType complains with .shape().

@JortsEnjoyer0
Copy link

Then why have the long version?

The actual answer is that .shape() will change an existing object schema whereas .object() sets it entirely. Use shape for schema changes down the line and object for the initial structure.

@madhurtrivedi
Copy link

madhurtrivedi commented Apr 20, 2024

Another purpose of using .shape({Field1: yup....., Field2: yup.....}, [[Field1, Field2], [Field3, Field4]]) is to validate fields conditionally. For e.g. When the Field1 === "Use Custom Option" then Field2 is required. Another e.g. can be Field3 === 5 then Field4 is required and Field4 === 7 then Field3 is required. This dependency of fields can be grouped inside of second parameter of shape which is an array, like in above case Field1 & Field2 are dependent and Field3 & Field4 are dependent.

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

8 participants