mirror of
https://github.com/Respect/Validation.git
synced 2026-03-16 23:35:45 +01:00
Currently, the templates that a user provides when running `assert()` can significantly impact how the message is displayed. Because of this, the formatters become complex as they all need to handle similar conditions to format results. This commit changes this behaviour, letting only the `InterpolationRenderer` handle the templates. This makes the code simpler and allows people to use the `InterpolationRenderer` directly, without needing to figure out how to handle templates. Thinking about it further, I believe handling templates is a concern for the `Renderer` anyway, and this will open the way to other improvements using the renderer. I also removed the exception that is thrown when the template is not a string, because I think that after validation has failed, we should not throw any other exceptions, as that could cause unexpected errors for users.
93 lines
4 KiB
PHP
93 lines
4 KiB
PHP
<?php
|
|
|
|
/*
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
test('Default', catchAll(
|
|
fn() => v::named(v::stringType(), 'Potato')->assert(12),
|
|
fn(string $message, string $fullMessage, array $messages) => expect()
|
|
->and($message)->toBe('Potato must be a string')
|
|
->and($fullMessage)->toBe('- Potato must be a string')
|
|
->and($messages)->toBe(['stringType' => 'Potato must be a string']),
|
|
));
|
|
|
|
test('Inverted', catchAll(
|
|
fn() => v::not(v::named(v::intType(), 'Zucchini'))->assert(12),
|
|
fn(string $message, string $fullMessage, array $messages) => expect()
|
|
->and($message)->toBe('Zucchini must not be an integer')
|
|
->and($fullMessage)->toBe('- Zucchini must not be an integer')
|
|
->and($messages)->toBe(['notIntType' => 'Zucchini must not be an integer']),
|
|
));
|
|
|
|
test('Template in Validator', catchAll(
|
|
fn() => v::named(v::stringType(), 'Eggplant')
|
|
->setName('Mushroom')
|
|
->assert(12),
|
|
fn(string $message, string $fullMessage, array $messages) => expect()
|
|
->and($message)->toBe('Eggplant must be a string')
|
|
->and($fullMessage)->toBe('- Eggplant must be a string')
|
|
->and($messages)->toBe(['stringType' => 'Eggplant must be a string']),
|
|
));
|
|
|
|
test('With bound', catchAll(
|
|
fn() => v::named(v::attributes(), 'Pumpkin')->assert(null),
|
|
fn(string $message, string $fullMessage, array $messages) => expect()
|
|
->and($message)->toBe('Pumpkin must be an object')
|
|
->and($fullMessage)->toBe('- Pumpkin must be an object')
|
|
->and($messages)->toBe(['attributes' => 'Pumpkin must be an object']),
|
|
));
|
|
|
|
test('With key that does not exist', catchAll(
|
|
fn() => v::key('vegetable', v::named(v::stringType(), 'Paprika'))->assert([]),
|
|
fn(string $message, string $fullMessage, array $messages) => expect()
|
|
->and($message)->toBe('Paprika must be present')
|
|
->and($fullMessage)->toBe('- Paprika must be present')
|
|
->and($messages)->toBe(['vegetable' => 'Paprika must be present']),
|
|
));
|
|
|
|
test('With property that does not exist', catchAll(
|
|
fn() => v::key('vegetable', v::named(v::stringType(), 'Broccoli'))->assert((object) []),
|
|
fn(string $message, string $fullMessage, array $messages) => expect()
|
|
->and($message)->toBe('Broccoli must be present')
|
|
->and($fullMessage)->toBe('- Broccoli must be present')
|
|
->and($messages)->toBe(['vegetable' => 'Broccoli must be present']),
|
|
));
|
|
|
|
test('With key that fails validation', catchAll(
|
|
fn() => v::key('vegetable', v::named(v::stringType(), 'Artichoke'))->assert(['vegetable' => 12]),
|
|
fn(string $message, string $fullMessage, array $messages) => expect()
|
|
->and($message)->toBe('Artichoke must be a string')
|
|
->and($fullMessage)->toBe('- Artichoke must be a string')
|
|
->and($messages)->toBe(['vegetable' => 'Artichoke must be a string']),
|
|
));
|
|
|
|
test('With nested key that fails validation', catchAll(
|
|
fn() => v::key(
|
|
'vegetables',
|
|
v::named(
|
|
v::create()
|
|
->key('root', v::stringType())
|
|
->key('stems', v::stringType())
|
|
->keyExists('fruits'),
|
|
'Vegetables',
|
|
),
|
|
)->assert(['vegetables' => ['root' => 12, 'stems' => 12]]),
|
|
fn(string $message, string $fullMessage, array $messages) => expect()
|
|
->and($message)->toBe('`.vegetables.root` (<- Vegetables) must be a string')
|
|
->and($fullMessage)->toBe(<<<'FULL_MESSAGE'
|
|
- Vegetables must pass all the rules
|
|
- `.vegetables.root` must be a string
|
|
- `.vegetables.stems` must be a string
|
|
- `.vegetables.fruits` must be present
|
|
FULL_MESSAGE)
|
|
->and($messages)->toBe([
|
|
'__root__' => 'Vegetables must pass all the rules',
|
|
'root' => '`.vegetables.root` must be a string',
|
|
'stems' => '`.vegetables.stems` must be a string',
|
|
'fruits' => '`.vegetables.fruits` must be present',
|
|
]),
|
|
));
|