Skip to content

parseWithZod fails to parse Arrays containing empty strings ('') correctly #839

Description

@naitoNanaco

Describe the bug and the expected behavior

When an array in formData contains an empty string ('') as the first element, it gets trimmed, and the resulting payload has fewer elements than expected. As a result, the indices in parseResult.value do not match the original formData.
This behavior also affects validation errors when using useForm, making it difficult to pinpoint which input has an error.

Conform version

v1.1.5

Steps to Reproduce the Bug or Issue

test("parseWithZod with array", () => {
  const formData = new FormData();

  formData.append("animals", "");
  formData.append("animals", "dog");
  formData.append("animals", "");
  formData.append("animals", "cat");

  const result = parseWithZod(formData, {
    schema: z.object({
      animals: z.array(z.string().optional()),
    }),
  });

  expect(result.status).toBe("success");
  expect(result.payload).toEqual({
    animals: ["", "dog", "", "cat"],
  });
});
AssertionError: expected { animals: [ 'dog', '', 'cat' ] } to deeply equal { animals: [ '', 'dog', '', 'cat' ] }

- Expected
+ Received

  Object {
    "animals": Array [
-     "",
      "dog",
      "",
      "cat",
    ],
  }

What browsers are you seeing the problem on?

Chrome

Screenshots or Videos

No response

Additional context

This issue occurs because '' is treated as falsy. As a result, setting values into the context is skipped until a concrete value is provided.

if (!prev) {
return next;

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions