Skip to content

[Validator] added improve support for collection validation #31196

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

Conversation

Simperfit
Copy link
Contributor

@Simperfit Simperfit commented Apr 22, 2019

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

I've taken #9988, what's missing here @webmozart ?

@nicolas-grekas
Copy link
Member

Can you please squash + review the CS?

@Simperfit Simperfit force-pushed the feature/improve-support-for-collection branch from fcfb4af to 36f0176 Compare April 26, 2019 06:56
@Simperfit
Copy link
Contributor Author

done @nicolas-grekas

@Simperfit Simperfit force-pushed the feature/improve-support-for-collection branch from 654a846 to 1f7be1f Compare April 26, 2019 06:58
@xabbuh
Copy link
Member

xabbuh commented Apr 27, 2019

Can you add an entry to the Validator component CHANGELOG.md file?

@Simperfit
Copy link
Contributor Author

changelog added, PR rebased with master.

@Simperfit Simperfit force-pushed the feature/improve-support-for-collection branch from 1f7be1f to fb26d1f Compare April 27, 2019 14:39
@Simperfit Simperfit changed the title [Validator] added improve support for collection validation [WIP][Validator] added improve support for collection validation Apr 27, 2019
@Simperfit Simperfit changed the title [WIP][Validator] added improve support for collection validation [Validator] added improve support for collection validation Apr 27, 2019
@Simperfit
Copy link
Contributor Author

Validators and test rewrited.

@Simperfit Simperfit force-pushed the feature/improve-support-for-collection branch 2 times, most recently from d16e041 to 9206395 Compare April 27, 2019 17:02
@Simperfit Simperfit force-pushed the feature/improve-support-for-collection branch from 9206395 to 20f9edd Compare April 30, 2019 07:35
Copy link
Contributor

@elementaire elementaire left a comment

Choose a reason for hiding this comment

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

Some typos.

@Simperfit Simperfit force-pushed the feature/improve-support-for-collection branch from 20f9edd to 59a8f1d Compare May 1, 2019 08:18
@Simperfit
Copy link
Contributor Author

Thanks @elementaire for the review.

@Simperfit Simperfit force-pushed the feature/improve-support-for-collection branch from 59a8f1d to 4d05d1d Compare May 8, 2019 10:56
@Simperfit Simperfit force-pushed the feature/improve-support-for-collection branch 2 times, most recently from d6efa82 to 6501f58 Compare June 8, 2019 16:25
@Simperfit
Copy link
Contributor Author

Thanks for the review @xabbuh, all comments have been resolved ;).

Status: Needs review

@Simperfit
Copy link
Contributor Author

Travis failure is not related

@Simperfit
Copy link
Contributor Author

PR Rebased

@Simperfit Simperfit force-pushed the feature/improve-support-for-collection branch from 6501f58 to 04ab76c Compare June 20, 2019 20:22
@Simperfit Simperfit force-pushed the feature/improve-support-for-collection branch from 04ab76c to a9aa221 Compare July 9, 2019 06:29
@Simperfit
Copy link
Contributor Author

@fabpot @xabbuh can we move forward with this ? conflict has been resolved.

@@ -18,6 +18,8 @@
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
*
* @author Yevgeniy Zholkevskiy <zhenya.zholkevskiy@gmail.com>
* @author Marc Morera Merino <yuhu@mmoreram.com>
* @author Marc Morales Valldepérez <marcmorales83@gmail.com>
Copy link
Member

Choose a reason for hiding this comment

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

Why these changes? And if, shouldn't they be done on the 4.3 branch?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

was from the original PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it needs to be revert because it was the contraints has been modfied in the original PR WDYT ? @xabbuh

/**
* {@inheritdoc}
*/
protected function getCompositeOption()
Copy link
Member

Choose a reason for hiding this comment

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

I do not really see why we need this method.

Copy link
Contributor Author

@Simperfit Simperfit Jul 16, 2019

Choose a reason for hiding this comment

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

to say if it's a contraints or a field, if we remove it, we need to remove it from the abtracts composite too and check if it's a contraints or a field

/**
* @author Hamza Amrouche <hamza.simperfit@gmail.com>
*/
class ExactlyValidator extends ConstraintValidator
Copy link
Member

Choose a reason for hiding this comment

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

I am actually not convinced that we need this validator. What would be a use case for it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

because of #31196 (comment)

* @author Marc Morera Merino <yuhu@mmoreram.com>
* @author Marc Morales Valldepérez <marcmorales83@gmail.com>
*/
class SomeValidator extends ConstraintValidator
Copy link
Member

Choose a reason for hiding this comment

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

What would be an actual use case for this constraint?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess if some of the required contraints passesa and some other not it should be working properly.

@Simperfit Simperfit force-pushed the feature/improve-support-for-collection branch from a9aa221 to 98cd0d4 Compare July 16, 2019 06:45
@Simperfit Simperfit force-pushed the feature/improve-support-for-collection branch from 98cd0d4 to 10948fc Compare July 16, 2019 06:47
@Simperfit
Copy link
Contributor Author

PR Rebased and review answered.

Status: Needs Review

@Simperfit
Copy link
Contributor Author

cc @xabbuh another round ?

@xabbuh
Copy link
Member

xabbuh commented Aug 26, 2019

@Simperfit I would really be interested in seeing a real concrete use case where one would like to ensure that a particular number of constraints passes (or fails). I cannot come up with an example where this seems to make sense.

Copy link
Member

@stof stof left a comment

Choose a reason for hiding this comment

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

Almost all validators need to be re-implemented in that PR.

* added `Each` constraint
* added `EachValidator`
* added `None` constraint
* added `NoneValidator`
Copy link
Member

Choose a reason for hiding this comment

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

I suggest merging this line with the previous one. Adding the new constraint and its validator are a single feature addition (same for other constraints).
Older versions don't mention the validator at all btw, only the new constraint (as that's the relevant user-facing thing)

use Symfony\Component\Validator\Exception\ConstraintDefinitionException;

/**
* @Annotation
Copy link
Member

Choose a reason for hiding this comment

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

should be removed. This abstract class is not an annotation by itself.

@@ -16,23 +16,12 @@
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
*
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @api
Copy link
Member

Choose a reason for hiding this comment

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

should be removed. We dropped such tag years ago in favor of @internal and @experimental

*
* @api
*
* @deprecated Deprecated in 4.3, to be removed in 5.0. Use
Copy link
Member

Choose a reason for hiding this comment

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

4.4

Copy link
Member

Choose a reason for hiding this comment

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

and this also needs to trigger a deprecation warning.

*
* @api
*
* @deprecated Deprecated in 4.3, to be removed in 5.0. Use
Copy link
Member

Choose a reason for hiding this comment

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

same issues here

throw new UnexpectedValueException($value, 'array or Traversable');
}

$validator = $this->context->getValidator()->inContext($this->context);
Copy link
Member

Choose a reason for hiding this comment

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

same here about not using the current context for violations you don't want to show.

*
* Message for notice Max Violation
*/
public $maxMessage = '{{ limit }} or less element of this collection should pass validation.|{{ limit }} or less elements of this collection should pass validation.';
Copy link
Member

Choose a reason for hiding this comment

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

no message for the exactly case ?

*
* Exactly number of Success expected
*/
public $exactly;
Copy link
Member

Choose a reason for hiding this comment

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

Do we need this property ? Isn't it the same than min = max ?


$totalIterations = \count($value) * \count($constraint->constraints);

$validator = $this->context->getValidator()->inContext($this->context);
Copy link
Member

Choose a reason for hiding this comment

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

same issue here about not reusing the context

$validator->atPath('['.$key.']')->validate($element, $constraint->constraints);
}

$constraintsSuccess = $totalIterations - (int) $this->context->getViolations()->count();
Copy link
Member

Choose a reason for hiding this comment

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

this is wrong IMO. You should not count how many constraints were successful, but how many elements were successful. Otherwise, this is a pain to get right.

@fabpot
Copy link
Member

fabpot commented Feb 4, 2020

Closing based on the comments from @xabbuh and @stof

@fabpot fabpot closed this Feb 4, 2020
@nicolas-grekas nicolas-grekas modified the milestones: next, 5.1 May 4, 2020
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.

9 participants