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

getFieldMeta does not return errors of nested field objects via yup schema #2956

Open
TomasJerrySebo opened this issue Dec 9, 2020 · 11 comments

Comments

@TomasJerrySebo
Copy link

Bug report

Current Behavior

After an update of Yup from 0.27.0 -> 0.29.3, yup introduced the following change jquense/yup#539 and this probably caused the following use case:

Given we have a yup schema validation with a nested object with a set of validation, the useFormikContext returns the form errors as a flat object previously set by yup schema.

Screenshot 2020-12-09 at 21 47 09

this flat error structure causes that now the getFieldMeta does not return any nested object, array errors since it expects that the errors are nested.

The following function fails to return the nested error:

https://github.com/formium/formik/blob/83c0299a7e287e16e1b2ee2c5d54328f6f2fc7c3/packages/formik/src/Formik.tsx#L900

because the getIn function only loops for nested errors.

https://github.com/formium/formik/blob/83c0299a7e287e16e1b2ee2c5d54328f6f2fc7c3/packages/formik/src/utils.ts#L77

Expected behavior

getFieldMeta returns the nested field object/array errors defined via yup validation schema.

Reproducible example

Suggested solution(s)

Update getIn function to return also errors via nested keys.

Additional context

Your environment

Software Version(s)
Formik 2.1.3
React 16.13.1
TypeScript 4.1.2.
Browser Chrome Version 87.0.4280.88
npm/Yarn 6.13.4
Operating System macOS Catalina 10.15.4
Yup 0.29.3
@TomasJerrySebo TomasJerrySebo changed the title getFieldMeta does not return errors of nested field object via yup schema getFieldMeta does not return errors of nested field objects via yup schema Dec 9, 2020
@niksumeiko
Copy link

We have the same issue after YUP update. With the following YUP schema, errors coming from useFormikContext() are flat for additionalInfo.referenceText input field.

object().shape({
    'additionalInfo.referenceText': object().shape({
        value: string()
            .required(Translation.t('form.validation.referenceTextRequired'))
            .max(30, Translation.t('form.validation.referenceTextTooLong', { length: 30 })),
    }),
});
const { errors } = useFormikContext();

console.log(errors);
// {
//    additionalInfo.referenceText: "Reference text is required"
// }

Before YUP update, it's structure was as follows:

{
    "additionalInfo": {
        "referenceText": "Reference text is required"
    }
}

And we had no problem retrieving individual form field error via getFieldMeta():

const { error } = getFieldMeta('additionalInfo.referenceText');

console.log(error);
// Reference text is required

@github-actions
Copy link
Contributor

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days

@github-actions github-actions bot added the stale label Jan 10, 2021
@niksumeiko
Copy link

Maybe this is because of YUP latest update?

@johnrom johnrom removed the stale label Jan 18, 2021
@github-actions
Copy link
Contributor

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days

@github-actions github-actions bot added the stale label Feb 18, 2021
@eranimo
Copy link

eranimo commented Mar 21, 2021

This is still an issue with yup 0.32.9. Actually, it's still broken for me in 0.27.0. Are you sure this is caused by yup?

@github-actions github-actions bot removed the stale label Mar 22, 2021
@github-actions
Copy link
Contributor

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days

@github-actions github-actions bot added the stale label Apr 22, 2021
@johnrom johnrom removed the stale label Apr 22, 2021
@github-actions
Copy link
Contributor

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days

@github-actions github-actions bot added the stale label May 23, 2021
@johnrom johnrom removed the stale label May 24, 2021
@github-actions
Copy link
Contributor

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days

@github-actions github-actions bot added the stale label Jun 24, 2021
@FranciscoMendes10866
Copy link

FranciscoMendes10866 commented Aug 30, 2022

Did anyone have a solution? I'm having the exact same problem in a project 🥲

@ghost
Copy link

ghost commented Nov 1, 2022

Yes found same issue here

@4nduril
Copy link

4nduril commented Jul 18, 2023

Definitely not a Yup issue. I am using validate with my own validation and I also can't access validation errors on Field level via meta.error. Errors for nested fields show up flattened with dotted field names in the form's error object instead of a nested object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants