From 2936b98dd4096bb46c6c57bd3394e6a09f4d0c59 Mon Sep 17 00:00:00 2001 From: Ashley Hood Date: Thu, 10 Dec 2020 12:44:26 +0000 Subject: [PATCH 1/4] feat: add string rule to the core rules --- README.md | 6 ++++++ src/Rules/String.php | 23 +++++++++++++++++++++++ src/Validator.php | 1 + 3 files changed, 30 insertions(+) create mode 100644 src/Rules/String.php diff --git a/README.md b/README.md index ad4c3fb..27af97a 100644 --- a/README.md +++ b/README.md @@ -948,6 +948,12 @@ Field under this rule may be empty. +
string + +Field under this rule must be a string. + +
+ ## Register/Override Rule Another way to use custom validation rule is to create a class extending `Rakit\Validation\Rule`. diff --git a/src/Rules/String.php b/src/Rules/String.php new file mode 100644 index 0000000..ecb9ecf --- /dev/null +++ b/src/Rules/String.php @@ -0,0 +1,23 @@ + new Rules\Defaults, 'default' => new Rules\Defaults, // alias of defaults 'nullable' => new Rules\Nullable, + 'string' => new Rule\String, ]; foreach ($baseValidator as $key => $validator) { From 591070161aaa5ed4316939e0fa366b7d21bd4736 Mon Sep 17 00:00:00 2001 From: Ashley Hood Date: Mon, 11 Jan 2021 13:36:15 +0000 Subject: [PATCH 2/4] Create test for String rule --- tests/Rules/StringTest.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 tests/Rules/StringTest.php diff --git a/tests/Rules/StringTest.php b/tests/Rules/StringTest.php new file mode 100644 index 0000000..0104b9a --- /dev/null +++ b/tests/Rules/StringTest.php @@ -0,0 +1,33 @@ +rule = new String; + } + + public function testValids() + { + $this->assertTrue($this->rule->check('foo')); + $this->assertTrue($this->rule->check('123asd')); + $this->assertTrue($this->rule->check('asd123')); + $this->assertTrue($this->rule->check('foo123bar')); + $this->assertTrue($this->rule->check('foo bar')); + $this->assertTrue($this->rule->check('

Lorem ipsum dolor sit amet cum omnis voluptatum!

')); + } + + public function testInvalids() + { + $this->assertFalse($this->rule->check(2)); + $this->assertFalse($this->rule->check([])); + $this->assertFalse($this->rule->check(new stdClass)); + } +} From 67e63a2bbc9ac5ec77bb313918f13daa7e174f62 Mon Sep 17 00:00:00 2001 From: Ashley Hood Date: Wed, 17 Feb 2021 20:27:52 +0000 Subject: [PATCH 3/4] fixed incorrect class name --- src/Rules/String.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Rules/String.php b/src/Rules/String.php index ecb9ecf..6fad15a 100644 --- a/src/Rules/String.php +++ b/src/Rules/String.php @@ -4,7 +4,7 @@ use Rakit\Validation\Rule; -class Alpha extends Rule +class String extends Rule { /** @var string */ From ea004be8da8c1e1c56876b2bba3cffff8b6e8138 Mon Sep 17 00:00:00 2001 From: Ashley Hood Date: Wed, 17 Feb 2021 21:10:45 +0000 Subject: [PATCH 4/4] fixed class name clash --- src/Rules/{String.php => Stringy.php} | 2 +- src/Validator.php | 2 +- tests/Rules/{StringTest.php => StringyTest.php} | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) rename src/Rules/{String.php => Stringy.php} (92%) rename tests/Rules/{StringTest.php => StringyTest.php} (88%) diff --git a/src/Rules/String.php b/src/Rules/Stringy.php similarity index 92% rename from src/Rules/String.php rename to src/Rules/Stringy.php index 6fad15a..0e9449d 100644 --- a/src/Rules/String.php +++ b/src/Rules/Stringy.php @@ -4,7 +4,7 @@ use Rakit\Validation\Rule; -class String extends Rule +class Stringy extends Rule { /** @var string */ diff --git a/src/Validator.php b/src/Validator.php index ae81cbb..523d5bf 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -162,7 +162,7 @@ protected function registerBaseValidators() 'defaults' => new Rules\Defaults, 'default' => new Rules\Defaults, // alias of defaults 'nullable' => new Rules\Nullable, - 'string' => new Rule\String, + 'string' => new Rules\Stringy, ]; foreach ($baseValidator as $key => $validator) { diff --git a/tests/Rules/StringTest.php b/tests/Rules/StringyTest.php similarity index 88% rename from tests/Rules/StringTest.php rename to tests/Rules/StringyTest.php index 0104b9a..e70181b 100644 --- a/tests/Rules/StringTest.php +++ b/tests/Rules/StringyTest.php @@ -2,16 +2,16 @@ namespace Rakit\Validation\Tests; -use Rakit\Validation\Rules\String; +use Rakit\Validation\Rules\Stringy; use PHPUnit\Framework\TestCase; use stdClass; -class StringTest extends TestCase +class StringyTest extends TestCase { public function setUp() { - $this->rule = new String; + $this->rule = new Stringy; } public function testValids()