From 310e66c02af3255e209cbef02b9aa172ca76a42c Mon Sep 17 00:00:00 2001 From: SandroMiguel Date: Thu, 22 Feb 2024 22:27:24 +0000 Subject: [PATCH] fix: fix min_length validation to allow empty fields Update the min_length validation rule to allow empty fields ('') to pass validation, ensuring consistency with other validation rules and providing more flexibility to users. --- src/Rules/MinLength.php | 3 ++- tests/MaxLengthTest.php | 9 +++++++++ tests/MinLengthTest.php | 10 +++++++++ tests/ValidatorTest.php | 45 ++++++++++++++++++++--------------------- 4 files changed, 43 insertions(+), 24 deletions(-) diff --git a/src/Rules/MinLength.php b/src/Rules/MinLength.php index fea4137..3ed0b40 100755 --- a/src/Rules/MinLength.php +++ b/src/Rules/MinLength.php @@ -69,7 +69,8 @@ public function validate(): bool $this->minLength = $this->ruleValues[0]; - if ($this->fieldValue === null) { + // if ($this->fieldValue === null) { + if ($this->fieldValue === null || $this->fieldValue === '') { return true; } diff --git a/tests/MaxLengthTest.php b/tests/MaxLengthTest.php index 3e175c6..f5dc488 100755 --- a/tests/MaxLengthTest.php +++ b/tests/MaxLengthTest.php @@ -81,6 +81,15 @@ public function testValidateNull(): void $this->assertTrue($this->validate(null, [5])); } + + /** + * An Empty String ('') value should pass validation (ignored field). + */ + public function testValidateEmptyString(): void + { + $this->assertTrue($this->validate('', [5])); + } + /** * The String ('text with 23 characters') value should violate the rule with max length of 20 characters. * diff --git a/tests/MinLengthTest.php b/tests/MinLengthTest.php index c4de156..db3811f 100755 --- a/tests/MinLengthTest.php +++ b/tests/MinLengthTest.php @@ -81,6 +81,16 @@ public function testValidateNull(): void $this->assertTrue($this->validate(null, [5])); } + /** + * An Empty String ('') value should pass validation (ignored field). + * + * @return void + */ + // public function testValidateEmptyString(): void + // { + // $this->assertTrue($this->validate('', [5])); + // } + /** * The String ('text with 23 characters') value should violate the rule with min length of 30 characters. * diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php index 6111c75..de4f4af 100755 --- a/tests/ValidatorTest.php +++ b/tests/ValidatorTest.php @@ -40,10 +40,10 @@ class ValidatorTest extends TestCase ]; /** @var array One field value OK */ - private $oneFieldValueOk = ['name' => 'John']; + private $oneFieldValue = ['name' => 'John']; - /** @var array One field value NOK */ - private $oneFieldValueNok = ['name' => '']; + /** @var array One field value with empty string */ + private $oneFieldValueEmptyString = ['name' => '']; /** @var array Field without label */ private $nameFieldWithoutLabelRequiredRule = [ @@ -176,7 +176,7 @@ public function testValidateNonExistentRule(): void { $this->expectException(ValidatorException::class); $this->expectExceptionMessage('The "non_existent" rule was not found.'); - $validator = new Validator($this->oneFieldValueNok, $this->nameFieldNonExistentRule); + $validator = new Validator($this->oneFieldValueEmptyString, $this->nameFieldNonExistentRule); $validator->validate(); } @@ -192,7 +192,7 @@ public function testGetErrorsNonExistentLanguage(): void 'Invalid argument; Argument name: $lang; Argument value: zz-ZZ; Language not available' ); $validator = new Validator( - $this->oneFieldValueNok, + $this->oneFieldValueEmptyString, $this->nameFieldRequiredRule, 'zz-ZZ' ); @@ -210,7 +210,7 @@ public function testGetErrorsMessageWrongPlaceholders(): void $this->expectExceptionMessage( 'The arguments array must contain 3 items, 2 given; Message: Message "min_length" rule. val1 = {param:1}, val2 = {param:2}, val3 = {param:3}.; Arguments: 5,Name' ); - $validator = new Validator($this->oneFieldValueNok, $this->nameFieldMinLengthRule); + $validator = new Validator($this->oneFieldValue, $this->nameFieldMinLengthRule); $validator->addSimpleCustomMessage( 'min_length', 'Message "min_length" rule. val1 = {param:1}, val2 = {param:2}, val3 = {param:3}.' @@ -225,7 +225,7 @@ public function testGetErrorsMessageWrongPlaceholders(): void */ public function testValidateRuleViolation(): void { - $validator = new Validator($this->oneFieldValueNok, $this->nameFieldRequiredRule); + $validator = new Validator($this->oneFieldValueEmptyString, $this->nameFieldRequiredRule); $isValid = $validator->validate(); $this->assertFalse($isValid); } @@ -245,7 +245,7 @@ public function testGetErrorsDefaultLanguage(): void ], ], ]; - $validator = new Validator($this->oneFieldValueNok, $this->nameFieldRequiredRule); + $validator = new Validator($this->oneFieldValueEmptyString, $this->nameFieldRequiredRule); $validator->validate(); $errors = $validator->getErrors(); $this->assertTrue( @@ -267,7 +267,7 @@ public function testGetErrorsWithoutLabel(): void ], ], ]; - $validator = new Validator($this->oneFieldValueNok, $this->nameFieldWithoutLabelRequiredRule); + $validator = new Validator($this->oneFieldValueEmptyString, $this->nameFieldWithoutLabelRequiredRule); $validator->validate(); $errors = $validator->getErrors(); $this->assertTrue( @@ -289,7 +289,7 @@ public function testGetErrorsWithLabel(): void ], ], ]; - $validator = new Validator($this->oneFieldValueNok, $this->nameFieldRequiredRule); + $validator = new Validator($this->oneFieldValueEmptyString, $this->nameFieldRequiredRule); $validator->validate(); $errors = $validator->getErrors(); $this->assertTrue( @@ -312,7 +312,7 @@ public function testGetErrorsWithMultipleLabels(): void ], ], ]; - $validator = new Validator($this->oneFieldValueNok, $this->nameFieldMultipleLabelsRequiredRule); + $validator = new Validator($this->oneFieldValueEmptyString, $this->nameFieldMultipleLabelsRequiredRule); $validator->validate(); $errors = $validator->getErrors(); $this->assertTrue( @@ -335,7 +335,7 @@ public function testGetErrorsNonDefaultLanguage(): void ], ], ]; - $validator = new Validator($this->oneFieldValueNok, $this->nameFieldRequiredRule, LangEnum::PT_PT); + $validator = new Validator($this->oneFieldValueEmptyString, $this->nameFieldRequiredRule, LangEnum::PT_PT); $validator->validate(); $errors = $validator->getErrors(); $this->assertTrue( @@ -358,7 +358,7 @@ public function testGetErrorsCustomMessage(): void ], ], ]; - $validator = new Validator($this->oneFieldValueNok, $this->nameFieldRequiredRule, LangEnum::PT_PT); + $validator = new Validator($this->oneFieldValueEmptyString, $this->nameFieldRequiredRule, LangEnum::PT_PT); $validator->addSimpleCustomMessage('required', 'Custom message for the "required" rule.'); $validator->validate(); $errors = $validator->getErrors(); @@ -382,7 +382,7 @@ public function testGetErrorsCustomMessageWithoutLabel(): void ], ], ]; - $validator = new Validator($this->oneFieldValueNok, $this->nameFieldWithoutLabelRequiredRule, LangEnum::PT_PT); + $validator = new Validator($this->oneFieldValueEmptyString, $this->nameFieldWithoutLabelRequiredRule, LangEnum::PT_PT); $validator->addCustomMessage( 'required', 'Custom message with label for required rule. Label: {param:1}.', @@ -410,7 +410,7 @@ public function testGetErrorsCustomMessageWithLabel(): void ], ], ]; - $validator = new Validator($this->oneFieldValueNok, $this->nameFieldRequiredRule); + $validator = new Validator($this->oneFieldValueEmptyString, $this->nameFieldRequiredRule); $validator->addCustomMessage( 'required', 'Custom message with label for required rule. Label: {param:1}.', @@ -439,7 +439,7 @@ public function testGetErrorsCustomMessageWithMultipleLabels(): void ], ]; $validator = new Validator( - $this->oneFieldValueNok, + $this->oneFieldValueEmptyString, $this->nameFieldMultipleLabelsRequiredRule, LangEnum::PT_PT ); @@ -470,12 +470,11 @@ public function testGetErrorsCustomMessages(): void 'name' => [ 'label' => 'Name', 'rules' => [ - 'min_length' => 'Custom message for the "min_length" rule.', 'required' => 'Custom message for the "required" rule.', ], ], ]; - $validator = new Validator($this->oneFieldValueNok, $this->nameFieldRequiredAndMinLengthRules, LangEnum::PT_PT); + $validator = new Validator($this->oneFieldValueEmptyString, $this->nameFieldRequiredAndMinLengthRules, LangEnum::PT_PT); $validator->addCustomMessages($customErrorMessages); $validator->validate(); $errors = $validator->getErrors(); @@ -500,7 +499,7 @@ public function testGetErrorsCustomMessagesWithoutLabel(): void ], ], ]; - $validator = new Validator($this->oneFieldValueOk, $this->nameFieldWithoutLabelNumericAndMinLengthRules); + $validator = new Validator($this->oneFieldValue, $this->nameFieldWithoutLabelNumericAndMinLengthRules); $validator->addCustomMessages($this->customErrorMessages); $validator->validate(); $errors = $validator->getErrors(); @@ -528,7 +527,7 @@ public function testGetErrorsCustomMessagesWithLabel(): void ], ]; $validator = new Validator( - $this->oneFieldValueOk, + $this->oneFieldValue, $this->nameFieldMultipleLabelsMinLengthRule, LangEnum::PT_PT ); @@ -555,7 +554,7 @@ public function testGetErrorsMessagePlaceholders(): void ], ], ]; - $validator = new Validator($this->oneFieldValueNok, $this->nameFieldMinLengthRule); + $validator = new Validator($this->oneFieldValue, $this->nameFieldMinLengthRule); $validator->validate(); $errors = $validator->getErrors(); $this->assertTrue( @@ -579,7 +578,7 @@ public function testGetErrorsMessageIgnoresPlaceholders(): void ], ], ]; - $validator = new Validator($this->oneFieldValueNok, $this->nameFieldMinLengthRule); + $validator = new Validator($this->oneFieldValue, $this->nameFieldMinLengthRule); $validator->addSimpleCustomMessage('min_length', 'Custom message for the "min_length" rule.'); $validator->validate(); $errors = $validator->getErrors(); @@ -622,7 +621,7 @@ public function testValidateTwoFields(): void */ public function testValidateNoRuleViolation(): void { - $validator = new Validator($this->oneFieldValueOk, $this->nameFieldRequiredRule); + $validator = new Validator($this->oneFieldValue, $this->nameFieldRequiredRule); $isValid = $validator->validate(); $this->assertTrue($isValid); }