- Only create objects that are instantiable.
- Validate if validator is a valid Zend Validator after its creation.
- Parse messages from Zend Validator on "assert()" and "check()."
- Upgrade supported version of Zend Validator: version 2.0 has some PHP
deprecations therefore there is no used to support it.
Co-authored-by: Danilo Correa <danilosilva87@gmail.com>
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
PHPT files are amazing, but they are also expensive to execute. Having
just one single file running the necessary tests is best than having one
file per test.
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
The sorted rule accepts a callback on its constructor that may be used
to filter values from inside the input. However, with the "Call" rule
one can archive almost the same result. Besides that particular
characteristic, its constructor accepts a boolean value to determine
whether the sorting is ascending or descending.
This commit will remove the callback from the constructor and replace
the boolean by a string which can be "ASC" or "DESC."
Along with those changes, this change will make a few more improvements:
- Make the exception message specific about the sorting direction;
- Allow the rule to validate also strings;
- Update documentation.
Co-authored-by: Danilo Correa <danilosilva87@gmail.com>
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
After writing the integration tests of the "Vowel" rule we noticed that
it is generating extra double-quotes for the "{{additionalChars}}"
template placeholder.
Besides, it considers any whitespace as valid. That is because in the
past Validation would always consider spaces as a valid.
This commit will also remove the extra double-quotes and do not allow
whitespaces to be considered as valid.
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
After writing the integration tests of the "StartsWith" rule we noticed
that it is generating extra parentheses for the "{{startValue}}"
template placeholder.
This commit will also remove those extra parentheses.
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
After writing the integration tests of the "Space" rule we noticed that
it is generating extra double-quotes for the "{{additionalChars}}"
template placeholder.
This commit will also remove those extra double-quotes.
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
This commit will add a couple of rules that we already use but also
introduce new ones and make sure all the code is in accordance with the
new coding standards.
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
After writing the integration tests of the "Punct" rule we noticed that
it is generating extra double-quotes for the "{{additionalChars}}"
template placeholder.
This commit will also remove those extra double-quotes.
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
This commit will also remove the second argument from the constructor of
the rule because there is no much use for that, instead if will simply
perform the CountryCode validation.
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
After writing the integration tests of the "Graph" rule we noticed that
it is generating extra double-quotes for the "{{additionalChars}}"
template placeholder.
This commit will also remove those extra double-quotes.
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
After seeing the integration tests of the "Consonant" rule we noticed
that it is generating extra double-quotes for the "{{additionalChars}}"
template placeholder.
This commit will also remove those extra double-quotes.
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
After writing the integration tests of the "Cntrl" rule we noticed that
it is generating extra double-quotes for the "{{additionalChars}}"
template placeholder.
This commit will also remove those extra double-quotes.
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
Whenever is possible it is better to declare our classes as final. The
PHPUnit tests should not be extended, therefore there is no reason for
them to not be final.
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
The tool we used to verify whether the code base has the correct coding
standard was removed [1].
This commit will set up one that works best for us and will also make
sure we have fully compliant to PS1 and PSR2.
[1]: ffec95acda
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
The data in this commit has been gathered, scrapped, organized and
treated based on the Emoji Unicode V11 specification [1].
[1]: https://unicode.org/Public/emoji/11.0/emoji-test.txt
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
The "--EXPECTF--" is supposed to be used when the output of "--FILE--"
is random. In many cases, we are using "--EXPECTF--" section while we
could use "--EXPECT--" instead.
This commit will replace the usages of "--EXPECTF--" by "--EXPECT--"
when the output does "--FILE--" is not random.
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
Because some classes extend the "Regex" class this commit will also
change the implementation of those classes to use "Regex" by composition
instead of extending the class.
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
While making sure that Validation uses PHP strict types [1] the compared
value in the "Contains" rule was cast to string. That is problematic
when someone wants to enforce the operation to compare if the input has
a value identical to the expected value.
This commit will fix that by not casting the expected value but also
make sure that string comparison will not be used when it's not a
string.
[1]: ef975629f3
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit will do many different things, but they are all improvements
to the "Ip" rules:
* Remove passing variables by reference: the "Ip" class uses that a lot
to define the start address, end address, and the mask used to
validate a network range;
* Remove double-typed argument from the constructor: the class "Ip"
class has only one argument that can be either a string with the range
of the IP or an integer with options for the "filter_var()" function.
This commit will split it into two different arguments, each of them
used for one of this functionalities;
* Update documentation to show how to validate IPv6.
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
Because traits are behaviors that are added to a class, it makes sense
to name them with the behavior that they add the classes that use them.
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
Due to the current status of the development of the library, it seems
like we will be supporting version 1.1 for a long time. Even when we
release version 2.0 we will still give support for version 1.1 for a
while.
This commit will make sure that version 1.1 is fully supported for PHP
7.2 and 7.3. Also, it will remove the support for HHVM since it will not
keep the compatibility with PHP anymore [1].
In order to make that happen, this commit will create a TestCase from
Validation so we can use the same API to create mocks in both PHPUnit
versions 4.0 and 5.0.
During the development of this commit, I noticed that PHPUnit 4.0 had
issues to mock "SplFileInfo" and for that reason, this commit will also
replace those mocks by "SplFileInfo" instances.
[1]: https://hhvm.com/blog/2018/09/12/end-of-php-support-future-of-hack.html
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
All the PHP files have the "@author" annotation to make clear which
people have contributed to that specific file, however, there is no such
thing in the PHPT files.
This commit will add the "CREDITS" section to the PHPT files. The list
of authors is created from the output of the `git blame` command.
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit will make sure that every class, interface, or trait will
have the "@author" annotation in it.
In order to create a list of authors, I used the "git blame" command,
which means that if someone changed or even created the file but does
not have any remaining line will not be shown in the list; it's a
trade-off worth but it is worth it. The other way to do it would be
carefully checking each file.
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
Some classes and one trait had some mismatch values for their "@author"
annotation and this commit will fix the mismatch putting the correct
authors.
I used the "git blame" command to find out which people changed the file
and created a list based on that information.
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
The argument "parameters" which is meant to give the wrapped rule's
exception some extra information does not always need to be defined, for
example when the exception does not need any parameter.
The current version of "AbstractEnvelope" required that argument and the
rules that are extending this class are passing an empty array.
This commit will make the "parameters" argument optional. As an extra,
it will make sure the tests from the children of "AbstractEnvelope" are
also covering their parent.
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
Removes the errant space in the CA PostalCode regex.
This space was introduced by 5a067fa.
It also adds unit tests for the CA PostalCode to validate
the fix.
Reviewed-by: Emmerson Siqueira <emmersonsiqueira@gmail.com>
Since the library doesn't need to give support to version 5.4 or less of
PHP using variadics in the constructor of "AbstractFilterRule" seems
better than doing the whole validation.
This commit will also apply the contribution guidelines to
"AbstractFilterRule" and use a better naming for it.
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit will allow validation of UUIDs with versions 1, 3, 4 and 5.
Version 2 was not implemented since its adoption is not that broad and
we couldn't find many details about it.
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
Signed-off-by: Michael Weimann <mail@michael-weimann.eu>
There shouldn't be possible to consider a non-string value as a valid
email anyways, but the real problem is that the "RFCValidation" from
"egulias/email-validator" casts the input as a string which makes PHP
trigger an error.
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
The "Yes" rule extends "Regex" rule. The only reasons why that is useful
is because "Yes" uses regular expressions to validate the inputs.
However, the "Yes" rule is way more complex simply validating a regular
expressing and having "Regex" as its parent is also a little bit
misleading.
This commit will:
* Remove unnecessary inheritance from "Yes" rule;
* Improve the documentation of the "Yes" rule;
* Enhance the unit tests of the "Yes" rule.
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
The "AbstractRegexRule" seems like an unnecessary overhead on top of the
"AbstractFilterRule". In fact, the "Phone" and "Uuid" rule should not
even be children of "AbstractFilterRule".
This commit will remove the "AbstractRegexRule" and replace it with
"AbstractFilterRule" on the rules that are depending on it. As for the
rules that don't depend on it it will replace by "AbstractRule" like
most of the other rules.
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
The "AbstractCtypeRule" class is an abstraction that is purely an
overhead on top of the "AbstractFilterRule". Not having a real reason to
exist I thought would be better to just remove it and replace its usage
by "AbstractFilterRule".
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This file should have been removed by when the contribution guidelines
were applied to "Image" but for some reason, we missed that.
Reference: 861216c690
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
When a template is set for a chain of rules, does not really matter
which messages the chain can have, the only message to be used should be
the one based on the defined template.
This commit set the same template of a parent rule to its children's
exception. Our first thought was to set the template to its children
however that would mean that if another rule would be added to the chain
we would have to set it as well. Doing that to the children's exception
make sure we only do that once.
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
Use the same condition to not include child exception into the iterator
to also not show the message of the main exception when using
"getFullMessage()".
The method created to validate that condition should probably be in the
"ValidationException" itself, but this commit is meant to be a bug fix,
which will be released in a PATH version, and creating this public
method would imply that a MINOR version should be released.
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
The commit 54d17ab tried to fix the that "IntVal" would pass with values
that are not considered as an integer, but it does not change
Even thought the commit 54d17ab fixes part of the wrong behaviour it
left out the filter for boolean values.
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
Also removed the possibility of validating keys once it's possible to
reach the same behavior by combining this rule with "Call" rule.
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
Values like "500.00" or 1.0 should not be considered as integer values
even though there is no data loss when they're converted to integer.
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
Also introduce "uopz" extension in order to create tests for "Uploaded"
rule.
Signed-off-by: Paul Karikari <paulkarikari1@gmail.com>
Co-authored-by: Henrique Moody <henriquemoody@gmail.com>