Commit graph

12 commits

Author SHA1 Message Date
Alexandre Gomes Gaigalas
1b584d88a3 Handle more collision scenarios on NestedArrayFormatter
See #1668 for more info.

There is a new test "Deep name collision" on AllOfTest that
exemplifies the kind of collision this change solves.

Some extra `__root__` keys needed to be added to a select few
other scenarios for consistency.
2026-02-02 20:21:14 +00:00
Alexandre Gomes Gaigalas
d9cdc118b2 Introduce REUSE compliance
This commit introduces REUSE compliance by annotating all files
with SPDX information and placing the reused licences in the
LICENSES folder.

We additionally removed the docheader tool which is made obsolete
by this change.

The main LICENSE and copyright text of the project is now not under
my personal name anymore, and it belongs to "The Respect Project
Contributors" instead.

This change restores author names to several files, giving the
appropriate attribution for contributions.
2026-01-21 06:28:11 +00:00
Alexandre Gomes Gaigalas
cd96d01364 Fix message overriding bug in NestedArrayFormatter
This commit resolves an issue where validation messages would overwrite
each other when multiple validators failed on the same path or key
(e.g., within an `Each` or `Key` validator).

Changes to `NestedArrayFormatter`:
- Implemented a merge strategy: Key collisions now result in a list of
  messages instead of the last message winning.
- Improved handling of mixed key types: When both numeric and string
  keys are present (common in composite validators), numeric keys are now
  replaced by the validator's ID (e.g., `arrayType`, `equals`) to provide
  meaningful, distinct keys.
- Preserved list behavior: Purely numeric key sets are treated as lists,
  maintaining their sequence without re-keying logic.
- Refactored the class to use smaller, single-purpose methods and
  `array_reduce` for clarity.

Tests:
- Updated feature tests (`EachTest`, `AttributesTest`, etc.) to expect the
  full set of validation errors.
- Enhanced `NestedArrayFormatterTest` with scenarios for key collisions,
  mixed keys, and ID substitution.
2026-01-12 10:42:11 +00:00
Henrique Moody
b5ad7aa47a
Make Validator immutable
Mutable objects can be challenging to work with in larger codebases
because different parts of a system may modify the same instance, making
it difficult to trace where and when changes occurred. This becomes
especially problematic when debugging unexpected behaviour.

By making `Validator` immutable, we ensure that adding rules via
`with()` returns a new instance rather than mutating the original, and
we use the `with()` method inside `__call()`, making every call to a
rule into a clone of the current `Validator`.

This provides several benefits:

1. Predictability: A `Validator` instance will always behave the same
   way throughout its lifetime, regardless of what other parts of the
   codebase do.

2. Safe dependency injection: Users can now confidently inject a base
   `Validator` from a DI container, knowing that any modifications made
   elsewhere will not affect their instance.

3. Easier debugging: Since validators cannot be mutated after creation,
   there's no need to track down where an unexpected rule was added.

4. Reusability: Users can create an initial `Validator` with some base
   rules and reuse it by just adding new rules to the chain without
   affecting the base `Validator`.
2026-01-02 15:45:23 +01:00
Henrique Moody
137c74c5b3
Change how we trace the path of results
Currently, we’re using scalar values to trace paths. The problem with
that approach is that we can’t create a reliable hierarchy with them, as
we can’t know for sure when a path is the same for different rules. By
using an object, we can easily compare and create a parent-child
relationship with it.

While making these changes, I deemed it necessary to also create objects
to handle Name and Id, which makes the code simpler and more robust. By
having Name and Path, we can create specific stringifiers that allow us
to customise how we render those values.

I didn’t manage to make those changes atomically, which is why this
commit makes so many changes. I found myself moving back and forth, and
making all those changes at once was the best solution I found.
2025-12-20 22:19:17 +01:00
Henrique Moody
cfeb01e89e
Bump respect/coding-standard from 4 to 5 2025-12-18 19:03:39 +01:00
Henrique Moody
d3239e878d
Change how we're writing Pest tests
The problem with the current approach is that the "expect()" calls
happen inside "tests/Pest.php". That means that when something fails, we
can't easily know which exact expectation has failed.

This commit will change the helper functions, and will make the tests
more verbose, but event with that, the developer experience is better.
2025-12-18 14:02:33 +01:00
Henrique Moody
1915b6fff7
Use paths to identify when a rule fails
When nested-structural validation fails, it's challenging to identify
which rule failed from the main exception message. A great example is
the `Issue796Test.php` file. The exception message says:

host must be a string

But you're left unsure whether it's the `host` key from the `mysql` key
or the `postgresql` key.

This commit changes that behaviour by introducing the concept of "Path."
The `path` represents the path that a rule has taken, and we can use it
in structural rules to identify the path of an array or object.

Here's what it looks like before and after:

```diff
-host must be a string
+`.mysql.host` must be a string
```

Because paths are a specific concept, I added a dot (`.`) at the
beginning of all paths when displaying them. I was inspired by the `jq`
syntax. I also added backticks around paths to distinguish them from any
other value.

I didn't manage to fix a test, and I skipped it instead of fixing it
because I want to make changes in how we display error messages as
arrays, and it will be easier to fix it then.
2024-12-27 23:28:35 +01:00
Henrique Moody
a0d6355980
Update some templates and improve tests
Some templates were a bit confusing, and I would like to favour adding
the `{{name}}` at the beginning of the templates as it helps when
reading nested messages.

I also deleted the regression tests for issue #1348, because it's a
non-issue, actually. The best approach to that problem is indeed using
`When` insteaf of `OneOf`.
2024-12-27 15:55:55 +01:00
Henrique Moody
aa633db46a
Improve format of Pest files
I added some trailing commas to the files.
2024-12-22 06:53:36 +01:00
Henrique Moody
873be39105
Fix grammar error in the "AllOf" rule 2024-12-20 17:06:53 +01:00
Henrique Moody
94daa8d669
Use Pest instead of PHPT files
Although I love PHPT files, and I've done my fair share of making it
easier to write them in this library, they're very slow, and running
them has become a hindrance.

I've been fidgeting with the idea of using Pest for a while, and I think
it's the right tool for the job. I had to create a couple of functions
to make it easier to run those tests, and now they're working really
alright.

I migrated all the PHPT files into Pest files -- I automated most of the
work with a little script using "nikic/php-parser"; this commit should
contain all the previous PHPT tests as Pest tests.

The previous integration tests would take sixteen seconds, and the Pest
tests take less than a second.
2024-12-16 17:07:47 +01:00