Skip to content

[Validator] add Validation::createCallable() #31466

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

Merged
merged 1 commit into from
Feb 4, 2020

Conversation

janvernieuwe
Copy link
Contributor

@janvernieuwe janvernieuwe commented May 10, 2019

Q A
Branch? 4.4
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
Doc PR

This is an initial PR to check/discuss the implementation of a callable validator.
If there is interest in merging this, I will gladly update the docs and such.

The use case is mainly for validation of console questions, since the default validation has been removed in the latest version and i could not find an easy solution to replace it (if there already is some solution for this, i'm not aware of it) and the question helper uses callables.
This small class allows the standard symfony validators to be used in console questions, or any other location that requires a callable validator.

Example use case:

$io = new SymfonyStyle($input, $output);
$required = new CallableValidator([new NotBlank()]);
$wsdl = $io->ask('Wsdl location (URL or path to file)', null, $required);

As said before, this is by no means the final version, but I would like to know if there is interest in merging this (and receive some feedback about the implementation) before I put any more effort into this.

Copy link
Contributor

@Simperfit Simperfit left a comment

Choose a reason for hiding this comment

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

You should remove the two unrelated commits

@janvernieuwe janvernieuwe force-pushed the callable-validator branch 2 times, most recently from 1edd0a0 to 94d121f Compare May 13, 2019 07:31
@janvernieuwe janvernieuwe changed the base branch from master to 4.3 May 13, 2019 07:31
@janvernieuwe
Copy link
Contributor Author

Updated it, guess my fork got out of sync

@ro0NL
Copy link
Contributor

ro0NL commented Jun 4, 2019

instead of a new CallableValidator, which isn't really a validator from the component pov (as it's not a true ValidatorInterface implementation) ... what about Validation::createCallable($constraints): Closure

@lyrixx lyrixx changed the title Callable validator [Console] Callable validator Jun 4, 2019
@lyrixx lyrixx changed the base branch from 4.3 to 4.4 June 4, 2019 12:08
@janvernieuwe
Copy link
Contributor Author

janvernieuwe commented Jun 7, 2019

@ro0NL that can work too, it should be pretty easy as something like this in the Validation class:

    public static function createCallable(Constraint...$constraints): callable
    {
        $validator = self::createValidator();

        return static function ($value) use ($constraints, $validator) {
            $violations = $validator->validate($value, $constraints);
            if (0 !== $violations->count()) {
                throw new LogicException((string)$violations);
            }

            return $value;
        };
    }

Don't know if it is desirable to create a new Validator instance for each new callable though, but it shouldn't be too bad.

@janvernieuwe janvernieuwe force-pushed the callable-validator branch 3 times, most recently from 6fe4e80 to 015b0c8 Compare July 26, 2019 08:18
@janvernieuwe
Copy link
Contributor Author

Failures on travis don't seem to be related to any of my code changes as far as i can see...

@nicolas-grekas nicolas-grekas modified the milestones: next, 4.4 Oct 27, 2019
@nicolas-grekas nicolas-grekas changed the title [Console] Callable validator [Validator] add Validation::createCallable() Nov 5, 2019
return static function ($value) use ($constraints, $validator) {
$violations = $validator->validate($value, $constraints);
if (0 !== $violations->count()) {
throw new LogicException((string) $violations);
Copy link
Member

Choose a reason for hiding this comment

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

This should be a RuntimeException to me, or better: a class derived from RuntimeException modelled after ValidationFailedException from the Messenger component. WDYTN

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a new exception for this, let me know if you want to change the naming or anything.

@nicolas-grekas nicolas-grekas modified the milestones: 4.4, next Nov 8, 2019
Copy link
Contributor

@ro0NL ro0NL left a comment

Choose a reason for hiding this comment

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

nice feature 👍

@nicolas-grekas
Copy link
Member

can you please rebase+retarget for master?
if not no worries, we can do it on merger's side.

@janvernieuwe
Copy link
Contributor Author

I won't immediately have time for retargetting. So if it can be done on the merger's side, that'd be great!

@nicolas-grekas nicolas-grekas changed the base branch from 4.4 to master January 27, 2020 21:35
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

(I rebased the PR for master)

@fabpot
Copy link
Member

fabpot commented Feb 4, 2020

Thank you @janvernieuwe.

fabpot added a commit that referenced this pull request Feb 4, 2020
…euwe)

This PR was merged into the 5.1-dev branch.

Discussion
----------

[Validator] add Validation::createCallable()

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

This is an initial PR to check/discuss the implementation of a callable validator.
If there is interest in merging this, I will gladly update the docs and such.

The use case is mainly for validation of console questions, since the default validation has been removed in the latest version and i could not find an easy solution to replace it (if there already is some solution for this, i'm not aware of it) and the question helper uses callables.
This small class allows the standard symfony validators to be used in console questions, or any other location that requires a callable validator.

Example use case:
```php
$io = new SymfonyStyle($input, $output);
$required = new CallableValidator([new NotBlank()]);
$wsdl = $io->ask('Wsdl location (URL or path to file)', null, $required);
```

As said before, this is by no means the final version, but I would like to know if there is interest  in merging this (and receive some feedback about the implementation) before I put any more effort into this.

Commits
-------

2e4f2ac [Validator] add Validation::createCallable()
@fabpot fabpot merged commit 2e4f2ac into symfony:master Feb 4, 2020
@nicolas-grekas nicolas-grekas modified the milestones: next, 5.1 May 4, 2020
@fabpot fabpot mentioned this pull request May 5, 2020
@stof
Copy link
Member

stof commented May 18, 2020

I see an issue here: the callable creation always creates a new Validator object instead of allowing to use an existing one. This makes it incompatible with the DI integration, and so forbids using any constraint validator which has some dependency.
And it also makes it incompatible with any mapping configuration.

@nicolas-grekas
Copy link
Member

@stof good catch. What's your reco here? Revert? Improving? (in this case, could you work on a PR?)

@stof
Copy link
Member

stof commented May 18, 2020

Well, the variadic signature makes it hard to improve, as we cannot add an optional parameter to pass a validator instance.

@stof
Copy link
Member

stof commented May 18, 2020

My proposal: change the constraints from a variadic argument to an array argument (or maybe ConstraintInterface[]|ConstraintInterface for the case of only one constraint), and add the validator as an optional second argument.

@stof
Copy link
Member

stof commented May 18, 2020

Btw, this is the main reason why I don't like variadic arguments: they lock the signature entirely.

nicolas-grekas added a commit that referenced this pull request May 19, 2020
…eCallable() (nicolas-grekas)

This PR was merged into the 5.1 branch.

Discussion
----------

[Validator] allow passing a validator to Validation::createCallable()

| Q             | A
| ------------- | ---
| Branch?       | 5.1
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

As spotted by @stof in #31466 (comment)

Commits
-------

1357cbf [Validator] allow passing a validator to Validation::createCallable()
symfony-splitter pushed a commit to symfony/validator that referenced this pull request May 19, 2020
…eCallable() (nicolas-grekas)

This PR was merged into the 5.1 branch.

Discussion
----------

[Validator] allow passing a validator to Validation::createCallable()

| Q             | A
| ------------- | ---
| Branch?       | 5.1
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

As spotted by @stof in symfony/symfony#31466 (comment)

Commits
-------

1357cbf8ed [Validator] allow passing a validator to Validation::createCallable()
@ogizanagi ogizanagi mentioned this pull request Aug 17, 2020
fabpot added a commit that referenced this pull request Aug 17, 2020
This PR was merged into the 5.1 branch.

Discussion
----------

Fix tests namespaces

| Q             | A
| ------------- | ---
| Branch?       | 5.1 <!-- see below -->
| Bug fix?      | no
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | N/A <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead -->
| License       | MIT
| Doc PR        | N/A

Fixes namespaces for tests introduced in #34869 & #31466, spotted by travis generating Composer warnings:

```
Deprecation Notice: Class Symfony\Component\Console\Tests\Output\NullOutputFormatterStyleTest located in ./src/Symfony/Component/Console/Tests/Formatter/NullOutputFormatterStyleTest.php does not comply with psr-4 autoloading standard. It will not autoload anymore in Composer v2.0. in phar:///home/travis/.phpenv/versions/7.4.9/bin/composer/src/Composer/Autoload/ClassMapGenerator.php:201
Deprecation Notice: Class Symfony\Component\Console\Tests\Output\NullOutputFormatterTest located in ./src/Symfony/Component/Console/Tests/Formatter/NullOutputFormatterTest.php does not comply with psr-4 autoloading standard. It will not autoload anymore in Composer v2.0. in phar:///home/travis/.phpenv/versions/7.4.9/bin/composer/src/Composer/Autoload/ClassMapGenerator.php:201
Deprecation Notice: Class Symfony\Component\Validator\Tests\Validator\ValidationTest located in ./src/Symfony/Component/Validator/Tests/ValidationTest.php does not comply with psr-4 autoloading standard. It will not autoload anymore in Composer v2.0. in phar:///home/travis/.phpenv/versions/7.4.9/bin/composer/src/Composer/Autoload/ClassMapGenerator.php:201
```

Commits
-------

4e68c90 Fix tests namespaces
@janvernieuwe janvernieuwe deleted the callable-validator branch January 15, 2021 00:45
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