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

Errors are discarded if using custom path error in zod's refinment #6817

Closed
1 of 2 tasks
nikita-sisutech opened this issue Sep 13, 2024 · 2 comments
Closed
1 of 2 tasks
Labels
Needs reproduction Issues without reproduction, closed within a week if the reproduction is not provided

Comments

@nikita-sisutech
Copy link

Dependencies check up

  • I have verified that I use latest version of all @mantine/* packages

What version of @mantine/* packages do you have in package.json?

7.12.1

What package has an issue?

@mantine/form

What framework do you use?

Vite

In which browsers you can reproduce the issue?

Firefox

Describe the bug

Consider following setup

import { useForm, zodResolver } from '@mantine/form';
import { z } from 'zod';

function Button() {
    const schema = z.object({
        first: z.string(),
        second: z.string(),
    }).refine(() => false, { path: 'first' });
        
    const form = useForm({
        mode: 'uncontrolled',
        initialValues: {},
        validate: zodResolver(schema),
    });
        
    function handleSubmit() {
        console.debug(form.validate();)
    }

      return <button onClick={handleSubmit}>Click</button>
  );
}

Console output will be empty, even though validation returns errors.

If possible, include a link to a codesandbox with a minimal reproduction

No response

Possible fix

The problem is that after validation library check that all error paths matched current path: https://github.com/mantinedev/mantine/blob/master/packages/%40mantine/form/src/validate/validate-field-value.ts#L15

For now there is only one workaround I found is to store errors in additional state and reset them to form:

import { useForm, zodResolver } from '@mantine/form';
import { z } from 'zod';
import { useState } from 'react';

function Button() {
    const [errors, setErrors] = useState<Record<string, any>>();
    const schema = z.object({
        first: z.string(),
        second: z.string(),
    }).refine(() => false, { path: 'first' });
        
    const form = useForm({
        mode: 'uncontrolled',
        initialValues: {},
        validate: (values) => {
            const result = zodResolver(schema)(values);
            if (Object.keys(result)) {
                setErrors(result);
            }
            return result;
        },
    });
    
    useEffect(() => {
        form.setErrors(errors);
    }, [errors]);
    
    function handleSubmit() {
        console.debug(form.validate();)
    }

    return <button onClick={handleSubmit}>Click</button>
}

Self-service

  • I would be willing to implement a fix for this issue
@rtivital
Copy link
Member

rtivital commented Oct 3, 2024

Please provide a sandbox with a minimal reproduction.

@rtivital rtivital added the Needs reproduction Issues without reproduction, closed within a week if the reproduction is not provided label Oct 3, 2024
@rtivital
Copy link
Member

rtivital commented Oct 8, 2024

You are welcome to reopen the issue if you can provide further details

@rtivital rtivital closed this as completed Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs reproduction Issues without reproduction, closed within a week if the reproduction is not provided
Projects
None yet
Development

No branches or pull requests

2 participants