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

zod-form-adapter turns all errors into one comma separated string, meta.errors assumes an array #942

Closed
Pascalmh opened this issue Sep 6, 2024 · 5 comments

Comments

@Pascalmh
Copy link
Contributor

Pascalmh commented Sep 6, 2024

Describe the bug

At the moment the zod-form-adapter's zodValidator validate and validateAsync functions turn the errors that zod reports into a comma separated string:

return result.error.issues.map((issue) => issue.message).join(', ')

When we want to display the Error(s) to the User the field.state.meta.errors field returns an array ValidationError[] which now only has one entry (the comma separated string.

Your minimal, reproducible example

https://stackblitz.com/edit/tanstack-form-r8vroz?file=src%2Findex.tsx

Steps to reproduce

  1. Open the Stackblitz
  2. The Form is already in the Error state (the Input # does not match the min-length nor the regex (letters a-Z)
  3. Observe: both Error messages are separated by a comma and a space => should only be a comma, see FieldInfo-Function
  4. You can also check the Console (you can filter for the string "errors")

Expected behavior

As a User
I expect field.state.meta.errors to be an array (type: ValidationError[]) with a length that matches the number of errors that resulted from the zod schema validation

BUT
I'm seeing an array (type: ValidationError[]) with the length of 1 no matter how many validation errors occurred.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

Not platform related

TanStack Form adapter

react-form

TanStack Form version

0.32.0

TypeScript version

No response

Additional context

No response

Pascalmh added a commit to Pascalmh/tanstack-form that referenced this issue Sep 6, 2024
Pascalmh added a commit to Pascalmh/tanstack-form that referenced this issue Sep 9, 2024
@Pascalmh
Copy link
Contributor Author

I think there is a bug in the definition of the ValidationError type

It is currently defined as:

export type ValidationError = undefined | false | null | string

Searching through the codebase there are places where ValidationError | undefined is specified:

const validatesPromises: Promise<ValidationError | undefined>[] = []
const linkedPromises: Promise<ValidationError | undefined>[] = []

This doesn't make sense as ValidationError already includes undefined in the union type.

Then there's this:

/**
   * An array of errors related to the field value.
   */
errors: ValidationError[]

where errors is an array of ValidationError - where the items of the array could be of type undefined | false | null | string so working with the errors requires an additional falsy check.

How to fix it?

My guess is that the ValidationError should be Array<string> | undefined | false | null

Related Issues

Pascalmh added a commit to Pascalmh/tanstack-form that referenced this issue Oct 8, 2024
@mimamuh
Copy link

mimamuh commented Dec 7, 2024

I have the same issue and wonder why this issue is not commented/flagged yet. Is this a bug or a feature? Personally, I would prefer not having the errors be joined and would therefore export an array with the custom transformErrors option.

@adhithiya05
Copy link

I have the same issue too.

@mimamuh
Copy link

mimamuh commented Dec 17, 2024

@adhithiya05 You can work around this by using the transformErrors option of the zodValidator like so; sadly it doesn't allow returning an array currently:

validatorAdapter: zodValidator({
    transformErrors: (errors) => {
        return errors.map(error => error.message).join('#!#'); 
    }
}),

And somewhere in your form / component you simply split the error again:

errors[0].split('#!#');

@Balastrong
Copy link
Member

Balastrong commented Dec 17, 2024

The meta.errors array contains the errors from all validators, that's why if you only have the onChange set you'll either find an empty array or an array with one element. It is working as expected and there's no bug to fix there.

Allowing to return an array of errors from a single validator might be a completely different topic, I wrote down some notes in the discussion at #946

@Balastrong Balastrong closed this as not planned Won't fix, can't repro, duplicate, stale Dec 17, 2024
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

4 participants