Commit graph

47 commits

Author SHA1 Message Date
Henrique Moody
cfeb01e89e
Bump respect/coding-standard from 4 to 5 2025-12-18 19:03:39 +01:00
Henrique Moody
7f66bcea10
Bump PHP support from 8.1 to 8.5
We want to release version 3.0 as fresh as possible, without having to
maintain backward compatibility with the previous versions. Because that
version will be on for some time, we decided it will be best to support
only PHP version 8.5 or higher.

Acked-by: Alexandre Gomes Gaigalas <alganet@gmail.com>
2025-12-18 19:03:38 +01:00
Henrique Moody
0066786fa7
Remove deprecated methods check() and validate()
We want to release version 3.0 as fresh as possible, without having to
maintain backward compatibility with the previous versions.

Acked-by: Alexandre Gomes Gaigalas <alganet@gmail.com>
2025-12-18 17:29:02 +01:00
Fabio Ribeiro
0e8ac1817b
Rename Result->isValid method 2025-03-31 21:29:37 +02:00
Henrique Moody
a3c197f600
Handle names via the Named rule
Because of how the validation engine works now [1], there's no reason to
keep adding names to each rule. Instead, create a single rule that
handles naming rules with a few other accessories. This change is not
necessarily simple, but it shrinks the `Rule` interface, and it's more
aligned with how the library works right now.

Personally, I think this API is much more straightforward than the
`setName()` method, as it's way more explicit about which rule we're
naming. Because of this change, the behaviour changed slightly, but it's
for the best.

Because of this change, I managed to remove a lot of code, but
unfortunately, it's quite a big-bang commit. It would be too complicated
to make it atomic since names are an intrinsic part of the library.

[1]: 238f2d506a
2024-12-26 23:10:19 +01:00
Henrique Moody
7cec227520
Create "Attribute" rule
With this change, any rule can be used as a PHP attribute. I have wanted
to implement this feature for a while, as it allows you to bind the
validation to a specific property and just validate the object
afterwards.
2024-12-13 03:49:29 +01:00
Henrique Moody
2485d54226
Create internal "Reducer" rule
When dealing with rules inside another rule, there are cases in which we
want to validate one or more rules but reduce the number of nested
results.
2024-12-12 18:11:04 +01:00
Henrique Moody
19db9cb07a
Create internal "Binder" rule
There are several cases in which we need to bind the name and template
of a rule to another. We need to do that because results need
information from rules to be created, and because results come from
rules, in some cases, it's not ideal (or possible) to change the
information after the result is created.
2024-12-12 16:20:08 +01:00
Henrique Moody
e6af762fe4
Rename "Validatable" to "Rule"
Besides the interface's name, everything already calls this type "Rule",
not "Validatable." This commit puts a stone on it and renames the
interface for better naming.
2024-12-05 19:32:14 +01:00
Henrique Moody
2aa5e39c54
Improve KeySet rule
After changes in the key-related rules, the KeySet rule became unusable.
Besides, when evaluating an input, it wasn't reporting every single
failure because it would not validate the items in the array if they had
missing or extra keys.

This commit will make several improvements to the rule. It will create
some not(keyExists($key)) rules for the extra keys, which makes the
error reporting much better. A limit of 10 additional keys will show up
when asserting an input with extra keys. I put that limit in place to
prevent the creation of too many rules.
2024-12-02 20:09:47 +01:00
Henrique Moody
a647a4737b
Split "Key" rules
Currently, the Key rule has a third parameter that allows the validation
of the wrapped rule to be optional, meaning that the validation will
only happen if the key exists. That parameter makes the rule harder to
understand at times.

I'm splitting the Key rule into Key, KeyExists, and KeyOptional. That
way, it becomes apparent when someone wants only to validate whether a
key exists or if they're going to validate the value of the key only
when it exists.

I deliberately didn't create an abstract class because those rules are
different enough not to have an abstraction. In fact, I can see myself
deleting the  "AbstractRelated" in the upcoming changes.

With these changes, the KeySet rule will not accept validating if the
key exists or validating the value only if the key exists. I should
refactor that soon, and I will likely need to create a common interface
for Key, KeyExists, and KeyOptional.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-03-04 00:06:18 +01:00
Henrique Moody
81befe8fa1
Move core rules into the "Core" namespace
That helps organize the code better, making it easier to spot the core
rule. It also helps not allow the Factory to load those rules, as the
new namespace is not registered in it.

Note that the "AbstractAge", "AbstractRelated", and "AbstractRule" were
not moved. I want to do that only when I refactor them.

After I moved classes, I realized that "Comparison" and "FilteredString"
had no tests. I created the tests, and while I did that, I spotted two
bugs:

* The "Equals" rule was failing when comparing non-scalar wth scalar
  values;

* The "Equals" and "Identical" rules were not working correctly because
  "Comparison" was converting their values.

I fixed those bugs in this commit.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-03-03 16:12:58 +01:00
Henrique Moody
74fd472b66
Do not allow cloning Result with different parameters
So far, I haven't seen any real case for that, but it's not like I have
a strong case for not allowing that, I just want to keep the code clean.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-02-23 01:52:50 +01:00
Henrique Moody
c04034c2a4
Update the validation engine of composite-based rules
This change will also make the composite-based rules require at least
two rules in their constructor because those rules do not make sense
with only one rule.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-02-23 00:56:30 +01:00
Henrique Moody
99dc8720ce
Update the validation engine of wrapper-based rules
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-02-22 18:44:32 +01:00
Henrique Moody
7199c5e440
Use convention to idenfity custom templates
With this convention, it's much simpler to identify whether an exception
has a custom template or if that template came from the rule itself.
This commit is a preparation for further changes.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-02-14 18:12:18 +01:00
Henrique Moody
c6677fd7ce
Remove duplicated code
We expect that every rule that has a custom template to use that
template instead of the standard one. This change ensures that happens.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-02-09 19:53:45 +01:00
Henrique Moody
02b70bf1cb
Move Template to the Message namespace
That way, everything related to messages would stay in the same
namespace.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-02-09 19:50:25 +01:00
Henrique Moody
84f1d3296e
Remove "@throws" annotations
I don't find much use for those, and it's hard to be consistent with all
places that could throw an exception.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-29 23:51:42 +01:00
Henrique Moody
e4f2c8154a
Use PHP attributes to define templates
Creating a specific exception for each rule adds a painful overhead. If
you want to make a custom message for your rule, you will need to create
an exception and then register that exception namespace to be able to
use it—all that is just for customizing the message of your rule.

Having different namespaces also implies that you need to fetch the
exception of the rule from another directory to change it. As Uncle Bob
said, "Classes that change together belong together. Classes that are
not reused together should not be grouped."

This commit will drastically change this library, moving all the
templates from the exceptions to the rules. Consequently, the Factory
becomes much simpler, and the library gets a bit smaller, too.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-29 23:43:57 +01:00
Henrique Moody
2b59e3df49
Only pass the necessary parameters to the exceptions
Currently, we convert the properties of a rule into parameters and pass
them to the exceptions. That complicates things for a few reasons:

1. The exception knows too much: there's a lot of information in an
   object, and the exception would only need a few parameters to work
   correctly.

2. Any variable change becomes a backward compatibility break: if we
   change the name of the variable type in a rule, even if it's a
   private one, we may need to change the template, which is a backward
   compatibility break.

3. The factory is bloated because of introspection tricks: it reads the
   properties from the class, even from the parent, and then passes it
   to the exception.

Of course, that means we introduce another method to `Validatable`, but
in most cases, extending `AbstractRule` is enough to create a new rule.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-29 23:30:38 +01:00
Henrique Moody
dd896bb12d
Move template definitions to the rules
It's easier to identify the reason for choosing a specific message in
the rule than in the exception. The same goes for the key we use to
determine the templates.

This change will simplify the `ValidationException` because it will
already receive the template it needs to use. As a consequence, the
`Factory` also becomes more straightforward.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-29 23:17:27 +01:00
Henrique Moody
e983e52663
Make properties readonly
That will make it clear that we should not overwrite some properties.

Because of this change, I've made a few refactorings here and there.
It's nice to see that I've spotted some issues just because I was
setting some properties as `readonly`.

There are a few properties that I would like to make read-only, but to
do that I'd need to refactor a lot of code, so for now, I'm keeping it
as is.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-28 01:24:15 +01:00
Henrique Moody
9a13c9fb03
Update coding standards
This change will bring many breaking changes. The good thing is that we
can finally use more modern resources available in PHP.

I can imagine that's not a popular change since it will bring many
breaking changes to users, but we shouldn't be stuck in time because of
that. Using some of those features will make it easier to contribute to
the project. At least, I hope so.

There are still some useless doc-blocks, and we're not using "readonly"
properties when we could. I aim to send those changes soon.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-01-28 00:22:41 +01:00
Alexandre Gomes Gaigalas
fc8230acef Make KeySet impossible to wrap in not(), fix structure message
The use case for negating a keyset is very confusing, and can
lead to validators that don't do what they expect.

This commit introduces NonNegatable rules, which will throw
a Component exception if you try to wrap them in `Not`.

This change was necessary to ensure proper message reporting
when extra keys exist on the keyset.

This fixes #1349
2023-02-19 00:19:10 -03:00
Alexandre Gomes Gaigalas
727e7ccbfa Increase phpstan level from 7 to 8
- Fixed all phpstan errors and ignoreds.
 - False positives now have a "Why:" comment on phpstan config.
2023-02-19 00:19:10 -03:00
Alexandre Gomes Gaigalas
ab3732f91f Use SPDX IDs for licensing
SPDX IDs are shorter than licensing notes previously used, and
adhere better to FOSS standards. It is also machine-readable.
2023-02-19 00:19:10 -03:00
Alexandre Gomes Gaigalas
15f148da24 Dusting off. See CHANGELOG.md for more details on this commit 2023-02-13 03:59:11 -03:00
Ondřej Vodáček
bf4082d208
fix phpdoc type for KeySet constructor (#1365)
by @voda
2022-01-19 12:40:08 +02:00
Henrique Moody
3145426472
Update version of "respect/coding-standard"
With that update, we will be fully following PSR-12.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2020-07-21 22:54:41 +02:00
Henrique Moody
ef8a8f4b27
Turn LICENSE file into plain/text
There is no need for that file to be a Markdown, and it can be a plain
text file.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2019-05-23 16:21:34 +02:00
Henrique Moody
272f18dcf5
Apply "Symfony.Functions.ScopeOrder"
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2019-05-11 20:00:19 +02:00
Henrique Moody
051866f75a
Make properties of "AbstractRelated" private
Following what is happening with pretty much every class in this
library, this commit will make the public properties of
"AbstractRelated" private.

Because other objects use some of those public properties, this commit
will also implement a couple of methods in "AbstractRelated" so they can
access the values they need.

This commit will also remove the method "decision" that makes dynamic
calls to "assert()," "check()," and "validate()" methods.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2019-05-05 16:54:04 +02:00
Henrique Moody
6040ddee42
Fix the case of the "@inheritDoc" tag
According to the official documentation [1] the correct way of writing
the "inheritDoc" tag is with the uppercase "D".

[1]: https://docs.phpdoc.org/guides/inheritance.html

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2019-04-05 19:02:12 +02:00
Henrique Moody
99b912ff87
Apply "SlevomatCodingStandard.ControlStructures.DisallowYodaComparison"
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2019-02-09 14:19:22 +01:00
Henrique Moody
c30603759e
Apply "SlevomatCodingStandard.TypeHints.TypeHintDeclaration"
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2019-02-09 14:09:28 +01:00
Henrique Moody
2e29b9e8c7
Fix "@author" annotations
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>
2018-11-25 18:04:59 +01:00
Henrique Moody
b696070874
Refactor "KeySet" rule
Do not extend AllOf exception, but instead extend "AbstractWrapper".
2018-03-03 18:59:36 +01:00
Henrique Moody
c80524b457
Method assert() should not have a return value
One this method should throw an exception when the input is not valid,
returning `TRUE` when it succeeds is not really consistent.
2018-01-28 17:38:40 +01:00
Henrique Moody
30dc089565
Method check() should not have a return value
One this method should throw an exception when the input is not valid,
returning `TRUE` when it succeeds is not really consistent.
2018-01-28 17:38:40 +01:00
Henrique Moody
ccf60f0ee3
Method validate() should always return a boolean 2018-01-28 17:38:36 +01:00
Henrique Moody
ef975629f3
Changes on PHP-CS-Fixer configuration
Because of `declare(strict_types=1)` some changes were necessary.
2018-01-04 17:59:37 +01:00
Henrique Moody
4d72af312f
Update PHP-CS-Fixer settings 2016-10-30 10:39:23 +01:00
Emmerson
d9a4c78b16 Fixes 'KeySet' rule when input is not array type 2016-05-05 13:01:04 +02:00
Henrique Moody
9c49dd3bcf Use short array syntax 2015-10-17 22:56:32 -03:00
Henrique Moody
418935c206 Fix strict standards on KeySet rule
An overwritten method must have the same signature of the parent method.
2015-07-28 17:04:09 -03:00
Henrique Moody
7d9d19009a Create KeySet rule 2015-07-21 12:35:44 -03:00