Include file uploads in validation#266
Open
archey347 wants to merge 1 commit into
Open
Conversation
archey347
commented
Nov 19, 2025
| if ($content_type =~ /^multipart\/form-data\s*(;|$)/i) { | ||
| # body_params only includes non-file parameters, so we need to fetch the | ||
| # uploads separately and append them with a file placeholder. | ||
| my $params = $c->req->body_params->clone; |
Author
There was a problem hiding this comment.
This does a full clone of the body params, which might cause problems if it's a large body. But then if they're doing stuff with large amounts of data, they probably should be using mojo's file upload stuff anyway.
I think this is needed, given we need to merge with the uploaded files to pass into the validator
This was referenced Nov 19, 2025
archey347
commented
Nov 19, 2025
Comment on lines
+133
to
+150
| if ($content_type =~ /^multipart\/form-data\s*(;|$)/i) { | ||
| # body_params only includes non-file parameters, so we need to fetch the | ||
| # uploads separately and append them with a file placeholder. | ||
| my $params = $c->req->body_params->clone; | ||
|
|
||
| for my $upload (@{$c->req->uploads}) { | ||
| my $name = $upload->name; | ||
|
|
||
| my $placeholder = JSON::Validator::FilePlaceholder->new({ | ||
| filename => $upload->filename, | ||
| size => $upload->size, | ||
| }); | ||
|
|
||
| $params->append($name => $placeholder); | ||
| } | ||
|
|
||
| $res->{value} = $params->to_hash; | ||
| } |
Author
There was a problem hiding this comment.
Doesn't seem ideal that we have to re-merge the files/body params like this.
But I think the parsing logic in Mojo::Message can only do files/non-files at any one time.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes
multipart/form-datavalidationbody_paramswithuploads, as Mojo likes to keep them separate so it can do magic stuff withMojo::UploadJSON::Validator::FilePlaceholderfor file uploads so we don't have to provide the entire fileMotivation
Currently, no validation is conducted on file uploads. The openapi spec has an option for this.
References
Requires jhthorsen/json-validator#288