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

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

Open
naitoNanaco opened this issue Dec 17, 2024 · 0 comments

Comments

@naitoNanaco
Copy link

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;

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

1 participant