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

validateSync() returns invalid paths for nested errors in some cases where field names contain "." (period) #2156

Closed
ir0nCr055 opened this issue Jan 3, 2024 · 0 comments

Comments

@ir0nCr055
Copy link
Contributor

ir0nCr055 commented Jan 3, 2024

Describe the bug

The validateSync() function may return invalid error paths for some nested object field names containing "." (period).

Namely, the invalid paths returned are missing quotes.

To Reproduce

Given the following schema and object...

const schema = yup.object({
    "i.t._admin_name": yup.string().required("Enter a valid name"),
    "i.t._admin_email": yup
      .string()
      .required("Enter a valid email")
      .test("valid_email", "Email is invalid", (val) => {
        return false; // This test always fails to simplify the demonstration
      }),
  });

  // This example demonstrates the bug - we expect errors for both keys.
  const obj = {
    "i.t._admin_name": "",
    "i.t._admin_email": "invalid email",
  };

Evaluating schema.validateSync(obj, { abortEarly: false }) throws a validationError containing the following paths:

  • ["i.t._admin_name"]
  • [i.t._admin_email]

This test case is demonstrated here: https://codesandbox.io/p/devbox/yh65qd

Expected behavior

We'd expect the paths to be:

  • ["i.t._admin_name"]
  • ["i.t._admin_email"]

Platform

All tested browsers, versions including Chrome, Firefox.

Additional context

Bug discovered while integrating Yup with Formik and React.

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

2 participants