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

Insert defaultValue is not typed if field is a nullable or optional array #514

Closed
nianiam opened this issue Mar 12, 2024 · 2 comments · Fixed by #597
Closed

Insert defaultValue is not typed if field is a nullable or optional array #514

nianiam opened this issue Mar 12, 2024 · 2 comments · Fixed by #597
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@nianiam
Copy link

nianiam commented Mar 12, 2024

Describe the bug and the expected behavior

I am creating some dynamic fields in my form using the form.insert method, but noticed that I was getting a type error whilst trying to add some default values. It would appear that when the field is an array and is optional/nullable then the defaultValue type becomes a string. Here is a simplified example

export const schema = z.object({
  test: z.array(z.object({ nested: z.string() })).nullable() // or .optional()
})
<button
  {...form.insert.getButtonProps({
    name: fields.test.name,
    defaultValue: { // <-- Type '{ nested: string; }' is not assignable to type 'string'.
      nested: "I am nested",
    },
  })}
>
  Click me
</button>

It's fairly easy to work aroud this situation by making the field required and setting the default value of the form to an empty array, however, I thought I would report in case this was not deliberate!

Maintaining the type inside of the array would make the most sense.

Conform version

v1.0.3

Steps to Reproduce the Bug or Issue

  1. Create a simple schema with an an optional/nullable array as a field.
  2. Use the field metadata to insert values in the array.
  3. See defaultValue is types as a string and not the type within the schema.

What browsers are you seeing the problem on?

Chrome, Firefox, Safari

Screenshots or Videos

No response

Additional context

No response

@edmundhung edmundhung added help wanted Extra attention is needed bug Something isn't working labels Mar 14, 2024
@lifeiscontent
Copy link
Contributor

@edmundhung this seems to be related to the defaultValue fix that @aaronadamsCA did whenever a schema has a key that is an array that also has an optional() or nullable() this seems to still have an issue.

lifeiscontent added a commit to lifeiscontent/conform that referenced this issue Apr 23, 2024
edmundhung pushed a commit to lifeiscontent/conform that referenced this issue Apr 28, 2024
@edmundhung
Copy link
Owner

Thanks for reporting the issue! This is fixed in v1.1.3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants