diff --git a/CHANGELOG.md b/CHANGELOG.md index 852adf0..6b58fb5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ Changelog All notable changes to this project will be documented in this file, in reverse chronological order by release. -## V1.1.2 - TBD +## V1.1.2 - 2019-02-28 ### Added @@ -23,7 +23,7 @@ All notable changes to this project will be documented in this file, in reverse ### Fixed -- Nothing. +- [#12](https://github.com/elie29/validator/issues/12) StringCleanerRule should not clean string on error. ## V1.1.1 - 2019-02-28 diff --git a/src/Rule/StringCleanerRule.php b/src/Rule/StringCleanerRule.php index 4598971..589aac5 100644 --- a/src/Rule/StringCleanerRule.php +++ b/src/Rule/StringCleanerRule.php @@ -9,7 +9,7 @@ /** * This class verifies that a value is a valid string. * It calls Text::removeInvisibleChars in order to clean the string - * after validation. + * after validate returns VALID. */ class StringCleanerRule extends StringRule { @@ -19,7 +19,7 @@ public function getValue(): string $value = parent::getValue(); // better in case called before validate - if (is_string($value)) { + if (! $this->error && is_string($value)) { $value = Text::removeInvisibleChars($value); } diff --git a/tests/Rule/StringCleanerRuleTest.php b/tests/Rule/StringCleanerRuleTest.php index 5fe3235..70f67f9 100644 --- a/tests/Rule/StringCleanerRuleTest.php +++ b/tests/Rule/StringCleanerRuleTest.php @@ -25,10 +25,14 @@ public function testValidate($value, $params, $expectedValue, $expectedError): v public function getStringValueProvider(): \Generator { - yield 'Given value is cleaned by trim' => [ + yield 'Given value is cleaned by trim and would be valid' => [ "\x00", [], '', '' ]; + yield 'Given value is not cleaned by trim and would be valid even when cleaned is empty' => [ + "\x00", [StringCleanerRule::TRIM => false, StringCleanerRule::REQUIRED => true], '', '' + ]; + yield 'Given value should be cleaned' => [ "f\x00f", [StringCleanerRule::REQUIRED => true], 'ff', '' ]; @@ -36,5 +40,10 @@ public function getStringValueProvider(): \Generator yield 'Given value between 4 and 8 characters' => [ '%7FPeter ', [StringCleanerRule::MIN => 4, StringCleanerRule::MAX => 8], 'Peter', '' ]; + + yield 'Given value should not be cleaned before validation' => [ + "f\x00f", [StringCleanerRule::REQUIRED => true, StringCleanerRule::MAX => 2], "f\x00f", + "name: The length of f\x00f is not between 0 and 2" + ]; } }