mirror of
https://github.com/Respect/Validation.git
synced 2026-03-18 08:09:51 +01:00
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`.
59 lines
2.2 KiB
PHP
59 lines
2.2 KiB
PHP
<?php
|
|
|
|
/*
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
test('https://github.com/Respect/Validation/issues/1469', catchAll(
|
|
fn() => v::init()
|
|
->arrayVal()
|
|
->keySet(
|
|
v::key(
|
|
'order_items',
|
|
v::init()
|
|
->arrayVal()
|
|
->each(
|
|
v::keySet(
|
|
v::key('product_title', v::stringVal()->notBlank()),
|
|
v::key('quantity', v::intVal()->notBlank()),
|
|
),
|
|
)
|
|
->notBlank(),
|
|
),
|
|
)
|
|
->assert([
|
|
'order_items' => [
|
|
[
|
|
'product_title' => 'test',
|
|
'quantity' => 'test',
|
|
],
|
|
['product_title2' => 'test'],
|
|
],
|
|
]),
|
|
fn(string $message, string $fullMessage, array $messages) => expect()
|
|
->and($message)->toBe('`.order_items.0.quantity` must be an integer value')
|
|
->and($fullMessage)->toBe(<<<'FULL_MESSAGE'
|
|
- Each item in `.order_items` must be valid
|
|
- `.order_items.0` validation failed
|
|
- `.order_items.0.quantity` must be an integer value
|
|
- `.order_items.1` contains both missing and extra keys
|
|
- `.order_items.1.product_title` must be present
|
|
- `.order_items.1.quantity` must be present
|
|
- `.order_items.1.product_title2` must not be present
|
|
FULL_MESSAGE)
|
|
->and($messages)->toBe([
|
|
'keySet' => [
|
|
'__root__' => 'Each item in `.order_items` must be valid',
|
|
0 => '`.order_items.0.quantity` must be an integer value',
|
|
1 => [
|
|
'__root__' => '`.order_items.1` contains both missing and extra keys',
|
|
'product_title' => '`.order_items.1.product_title` must be present',
|
|
'quantity' => '`.order_items.1.quantity` must be present',
|
|
'product_title2' => '`.order_items.1.product_title2` must not be present',
|
|
],
|
|
],
|
|
]),
|
|
));
|