8000 Add PSR-7 support for FileValidator by wouterj · Pull Request #15421 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Add PSR-7 support for FileValidator #15421

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

Closed
wants to merge 1 commit into from

Conversation

wouterj
Copy link
Member
@wouterj wouterj commented Jul 31, 2015
Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #15414 (partially)
License MIT
Doc PR -

I couldn't find an easy way to add tests for this, as the base tests are about files in the filesystem and not uploaded files.

@nicolas-grekas
Copy link
Member

For adding a test, you could create a mock UploadedFileInterface object that returns credible values for the method used in the validator.
You'd need to add psr/http-message in the require-dev section of this component's composer.json file, and in Symfony's one also.

@stof
Copy link
Member
stof commented Aug 1, 2015

the way this is implemented means that supporting PSR-7 requires to have HttpFoundation installed. Wouldn't it be better to support using PSR-7 directly rather than converting it to HrrpFoundation ?

@@ -51,6 +52,10 @@ public function validate($value, Constraint $constraint)
return;
}

if ($value instanceof UploadedFileInterface) {
$value = new UploadedFile('', $value->getClientFilename(), $value->getClientMediaType(), $value->getSize(), $value->getError());
Copy link
Member

Choose a reason for hiding this comment

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

passing an empty path looks wrong here. It will break things when retrieving the path to the file later in the validation logic to perform the assertions

Copy link
Member

Choose a reason for hiding this comment

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

If this approach is followed, wouldn't it be better to use the bridge instead of duplicating the logic?

But I agree with @stof, a validator without dependency to HttpFoundation would be better (better performance, less complexity and dependencies).

@dunglas
Copy link
Member
dunglas commented Aug 7, 2015

Take a look at the PSR-7 bridge for tests examples.

@wouterj
Copy link
Member Author
wouterj commented Aug 9, 2015

Looked at the code again after my week off and discovered that 90% of the validator is about files in the filesystem and only a small section about uploaded files. The UploadedFile from HttpFoundation represents both the uploaded file as the file on the filesystem, while UploadedFileInterface in PSR-7 only represents the uploaded file.

I've now rewritten some lines in the validator so that the upload errors are now validated for both HttpFoundatio 8000 n's uploaded file as PSR-7 uploaded file.

I'm not sure if this is really the direction we want to go, as this means the FileValidator without HttpFoundation is quite useless (it's the mime type validation and such that make this validator interesting).

@@ -292,6 +307,33 @@ private static function moreDecimalsThan($double, $numberOfDecimals)
return strlen((string) $double) > strlen(round($double, $numberOfDecimals));
}

private static function getMaxFilesize()
Copy link
Member Author

Choose a reason for hiding this comment

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

This method is equal to HttpFoundation\File\UploadedFile::getMaxFilesize(). It exists here in order to remove the dependency on HttpFoundation for creating the error message.

Copy link
Member

Choose a reason for hiding this comment

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

But we still have a dependency on the component, so what's the advantage?

8000 Copy link
Member

Choose a reason for hiding this comment

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

nevermind - it's a dev dependency. I'm with you :)

@@ -29,7 +29,8 @@
"symfony/expression-language": "~2.4|~3.0.0",
"doctrine/annotations": "~1.0",
"doctrine/cache": "~1.0",
"egulias/email-validator": "~1.2,>=1.2.1"
"egulias/email-validator": "~1.2,>=1.2.1",
"psr/http-message": "~1.0"
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this go into suggest too?

Copy link
Member

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.

It needs to be here because we reference the Psr UploadedFileInterface

Copy link
Member Author

Choose a reason for hiding this comment

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

I've now also added it to the suggests section as well.

@fabpot
Copy link
Member
fabpot commented Sep 14, 2015

ping @symfony/deciders What about this one? I don't this we should bother too much about this now. So, I'm -0

@nicolas-grekas
Copy link
Member

👍 on my side. It doesn't mean I'm yet convinced by psr-7 but this opens the use cases with very small changes.

@weaverryan
Copy link
Member

I don't really care either way on this one either - but it is a small patch to accomplish this. So 👍 - it's consistent with our decision to add support PSR-7 to components when we can do so without breaking BC.

return new FileValidator();
}

public function testValidUploadedFile()
Copy link
Member

Choose a reason for hiding this comment

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

why not putting them in the FileValidatorTest class directly ?

Copy link
Member Author

Choose a reason for hiding this comment

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

There is not one FileValidatorTest, there are 2: FileValidatorObjectTest, FileValidatorPathTest, both extending FileValidatorTest. It makes no sense to put it in FileValidatorTest, as it would be run twice with exact the same input/output.

@fabpot
Copy link
Member
fabpot commented Sep 28, 2015

Re-reading this PR now and I think I'm 👎 now. If you are using PSR-7, you don't have mime-type validation, size validation, ... Having a validator that does not do the same thing for PSR-7 and HttpFoundation looks dangerous to me, especially as there is nothing warning the developer about the different behavior. As we decided to not support PSR-7 in 3.0, I propose to close this one.

@weaverryan
Copy link
Member

Ah, good point. I agree - unless they work perfectly the same with both Request inputs, it seems like a bad idea. You could even configure some mimeType stuff, but then have it totally ignored.

@fabpot fabpot closed this Oct 1, 2015
@wouterj wouterj deleted the validator_psr-7 branch October 1, 2015 21:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants
0