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

Add readValidatedMultipartFormData #706

Open
1 task done
genu opened this issue Mar 15, 2024 · 0 comments
Open
1 task done

Add readValidatedMultipartFormData #706

genu opened this issue Mar 15, 2024 · 0 comments

Comments

@genu
Copy link

genu commented Mar 15, 2024

Describe the feature

Its a good pattern for multipart data to include both data and files. I implemented this pattern internally, but I think it would offer a good out of the box convenience.

The idea is that given this data object:

{
    "name": "John doe",
    "profileImage": File
}

To send it to the backend, we must send it using FormData like this:

{
     "data": <JSON.stringify({
     {
        "name": "John doe",
        "profileImage": "file_0"
     }
     })>,
     "file_0": <binary>
}

In a route, we would do:

const data = await readValidatedMultipartFormData(event, MySchema.parse);

The opinionated aspects:

  • the frontend would need to handle structuring the payload. In my implementation, I do a recursive search of the data and replace all File objects with a reference (e.g. file_x) and append the file at the end of the FormData. Then I stringify the payload into data
  • On the backend, I essentially do the reverse to rebuild the original data structure, and then pass it through the validator.

Let me know your thoughts.

Additional information

  • Would you be willing to help implement this feature?
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