-
Notifications
You must be signed in to change notification settings - Fork 46
Include file uploads in validation #266
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
base: main
Are you sure you want to change the base?
Conversation
| 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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
| 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; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
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