resolvers icon indicating copy to clipboard operation
resolvers copied to clipboard

Yup validation schema does not work with nested objects

Open nareshbhatia opened this issue 6 years ago • 6 comments

I modified the yup schema validation example to use nested objects. However validations are not working for nested objects.

Please see my CodeSandbox

I know there was a related issue (https://github.com/react-hook-form/react-hook-form/issues/893) in January, however I am not sure if it was fixed.

I am using the latest versions except for @hookform/resolvers. Looks like v1.00 has some API changes, so reverted back to v0.0.3 which was used in the original example.

My project uses nested objects very heavily, so it is important to get this working. Please help!

nareshbhatia avatar Oct 04 '20 16:10 nareshbhatia

Hi @nareshbhatia !

Your schema validation works well ! :)

Your schema is:

const SignupSchema = yup.object().shape({
  person: yup.object().shape({
    firstName: yup.string().required(),
    age: yup.number().required().positive().integer(),
    website: yup.string().url()
  })
});

So to display errors you should do:

<div>
    <label>First Name</label>
    <input type="text" name="person.firstName" ref={register} />
    {errors.person?.firstName && <p>{errors.person?.firstName.message}</p>}
</div>

instead of:

<div>
    <label>First Name</label>
    <input type="text" name="person.firstName" ref={register} />
    {errors.firstName && <p>{errors.firstName.message}</p>}
</div>

You have forgotten person => errors.person?.firstName

➡️ https://codesandbox.io/s/react-hook-form-v6-validationschema-forked-q7tlg

jorisre avatar Oct 04 '20 18:10 jorisre

Wow, how could I miss that? Embarrassed :-). Thank you so much @jorisre, you made my day!

nareshbhatia avatar Oct 04 '20 23:10 nareshbhatia

thank you @jorisre 🙏 ❤️

bluebill1049 avatar Oct 05 '20 07:10 bluebill1049

Hi @nareshbhatia !

Your schema validation works well ! :)

Your schema is:

const SignupSchema = yup.object().shape({
  person: yup.object().shape({
    firstName: yup.string().required(),
    age: yup.number().required().positive().integer(),
    website: yup.string().url()
  })
});

So to display errors you should do:

<div>
    <label>First Name</label>
    <input type="text" name="person.firstName" ref={register} />
    {errors.person?.firstName && <p>{errors.person?.firstName.message}</p>}
</div>

instead of:

<div>
    <label>First Name</label>
    <input type="text" name="person.firstName" ref={register} />
    {errors.firstName && <p>{errors.firstName.message}</p>}
</div>

You have forgotten person => errors.person?.firstName

➡️ https://codesandbox.io/s/react-hook-form-v6-validationschema-forked-q7tlg

I have been dealing with same problem. Thank you!

SinaBYR avatar Aug 15 '21 08:08 SinaBYR

Hi. Had same issue and was resolved, but to only one level deep of object. I have...

const NewPackSchema = yup.object().shape({ reference: yup.string().required("Reference is a required field"), createdById: yup.string().required("Created by is required"), requestDetail: yup.object().shape({ reference: yup.string().required("REQ Reference is a required field"), applicantDetail: yup.object().shape({ fullName: yup.string().required("Fullname is a required field"), }), }), });

and get error TypeError: Cannot read properties of undefined (reading 'message')

on ... <TextField label="Fullname" {...register("requestDetail.applicantDetail.fullName")} error={ errors.requestDetail?.applicantDetail?.fullName ? true : false} />

... help would be appreciated, thanks.

Biz-e-Services avatar Jul 22 '22 07:07 Biz-e-Services

@Biz-e-Services Hello, I find similar difficulties. I found a solution using get method of lodash library, This function gets the value at path of object first import get method from lodash

import { get } from 'lodash'

then ,

<TextField label="Fullname" {...register("requestDetail.applicantDetail.fullName")} 
error={ get(errors, '"requestDetail.applicantDetail.fullName") && error['message'] } />

clean code:

let fullname = "requestDetail.applicantDetail.fullName"
<TextField label="Fullname" {...register(fullname)}  error={ get(errors, fullname) && error['message'] } />

mahfuzur avatar Apr 10 '23 00:04 mahfuzur