Merge pull request #8499 from kenjis/feat-Validation-run-DB-connection

feat: Validation::run() accepts DB connection
This commit is contained in:
kenjis 2024-02-07 10:18:33 +09:00 committed by GitHub
commit 846466e82a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 37 additions and 13 deletions

View File

@ -233,6 +233,7 @@ parameters:
- I18n
Validation:
- HTTP
- Database
View:
- Cache
skip_violations:

View File

@ -14,6 +14,7 @@ declare(strict_types=1);
namespace CodeIgniter\Validation;
use Closure;
use CodeIgniter\Database\BaseConnection;
use CodeIgniter\HTTP\Exceptions\HTTPException;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\Method;
@ -128,14 +129,11 @@ class Validation implements ValidationInterface
* Runs the validation process, returning true/false determining whether
* validation was successful or not.
*
* @param array|null $data The array of data to validate.
* @param string|null $group The predefined group of rules to apply.
* @param string|null $dbGroup The database group to use.
*
* @TODO Type ?string for $dbGroup should be removed.
* See https://github.com/codeigniter4/CodeIgniter4/issues/6723
* @param array|null $data The array of data to validate.
* @param string|null $group The predefined group of rules to apply.
* @param array|BaseConnection|non-empty-string|null $dbGroup The database group to use.
*/
public function run(?array $data = null, ?string $group = null, ?string $dbGroup = null): bool
public function run(?array $data = null, ?string $group = null, $dbGroup = null): bool
{
if ($data === null) {
$data = $this->data;

View File

@ -13,6 +13,7 @@ declare(strict_types=1);
namespace CodeIgniter\Validation;
use CodeIgniter\Database\BaseConnection;
use CodeIgniter\HTTP\RequestInterface;
/**
@ -24,11 +25,11 @@ interface ValidationInterface
* Runs the validation process, returning true/false determining whether
* validation was successful or not.
*
* @param array|null $data The array of data to validate.
* @param string|null $group The predefined group of rules to apply.
* @param string|null $dbGroup The database group to use.
* @param array|null $data The array of data to validate.
* @param string|null $group The predefined group of rules to apply.
* @param array|BaseConnection|non-empty-string|null $dbGroup The database group to use.
*/
public function run(?array $data = null, ?string $group = null, ?string $dbGroup = null): bool;
public function run(?array $data = null, ?string $group = null, $dbGroup = null): bool;
/**
* Check; runs the validation process, returning true or false

View File

@ -85,6 +85,17 @@ class DatabaseRelatedRulesTest extends CIUnitTestCase
$this->assertTrue($this->validation->run($data));
}
public function testIsUniqueWithDBConnection(): void
{
$db = db_connect();
$this->validation->setRules(['email' => 'is_unique[user.email]']);
$data = ['email' => 'derek@world.co.uk'];
$result = $this->validation->run($data, null, $db);
$this->assertTrue($result);
}
public function testIsUniqueWithInvalidDBGroup(): void
{
$this->expectException(InvalidArgumentException::class);

View File

@ -87,6 +87,8 @@ Interface Changes
the ``ResponseInterface::setCookie()`` has been fixed from ``''`` to ``0``.
- **Logger:** The `psr/log <https://packagist.org/packages/psr/log>`_ package has
been upgraded to v3.0.0.
- **Validation:** The method signature of ``ValidationInterface::run()`` has been
changed. The ``?string`` typehint on the ``$dbGroup`` parameter was removed.
.. _v450-method-signature-changes:
@ -136,6 +138,8 @@ Others
that implements the PSR-3 interface have been fixed. The ``bool`` return
types are changed to ``void``. The ``$message`` parameters now have
``string|Stringable`` types.
- **Validation:** The method signature of ``Validation::run()`` has been
changed. The ``?string`` typehint on the ``$dbGroup`` parameter was removed.
.. _v450-removed-deprecated-items:
@ -316,8 +320,12 @@ See :ref:`Using CodeIgniters Model <model-update-only-changed>` for details.
Libraries
=========
- **Validation:** Added the new rule ``field_exists`` that checks the filed
exists in the data to be validated.
- **Validation:**
- Added the new rule ``field_exists`` that checks the filed exists in the
data to be validated.
- The ``$dbGroup`` parameter of ``Validation::run()`` now accepts not only
a database group name, but also a database connection instance or an array
of database settings.
Helpers and Functions
=====================

View File

@ -213,6 +213,11 @@ using them, upgrade your code. See :ref:`ChangeLog <v450-removed-deprecated-item
Breaking Enhancements
*********************
- The method signatures of ``Validation::run()`` and ``ValidationInterface::run()``
have been changed. The ``?string`` typehint on the ``$dbGroup`` parameter was
removed. Extending classes should likewise remove the parameter so as not to
break LSP.
Project Files
*************