Commit graph

1825 commits

Author SHA1 Message Date
Danilo Correa 083ccec068
Apply contribution guidelines to "LeapYear" rule
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-09-14 08:41:14 +02:00
Danilo Correa 5b6184d4e2
Apply contribution guidelines to "NotOptional" rule
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-09-14 08:40:32 +02:00
Henrique Moody 9283c8ecd9
Do not accept whitespace by default in "Digit" rule
The "Digit" rule is meant to validate digits. However, by default, it
also considers any whitespace character (spaces, new lines, tabs, etc)
as valid.

Since the rule also accepts a list of characters to ignore during the
validation it seemed logical to me to leave the responsibility of
allowing whitespace characters on the hands of the one who uses the
rule.

The messages of the exception are not really consistent, this commit
will also fix that.

It's also clear that the "AbstractCtypeRule" is an unnecessary overhead
since it is only a proxy for "AbstractFilterRule". That one can and
should even be removed after this commit is applied especially because
this commit will also remove the method "filterWhiteSpaceOption" which
is the only substantial difference between "AbstractCtypeRule" and
"AbstractFilterRule".

This commit will also apply our guidelines to the "Digit" rule since we
want to do that to all the rules we have.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-09-12 10:00:17 +02:00
Henrique Moody 27bd5d204d
Do not accept whitespace by default in "Alpha" rule
The intent of the "Alpha" rule is to validate alphabetic values.
However, it also considers any whitespace character (by default). That
causes some confusion, and unless you check its code or tests, you would
never expect that behavior.

Because of that confusion, I decided to make "Alpha" to not consider
whitespace characters as valid, and since in the constructor of this
rule it's possible to add extra characters to the validation it makes
sense to let the user decide whether they want whitespaces, tabs, new
lines, etc. or not.

This rule, as the same as "Alnum" previously, extends
"AbstractCtypeRule" pretty much to only make it easier to consider any
whitespaces as valid, therefore I saw no reason to keep extending it.
Now "Alpha" extends the "AbstractFilterRule" which is the parent of
"AbstractCtypeRule".

I also took the opportunity to apply our contribution guidelines to
"Alpha" since we want to apply that to all the rules.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-09-11 11:38:15 +02:00
Henrique Moody 3601adf6db
Create "FilteredValidationException" class
All the classes that were extending "AbstractFilterRule" were throwing
exceptions that were extending "AlphaException". That can cause some
issues when the user has a validation chain with "Alpha" and another
child of "AbstractFilterRule" and expects "AlphaException" specifically.

This commit creates "FilteredValidationException" that can be used as
the parent of the exceptions thrown by rules that extend the class
"AbstractFilterRule".

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-09-11 11:02:17 +02:00
Danilo Correa 24040c9473 Apply contribution guidelines to "NotBlank" rule 2018-09-10 21:00:53 -03:00
Danilo Correa 0e67549dd9
Apply contribution guidelines to "Pesel" rule
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
2018-09-10 14:51:52 +02:00
Danilo Benevides 2bb6f66251
Apply contribution guidelines to "LeapDate" rule
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
2018-09-10 14:51:52 +02:00
Danilo Correa 73a0107349
Apply contribution guidelines to "PhpLabel" rule
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
2018-09-10 08:18:15 +02:00
Danilo Correa 30993fc4a0
Apply contribution guidelines to "Pis" rule
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
2018-09-10 08:16:25 +02:00
Danilo Correa e832a99d4c
Apply contribution guidelines to "StringVal" rule
Also creates a "ToStringStub" class to help on testing objects that can
be converted to string.

Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-09-06 08:41:12 +02:00
Danilo Correa c7fbded457
Apply contribution guidelines to "NumericVal" rule
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-09-06 08:41:11 +02:00
Danilo Correa 17f8c50f54
Apply contribution guidelines to "Length" rule
Because of the type hinting some validation could be removed from the
"length" constructor.

While applying the contribution guidelines we could also see some
duplicated logic in the "extractLength" method and that the rule was
validating

Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-09-06 08:41:11 +02:00
Henrique Moody 779c0c1503
Do not accept whitespace by default in "Alnum" rule
The "Alnum" rule is supposed to validate alphanumeric values, but
instead, it also validates any whitespace character as valid.

The rule also accepts a list of characters on its constructor, so it the
users intentionally want some specific characters to also be allowed it
is better than they also defined these characters on the rule's
constructor.

While refactoring the rule I could notice that "AbstractCtypeRule" is
just an overhead that does not add much to it, so instead of extending
it "Alnum" now extends "AbstractFilterRule" directly (which is the
parent of "AbstractCtypeRule").

And since we want all rules to follow our contribution guidelines, this
commit also make sure the "Alnum" rule is in accordance with that.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-09-04 00:05:24 +02:00
Henrique Moody fb9d3a6918
Remove condition related to HHVM
Because we don't support HHVM anymore, there is no reason to keep a
condition that keeps the test when running the test suite in HHVM.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-09-03 17:38:33 +02:00
Henrique Moody c7fcc09af6
Upgrade PHPUnit version
The changes you see in this commit had to be made due to the upgrade of
PHPunit version.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-09-03 17:37:44 +02:00
Danilo Benevides 9a8cac06d7 Apply contribution guidelines to "Json" rule 2018-08-30 23:08:27 -03:00
Henrique Moody 5a067faec7
Update "PostalCode" list
Updated the whole list according to the last version found on
geonames.org.

The script that I executed to generate the list is:

curl -L http://download.geonames.org/export/dump/countryInfo.txt |
  sed 's,\t,\;,g' |
  sort -u |
  cut -d ';' -f 1,15 |
  sed -E "/^#/d; /^[A-Z]{2}\;$/d; s,([A-Z]{2})\;(.+),'\1' => '/\2/'\,,g"

After generating the list I few changes had to be made since some of the
postal codes in the list were manually updated because they were not
correct.

This change was made because James Macon reported that "110231" was
not recognized as a valid Colombian postal code.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-08-29 22:30:50 +02:00
Henrique Moody a039d6d4b9
Fix wrong behavior of "Date" rule
The validation was considering "99" as a valid month and neither
date_parse_from_format() [1] or DateTime::createFromFormat() [2] would
complain about that.

The solution for that was to use checkdate() [3] to verify whether the
date is valid or not. Also, an extra step was added to the validation
that a date that only contains a day would return false since a day
without a month and year is impossible to validate.

Apparently, there is no problem while validation when it comes to time,
therefore nothing needed to be added for this validation.

[1]: http://php.net/date_parse_from_format
[2]: http://php.net/datetime.createFromFormat
[3]: http://php.net/checkdate

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-08-29 21:46:53 +02:00
Henrique Moody 4354652195
Merge branch '1.1' 2018-08-23 02:12:44 +02:00
Henrique Moody 52614d600d
Organize documentation for "Read the Docs"
The current documentation is hosted via GitHub pages rendered by
"Couscous". Every time we need a new version of the documentation
published we need to manually execute the "couscous".

This commit reorganize the documentation to be published to
"Read the Docs" because it will also allow us to have documentations per
version of the library most importantly provider a search field for the
documentation.

The documentation will be then published on:
https://respect-validation.readthedocs.io/

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-08-23 01:59:39 +02:00
Henrique Moody 258a456eec
Setup PHPStan (PHP Static Analysis Tool)
Require "phpstan/phpstan" for development and add to the Travis CI
configuration file to execute the analysis when Travis executes the
build with the version 7.2 of PHP.

The level of the configuration is very week for now (just "1") and still
quite some changes had to be made in order to make the analysis pass. I
hope it does not take much time to increase the level of the strictness
of the analyses.

I tried to configure that before but because of dependencies with
"symfony/validator" it was not possible.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-08-22 19:10:20 +02:00
Henrique Moody f5c167c411
Merge branch '1.1' 2018-08-22 19:09:55 +02:00
Henrique Moody 1da164a26e
Change how "Sf" rule works
Instead of creating the Symfony constraints itself "Sf" accepts an
instance of "Symfony\Component\Validator\Constraint".

Creating objects inside a rule, specially from an external library,
makes the rule too complex and also limits the possibilities with the
"Sf" rule since Symfony allows users to create complex validations (even
thought their API is not as simple as ours).

This commit also simplifies the way the messages are passed from Symfony
to the "Sf" when only one constraint has failed; instead of passing
the message of the whole constraint violation list, only the fist
constraint violation message it passed.

The problem that this rule will always have is that when using "Not" to
invert the validation we have a way to get a proper message since
Symfony Validator only return the result of constraints that failed.
That's something the Respect\Validation does in a similar way and to
change it a lot has to be changed.

These changes were checked in "symfony/validator" 4.0 and the version
was added to the "composer.json" file.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-08-22 17:46:21 +02:00
Henrique Moody fe7fed3461
Remove support "egulias/email-validator" ~1.0
The rule "Email" now only supports the version >=2.0 of
"egulias/email-validator". Also a few changes were made to make the rule
a bit simpler.

The method "getEmailValidator()" which was a lazy load of the
"EmailValidator" class was removed in favor of trying to create the
instance of "EmailValidator" in the constructor of "Email". That makes
the rule immutable after its creation.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-08-22 17:22:46 +02:00
Henrique Moody d64d26c681
Intercept throwables in the "Call" rule
The callable defined to the "Call" rule may also throw an exception and
as we don't want to have errors nor exceptions that are not part of the
Validation during the validation of inputs it just makes sense to
intercept any instance of Throwable.

This change was initially thought of because in Travis the version 7.4
of PHP was throwing "Error" instead of triggering PHP errors which made
the tests fail.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-08-22 17:04:20 +02:00
Henrique Moody 2fac861aa1
Do not trigger PHP errors in the "Call" rule
This commit make sure that when the callable is executed by the "Call"
rule and PHP triggers an error, the user does not have to deal with it,
and instead the rule will throw "CallException".

Because of the many changes that were made, it didn't make sense to keep
the class "Call" extending the "AbstractRelated" class.

One thing that is a bit problematic with this rule - and with other
rules as well - is that Validation only knows details of a validation
when it fails, because of that we cannot invert the validations that
passed, meaning that the "Not" rule cannot give the proper response to
a validation that passed. This is a know issue that can only be fixed
is we provide a way for Validation do have more granularity control.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-08-21 01:22:38 +02:00
Henrique Moody 3b789815fb
Execute PHP-CS-Fixer
The last version of PHP-CS-Fixer fixes a few things that were not
detected before.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-08-21 01:20:54 +02:00
Danilo Benevides 2b8acfd790 Apply contribution guidelines to "In" rule 2018-08-19 16:01:10 -03:00
Henrique Moody add542ea48
Update "Tld" rule with IANA's list
This commit will update the list of Top-Level Domains in the "Tld" rule
with the latest version found in the website of IANA (Internet Assigned
Numbers Authority).

The list was created with the commands below:

curl -L curl -L https://data.iana.org/TLD/tlds-alpha-by-domain.txt |
    grep -v '^#' |
    tr '[A-Z]' '[a-z]' |
    sed -E "s,^,',; s/$/', /" |
    tr -d '\n' |
    fold -w 72 -s

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-08-18 15:10:37 +02:00
Danilo Benevides d022a71f54
Apply contribution guidelines to "Instance" rule
Also updates how the tests define the name of the instance by using the
class keyword [1] instead of a string with the class. That is useful
in code analyses be able to identify the usage of these
classes/interfaces.

[1] http://php.net/class#language.oop5.basic.class.class

Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
2018-08-16 21:54:17 +02:00
Ismael Elias fad0005fa0
Apply contribution guidelines to "Number" rule
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
2018-08-16 21:54:17 +02:00
Danilo Benevides 94d2e87b2a
Apply contribution guidelines to "Infinite" rule
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
2018-08-16 21:54:16 +02:00
Danilo Benevides f01972e208
Apply contribution guidelines to "Imei" rule
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
2018-08-16 21:54:16 +02:00
Henrique Moody afab4eb2b4
Rename "Related" to "Child"
The "NestedValidationException" has exceptions that are its children.
However, we call them "related". This commit updates the term over the
library.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-08-02 11:18:33 +02:00
Henrique Moody a1bc0cd6b4
Apply contribution guidelines to "NestedValidationException"
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-08-02 11:11:04 +02:00
Henrique Moody 3ad35ed545
Remove unneessary information from iterator
In the iterator in the "NestedValidationException" the only information
that is necessary is the depth of the message, for that reason all the
other information can (and should) be removed.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-08-02 11:09:30 +02:00
Henrique Moody da10941ddc
Apply contribution guidelines to "Validator" class
Also removes the possibility of the "create()" method to create a new
Validation with predefined rules since that is the behavior that the
constructor already has.

And even though may be a bit scary to make "Validator" final I believe
there should be no reason for this class to be extended, and if there is
it is probably because we are missing something, which I would very much
like to find out.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-08-02 10:37:38 +02:00
Henrique Moody 3554288c60
Remove "buildRule" method from "Validator"
Because Validator uses a Factory to create rules, it does not make sense
to create rules with this method anymore. Furthermore it goes over the
responsibilities of the "Validator" to create objects.

That said, the existence of such method is only one more point of
maintenance and nothing that brings much values for our uses therefore
it should be remove.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-08-02 10:37:10 +02:00
Henrique Moody e4483b74f2
Remove unnecessary validation from "Factory" class
The "ReflectionClass" throws an exception if the class cannot be found,
for that reason the validation with the "class_exists()" function was
unnecessary.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-08-02 10:23:27 +02:00
Henrique Moody eded93b0df
Merge branch '1.1' 2018-08-01 22:08:30 +02:00
Henrique Moody 0cdd8c4546
Fix issues after merging 1.1
These two branches are very different, therefore merging is becoming
very hard.

I decided to not put these changes together with 5750952 because it
seems easy to track these changes with a specific commit.

While working on this merge I realized that would make more sense to
create "AbstractComparison" to handle the rules that compare values.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-08-01 20:46:34 +02:00
Henrique Moody 57509520a4
Merge branch '1.1' 2018-08-01 20:22:37 +02:00
Andrei Drulchenko 19d6ec8939 fix IntVal() leading zero behavior 2018-08-01 16:06:54 +03:00
Henrique Moody 47f21596a5
Fix casting error in AbstractInterval classes
The classes that are children of "AbstractInterval" convert their values
before comparing them.

Because PHP tries to convert values when making comparisons and an
"DateTime" object cannot be converted to integer or float some
validations would result into PHP triggering an error like:

> Object of class DateTime could not be converted to int
> Object of class DateTime could not be converted to float

This commit prevents that to happen by verifying if both compared values
are scalar or not before comparing them with each other.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-07-28 05:01:58 +02:00
Axel Wargnier 4f3aa90d1f Fix postalCode for Portugal (PT) 2018-07-25 12:33:08 +02:00
Henrique Moody b26ed6bf39
Simplify "AbstractComposite" class
There is a log of magic in the "AbstractComposite" that allows an user
to add a rule in multiple ways, remove rules, verify if a rule is
inserted or not. Because of that the class has a lot of complexity and
for unnecessary reasons since we do not use these features internally.

The ideal would be to make this class immutable, however due to many
usages is not possible to do it now, but that is the plan for a near
future - hopefully.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-07-24 08:07:18 +02:00
Danilo Benevides 861216c690
Apply contribution guidelines to "Image" rule
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-07-23 21:55:54 +02:00
Henrique Moody fd2bae7352
Enforce the use of "@covers" annotation
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-07-23 21:37:38 +02:00
Henrique Moody e044e4b16e
Add some code standards for PHPUnit tests
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-07-23 20:46:18 +02:00