Skip to content

Commit

Permalink
Merge pull request #32 from SandroMiguel/fix/min-length-validation
Browse files Browse the repository at this point in the history
fix: fix min_length validation to allow empty fields
  • Loading branch information
SandroMiguel authored Feb 22, 2024
2 parents a62dac9 + 310e66c commit b215d37
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 24 deletions.
3 changes: 2 additions & 1 deletion src/Rules/MinLength.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
9 changes: 9 additions & 0 deletions tests/MaxLengthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
10 changes: 10 additions & 0 deletions tests/MinLengthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
45 changes: 22 additions & 23 deletions tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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();
}

Expand All @@ -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'
);
Expand All @@ -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}.'
Expand All @@ -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);
}
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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();
Expand All @@ -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}.',
Expand Down Expand Up @@ -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}.',
Expand Down Expand Up @@ -439,7 +439,7 @@ public function testGetErrorsCustomMessageWithMultipleLabels(): void
],
];
$validator = new Validator(
$this->oneFieldValueNok,
$this->oneFieldValueEmptyString,
$this->nameFieldMultipleLabelsRequiredRule,
LangEnum::PT_PT
);
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -528,7 +527,7 @@ public function testGetErrorsCustomMessagesWithLabel(): void
],
];
$validator = new Validator(
$this->oneFieldValueOk,
$this->oneFieldValue,
$this->nameFieldMultipleLabelsMinLengthRule,
LangEnum::PT_PT
);
Expand All @@ -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(
Expand All @@ -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();
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit b215d37

Please sign in to comment.