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

Type inference issue in deeply nested form validation #4735

Closed
sammiya opened this issue Sep 7, 2023 · 1 comment
Closed

Type inference issue in deeply nested form validation #4735

sammiya opened this issue Sep 7, 2023 · 1 comment
Labels
Fixed patch Completed issues that will be published with next patch (1.0.X)

Comments

@sammiya
Copy link

sammiya commented Sep 7, 2023

What package has an issue

@mantine/form

Describe the bug

In the following code, during the validation function for users.tags.tag, the values parameter should contain data of type FormValues. However, it's incorrectly inferred as { tag: string; key: string; }.

import { randomId } from "@mantine/hooks";
import { useForm } from "@mantine/form";

type FormValues = {
  users: {
    name: string;
    key: string;
    tags: {
      tag: string;
      key: string;
    }[];
  }[];
};

export default function App() {
  const initialValues: FormValues = {
    users: [
      {
        name: "Dan Abramov",
        key: randomId(),
        tags: [{ tag: "React", key: randomId() }],
      },
    ],
  };

  const form = useForm({
    initialValues,
    validate: {
      users: {
        name: (value, values, path) => {
          console.log("validate users.name", { value, values, path });
          if (value === "") return "Name is required";
          return null;
        },
        tags: {
          tag: (value, values, path) => {
            // Here, the 'values' actually contains a value of type FormValues, but for some reason, it's inferred as { tag: string; key: string; }
            console.log("validate users.tags.tag", { value, values, path });
            if (value === "") return "Tag is required";

            return null;
          },
        },
      },
    },
  });

  return <>{/* Omitted for brevity */}</>;
}

What version of @mantine/hooks page do you have in package.json?

6.0.20

If possible, please include a link to a codesandbox with the reproduced problem

https://codesandbox.io/s/nostalgic-wood-9dnyr6?file=/src/App.tsx

Do you know how to fix the issue

None

Are you willing to participate in fixing this issue and create a pull request with the fix

None

Possible fix

No response

@rtivital rtivital added the Fixed patch Completed issues that will be published with next patch (1.0.X) label Sep 21, 2023
@rtivital
Copy link
Member

The issue is resolved in 7.0.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fixed patch Completed issues that will be published with next patch (1.0.X)
Projects
None yet
Development

No branches or pull requests

2 participants