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

[Bug]: zod-form-data gives different results depending on the sort order of properties in the schema #359

Closed
1 of 4 tasks
thdk opened this issue Apr 3, 2024 · 2 comments
Labels
bug Something isn't working upstream issue

Comments

@thdk
Copy link

thdk commented Apr 3, 2024

Which packages are impacted?

  • remix-validated-form
  • @remix-validated-form/with-zod
  • @remix-validated-form/with-yup
  • zod-form-data

What version of these packages are you using?

zod-form-data: 2.0.2

Please provide a link to a minimal reproduction of the issue.

https://stackblitz.com/edit/vitest-dev-vitest-bfrru6?file=test%2Fbasic.test.ts&view=editor

Steps to Reproduce the Bug or Issue

import { describe, expect, test } from 'vitest';
import { z } from 'zod';
import { zfd } from 'zod-form-data';

describe('zfd', () => {
  test.each([
    {
      name: 'repeatable() before text()',
      // this schema works
      schema: zfd.formData({
        field1: zfd.repeatable(),
        field2: zfd.text(),
      }),
    },
    {
      name: 'text() before repeatable()',
      // this schema does not work
      schema: zfd.formData({
        field2: zfd.text(),
        field1: zfd.repeatable(),
      }),
    },
  ])(
    'validates form data when a schema is defined with $name',
    async ({ schema }) => {
      const data = new FormData();
      data.append('field1', '');
      data.append('field2', '');

      const result = await schema.safeParseAsync(data);

      expect(result.success).toBe(false);

      if (result.success) {
        throw new Error('Expected failure but got success');
      } else {
        const errors = result.error.flatten() as z.inferFlattenedErrors<
          typeof schema
        >;

        expect.soft(errors.fieldErrors.field1).toEqual(['Required']);
        expect.soft(errors.fieldErrors.field2).toEqual(['Required']);
      }
    }
  );
});

Expected behavior

It shouldn't matter in which order the form fields are defined in the schema.

All validation issues should always be detected in one go.

No matter if schema is defined as:

zfd.formData({
    field1: zfd.repeatable(),
    field2: zfd.text(),
})

or as:

zfd.formData({
    field2: zfd.text(),
    field1: zfd.repeatable(),
})

Screenshots or Videos

No response

Platform

  • OS: MacOS
  • Browser: arc

Additional context

Could be related to my previous reported issue: #358

@thdk thdk added the bug Something isn't working label Apr 3, 2024
@airjp73
Copy link
Owner

airjp73 commented Apr 3, 2024

Hi again!

This one appears to be a zod bug: colinhacks/zod#3123 (comment)

Reproduction: https://stackblitz.com/edit/vitest-dev-vitest-w22qjv?file=test%2Fbasic.test.ts

@thdk
Copy link
Author

thdk commented Apr 3, 2024

Aha, thanks for pointing that out! This issue can be closed for me 👍🏼

@airjp73 airjp73 closed this as not planned Won't fix, can't repro, duplicate, stale Apr 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working upstream issue
Projects
None yet
Development

No branches or pull requests

2 participants