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

Set action input default values from zod if FormData key is not present #11655

Merged
merged 1 commit into from
Aug 13, 2024

Conversation

billy-le
Copy link
Contributor

@billy-le billy-le commented Aug 8, 2024

Changes

  • What does this change?

Resolves #11659

This changes formDataToObject to allow default values to be set from zod.

Testing

Add tests to capture changes. I ran unit tests for this specific change and it passes. Not sure if the whole project tests are working correctly since I was getting some failures.

Docs

Copy link

changeset-bot bot commented Aug 8, 2024

🦋 Changeset detected

Latest commit: 4b336dd

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Aug 8, 2024
@billy-le
Copy link
Contributor Author

billy-le commented Aug 8, 2024

Adding some context on why I added this:

I was working on server actions and I have a file input that accepts multiple files. I'm using zod with a property of z.array(z.instanceOf(File)).default([]) Whenever I submitted the FormData to my action, I kept getting an error because the formDataToObject validator skipped over instances of z.ZodDefault and where it was expecting an array, it converted to an object by going to handleFormDataGet.

It looks like the z.ZodDefault wasn't accounted for so I thought I would add this bit.

There might be a case when someone could chain a bunch of z...default('').default('') and it will produce the same error that I had but it seems minor at the moment.

Copy link
Contributor

@bholmesdev bholmesdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, that was my (de)fault for not checking against default validators. Great fix! Just a couple comments

packages/astro/src/actions/runtime/virtual/server.ts Outdated Show resolved Hide resolved
.changeset/nervous-garlics-beam.md Outdated Show resolved Hide resolved
* fix: remove duplicate while loop. use correct boolean values on validation

* chore: rephrase changeset
@billy-le
Copy link
Contributor Author

Hey @bholmesdev, I've updated my code. Let me know if there's anything you think I might have missed!

@ematipico ematipico merged commit dc0a297 into withastro:main Aug 13, 2024
13 checks passed
@astrobot-houston astrobot-houston mentioned this pull request Aug 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE] - default values for input schema for Server Actions
3 participants