Skip to content

[RFC] New constraints for Symfony validator #28608

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
javiereguiluz opened this issue Sep 26, 2018 · 15 comments
Closed

[RFC] New constraints for Symfony validator #28608

javiereguiluz opened this issue Sep 26, 2018 · 15 comments
Labels
RFC RFC = Request For Comments (proposals about features that you want to be discussed) Validator

Comments

@javiereguiluz
Copy link
Member

Given that Symfony's validator is based on Java's validator, I was checking the Bean Validation 2.0 standard to see if they introduced new features that we could implement too.

These are the new constraints that Symfony doesn't define yet (although you can implement them indirectly with other existing constraints):

Dates:

  • @Past
  • @PastOrPresent
  • @Future
  • @FutureOrPresent

Numbers:

  • @Positive
  • @PositiveOrZero
  • @Negative
  • @NegativeOrZero

In addition, Hibernate Validator, the only official implementation of Bean Validator 2.0, defines other constraints:

  • @CreditCardNumber (it does just the Luhn algorithm check, via the @LuhnCheckValidator but I guess they defined this alias to make it easier to find and understand).
  • @EAN for barcode numbers.
  • @SafeHtml validates that the string does not contain malicious code. It uses an external sanitizer library.

Are we interested in implementing any of these constraints?

@javiereguiluz javiereguiluz added Validator RFC RFC = Request For Comments (proposals about features that you want to be discussed) labels Sep 26, 2018
@stof
Copy link
Member

stof commented Sep 26, 2018

if @SafeHtml requires a third-party sanitizer library, I suggest keeping it out of Symfony. A separate composer package can provide it, and have a requirement on the library it uses (instead of dealing with optional requirements).

regarding the CreditCardNumber alias, I would point you to the discussion on #26436, which seems to tell me we don't want to make it easier to discover that a credit card number is using luhn.

Regarding EAN, in Hibernate, this is just a Length constraint with only 2 possible allowed values for the length: https://github.com/hibernate/hibernate-validator/blob/eaa2cb6e45f95007c18297c2e4faea33e1c6b5ab/engine/src/main/java/org/hibernate/validator/internal/constraintvalidators/hv/EANValidator.java

@javiereguiluz
Copy link
Member Author

About the EAN validator, the last digit is a checksum digit ... so there's something to validate there. See https://gist.github.com/damonjones/7566978

@stof
Copy link
Member

stof commented Sep 27, 2018

Well, but there could be indeed. But Hibernate does not, making this constraint quite useless.

@jsamouh
Copy link
Contributor

jsamouh commented Sep 28, 2018

@javiereguiluz

1- for the Date Validator, do we have always a interest to keep DateTimeValidator and DateValidator ? Should we keep just one ? DateValidator with format accepted by date() ?

2- Past, Future can be options of DateConstraint ? or should it be annotation using the validation of DateValidator ?

@stof
Copy link
Member

stof commented Sep 28, 2018

Well, these should not be options of the Date constraints, as these constraints make sense on a property holding a DateTime object too, only on properties holding a string representing a date (which is what the Date constraint is about).

@jsamouh
Copy link
Contributor

jsamouh commented Sep 29, 2018

so @past.... can only work if value is instance of DateTimeInterface ? and a format Date constraint which is YYYY-MM-DD ? no format option for this constraint ?
@stof

@ro0NL
Copy link
Contributor

ro0NL commented Sep 29, 2018

From https://docs.jboss.org/hibernate/stable/beanvalidation/api/javax/validation/constraints/Past.html i'd say yes, but maybe we can add a format option to these to allow strings.

edit: actually we can just create dates from strings as is: https://3v4l.org/vk0AO

@jsamouh
Copy link
Contributor

jsamouh commented Sep 29, 2018

@ro0NL I do not see your point regard to https://3v4l.org/vk0AO

@ro0NL
Copy link
Contributor

ro0NL commented Sep 29, 2018

well.. it shows we dont need a format option

@Past                 # datetime | string
@Date @Past           # string
@Type(DateTime) @Past # datetime

the first excepts any date string (e.g. https://3v4l.org/vk0AO), the second is narrowed to a formatted stirng

@pimolo
Copy link

pimolo commented Sep 29, 2018

About the SafeHtml constraint, in my opinion, if you need to make sure that a string is safe from malicious code, it should be passed into a sanitizer anyway.

I don't see a common use case which would require to check first if it is already safe.

@javiereguiluz
Copy link
Member Author

I have an Event entity and I wanted to use https://symfony.com/blog/new-in-symfony-3-4-improved-comparison-constraints to replace my own callback validator by @Assert\GreaterThan(propertyPath="startDate") in the endDate.

However, the callback also validates that startDate is a future date ... and that's something you can't do with an expression validator ... so I ended up keeping the callback validator.

In summary: the @Future or @FutureOrPresent asserts would have come in handy for me today :)

@ogizanagi
Copy link
Contributor

@javiereguiluz : Do you mean @Assert\GreaterThan("today") ? 😄 https://symfony.com/doc/current/reference/constraints/GreaterThan.html#comparing-dates

@javiereguiluz
Copy link
Member Author

🤦‍♂️ Thanks Maxime 😊

@fabpot fabpot closed this as completed Mar 31, 2019
fabpot added a commit that referenced this issue Mar 31, 2019
This PR was squashed before being merged into the 4.3-dev branch (closes #28637).

Discussion
----------

[Validator] add number constraints

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #28608
| License       | MIT
| Doc PR        | tbd.

I added the following constraints:
* `Positive`
* `PositiveOrZero`
* `Negative`
* `NegativeOrZero`

Commits
-------

0187039 [Validator] add number constraints
@dkarlovi
Copy link
Contributor

This shouldn't have been closed since it's mentioning EAN (AKA GTIN), it's quite common for developers to work with "products" and having a dedicated validator for this specific identificator seems like something you'd want. It's not just length + checksum, it also has various flavors, etc.

@norkunas
Copy link
Contributor

Also that HtmlSanitizer is now proposed as new Symfony component (#44144) @SafeHtml constraint could be also included after :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFC RFC = Request For Comments (proposals about features that you want to be discussed) Validator
Projects
None yet
Development

No branches or pull requests

9 participants