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

[FEATURE] - default values for input schema for Server Actions #11659

Closed
1 task done
billy-le opened this issue Aug 9, 2024 · 2 comments · Fixed by #11655
Closed
1 task done

[FEATURE] - default values for input schema for Server Actions #11659

billy-le opened this issue Aug 9, 2024 · 2 comments · Fixed by #11655
Labels
- P3: minor bug An edge case that only affects very specific usage (priority)

Comments

@billy-le
Copy link
Contributor

billy-le commented Aug 9, 2024

Astro Info

Astro                    v4.13.1
Node                     v22.4.0
System                   macOS (arm64)
Package Manager          unknown
Output                   server
Adapter                  @astrojs/node
Integrations             @clerk/astro/integration
                         @astrojs/react
                         @astrojs/tailwind
                         astro:db
                         @astrojs/db/file-url

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

I have a FormSchema, that I use for the client and on the server. The input for a server action doesn't return a default value where I was expecting it. I discovered this because I wanted to pass in an array of Files to upload to a bucket but the server kept saying that my input for images is invalid.

So I did a little investigation and found the part that would fix my use case and opened a PR. I've since taken it down so I'm raising this issue instead.

Ultimately, what I wanted was a list of Files from the client in the server action and it was throwing an error because the .default() method was not being handled.

export const PropertyFormSchema = z.object({
  images: z.array(z.instanceof(File)).default([]),
});
defineAction({
    accept: "form",
    input: PropertyFormSchema,
    handler: async (input, context) => {}
})

What's the expected result?

I expected this input to handle this correctly:

z.array(z.instanceof(File)).default([]) to return an empty array or a list of Files.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-7qnawk?file=src%2Fcomponents%2FPropertyForm.tsx

Couldn't reproduce on stackblitz. Seems like I can't perform server actions on there.

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Aug 9, 2024
@bholmesdev bholmesdev added - P3: minor bug An edge case that only affects very specific usage (priority) and removed needs triage Issue needs to be triaged labels Aug 9, 2024
@bholmesdev
Copy link
Contributor

Thanks for reporting this @billy-le! Yes, it looks like default values on form inputs aren't handled correctly. I checked that PR you linked, and I'm honestly happy to reopen and leave a few review comments before merging. Would you be open to that?

@billy-le
Copy link
Contributor Author

billy-le commented Aug 9, 2024

@bholmesdev for sure! I am thrilled to be contributing. Looking forward to your comments. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P3: minor bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants