mirror of
https://github.com/Respect/Validation.git
synced 2024-05-20 23:26:42 +02:00
fc8230acef
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
50 lines
1.2 KiB
PHP
50 lines
1.2 KiB
PHP
<?php
|
|
|
|
/*
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Validation\Exceptions;
|
|
|
|
use function count;
|
|
|
|
/**
|
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
|
*/
|
|
final class KeySetException extends GroupedValidationException implements NonOmissibleException
|
|
{
|
|
public const STRUCTURE = 'structure';
|
|
public const STRUCTURE_EXTRA = 'structure_extra';
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
protected $defaultTemplates = [
|
|
self::MODE_DEFAULT => [
|
|
self::NONE => 'All of the required rules must pass for {{name}}',
|
|
self::SOME => 'These rules must pass for {{name}}',
|
|
self::STRUCTURE => 'Must have keys {{keys}}',
|
|
self::STRUCTURE_EXTRA => 'Must not have keys {{extraKeys}}',
|
|
],
|
|
];
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
protected function chooseTemplate(): string
|
|
{
|
|
if (count($this->getParam('extraKeys'))) {
|
|
return self::STRUCTURE_EXTRA;
|
|
}
|
|
|
|
if (count($this->getChildren()) === 0) {
|
|
return self::STRUCTURE;
|
|
}
|
|
|
|
return parent::chooseTemplate();
|
|
}
|
|
}
|