Skip to content

Array items are not validated when calling .validate(...) on an array field #1649

@Thomascogez

Description

@Thomascogez

Describe the bug

Hey 👋,

I was recently working on a project with tanstack/form where I was using it form a multistep form.
In order to validate fields for each steps, I was using form.validate("stepField"). I actually figure that array fields with children validation does not seem to be correctly validated

It wrote a quick unit test in order to make it clearer

  it('should correctly validate array items with a schema', async () => {
    const form = new FormApi({
      defaultValues: {
        names: [] as string[],
      },
      validators: {
        onSubmit: z.object({
          names: z.array(z.string().max(1)),
        })
      }
    })
    form.mount()

    const field = new FieldApi({
      form,
      name: 'names',
    })
    field.mount()

    field.pushValue("that more than 3 characters and should fail")

    const issueResult = await field.validate("submit")

    expect(issueResult).toBeDefined()
    expect(Array.isArray(issueResult)).toBe(true)
    expect(issueResult.length).toBeGreaterThan(0)
  })

and it output like this
Image

Hope it can be hopeful ! I start to deep dive the codebase, and if end-up being a real issue i would love to contribute 👍

Your minimal, reproducible example

Provided in the description

Steps to reproduce

  1. Define an field with for example z.array(z.string().max(1))
  2. Input a value that does not match the schema
  3. Call validate on the field
  4. Error should be reported indicating that the array item is invalid

Expected behavior

Error should be reported indicating that the array item is invalid

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • Macos
  • Reproduced inside tanstack/form repository

TanStack Form adapter

None

TanStack Form version

1.15.1

TypeScript version

NA

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions