Skip to content

Commit

Permalink
StringCleanerRule should not clean string on error #12
Browse files Browse the repository at this point in the history
  • Loading branch information
elie29 committed Feb 28, 2019
1 parent 94bc9e7 commit e19bc9f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Expand Up @@ -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

Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/Rule/StringCleanerRule.php
Expand Up @@ -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
{
Expand All @@ -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);
}

Expand Down
11 changes: 10 additions & 1 deletion tests/Rule/StringCleanerRuleTest.php
Expand Up @@ -25,16 +25,25 @@ 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', ''
];

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"
];
}
}

0 comments on commit e19bc9f

Please sign in to comment.