respect-validation/library/Exceptions/KeySetException.php
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

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();
}
}