-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add Validation Strict Rules #5445
feat: add Validation Strict Rules #5445
Conversation
The behavior changes are only the following: $ diff -u FormatRulesTest.php StrictRules/FormatRulesTest.php
--- FormatRulesTest.php 2021-12-08 11:01:42.000000000 +0900
+++ StrictRules/FormatRulesTest.php 2021-12-08 11:33:22.000000000 +0900
@@ -9,9 +9,10 @@
* the LICENSE file that was distributed with this source code.
*/
-namespace CodeIgniter\Validation;
+namespace CodeIgniter\Validation\StrictRules;
use CodeIgniter\Test\CIUnitTestCase;
+use CodeIgniter\Validation\Validation;
use Config\Services;
use Generator;
use Tests\Support\Validation\TestRules;
@@ -837,21 +838,19 @@
public function integerInvalidTypeDataProvider(): Generator
{
- // TypeError : CodeIgniter\Validation\FormatRules::integer(): Argument #1 ($str) must be of type ?string, array given
- // yield 'array with int' => [
- // [555],
- // false,
- // ];
-
- // TypeError : CodeIgniter\Validation\FormatRules::integer(): Argument #1 ($str) must be of type ?string, array given
- // yield 'empty array' => [
- // [],
- // false,
- // ];
+ yield 'array with int' => [
+ [555],
+ false, // true in v4.1.5 and earlier
+ ];
+
+ yield 'empty array' => [
+ [],
+ false, // true in v4.1.5 and earlier
+ ];
yield 'bool true' => [
true,
- true, // incorrect
+ false, // true in v4.1.5 and earlier
];
yield 'bool false' => [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am quite uncomfortable with the sheer amount of duplicated code. Too much of a maintenance burden updating both strict and non-strict versions.
Since the changes here are mostly adding declare(strict_types=1)
and adding some type safety checks, I suggest using composition. Something like this (for example, in Credit Card Rules):
<?php
namespace CodeIgniter\Validation\StrictRules;
use CodeIgniter\Validation\CreditCardRules as NonstrictCreditCardRules;
class CreditCardRules
{
private $nonStrictCreditCardRules;
public function __construct()
{
$this->nonstrictCreditCardRules = new NonstrictCreditCardRules();
}
public function valid_cc_number($ccNumber, string $type): bool
{
if (! is_string($ccNumber)) {
return false;
}
return $this->nonstrictCreditCardRules->valid_cc_number($ccNumber, $type);
}
}
If a non-strict version's method cannot be used entirely unmodified, then we proceed overriding it instead.
d69b629
to
44ce94b
Compare
@paulbalandan Refactored with composition and inheritance. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking much better! I left a few comments on the type hinting and docblocks.
I changed Why does The behavior changes of this PR: $ diff -u FormatRulesTest.php StrictRules/FormatRulesTest.php
--- FormatRulesTest.php 2022-01-07 09:35:57.000000000 +0900
+++ StrictRules/FormatRulesTest.php 2022-01-07 13:21:17.000000000 +0900
@@ -9,9 +9,10 @@
* the LICENSE file that was distributed with this source code.
*/
-namespace CodeIgniter\Validation;
+namespace CodeIgniter\Validation\StrictRules;
use CodeIgniter\Test\CIUnitTestCase;
+use CodeIgniter\Validation\Validation;
use Config\Services;
use Generator;
use Tests\Support\Validation\TestRules;
@@ -472,7 +473,7 @@
yield from [
[
null,
- true,
+ false, // true in Traditional Rule
],
[
self::ALPHABET,
@@ -837,21 +838,19 @@
public function integerInvalidTypeDataProvider(): Generator
{
- // TypeError : CodeIgniter\Validation\FormatRules::integer(): Argument #1 ($str) must be of type ?string, array given
- // yield 'array with int' => [
- // [555],
- // false,
- // ];
-
- // TypeError : CodeIgniter\Validation\FormatRules::integer(): Argument #1 ($str) must be of type ?string, array given
- // yield 'empty array' => [
- // [],
- // false,
- // ];
+ yield 'array with int' => [
+ [555],
+ false, // TypeError in Traditional Rule
+ ];
+
+ yield 'empty array' => [
+ [],
+ false, // TypeError in Traditional Rule
+ ];
yield 'bool true' => [
true,
- true, // incorrect
+ false, // true in Traditional Rule
];
yield 'bool false' => [ |
63b54b2
to
8a71b75
Compare
mixed includes null.
To return validation error (not TypeError exception), it must accept all type values. And called from a file without `strict_types`, implicit type conversion occurs.
Other alpha_* does not accept null.
Co-authored-by: John Paul E. Balandan, CPA <51850998+paulbalandan@users.noreply.github.com>
Co-authored-by: John Paul E. Balandan, CPA <51850998+paulbalandan@users.noreply.github.com>
Co-authored-by: John Paul E. Balandan, CPA <51850998+paulbalandan@users.noreply.github.com>
1034bab
to
b765800
Compare
Description
Supersedes #5402
Fixes #5374
Add the following classes:
CodeIgniter\Validation\StrictRules\CreditCardRules
CodeIgniter\Validation\StrictRules\FileRules
CodeIgniter\Validation\StrictRules\FormatRules
CodeIgniter\Validation\StrictRules\Rules
How to check the code:
Checklist: