-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "contains_none" rule to cell validation (#88)
This update introduces a new rule named "contains_none" to the cell validation process. This rule takes an array of strings and validates that none of these strings appear in the cell value. Changes have been incorporated in the relevant schema example files and the associated PHP and Test files for the new validation rule have been created.
- Loading branch information
Showing
9 changed files
with
123 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,6 +75,9 @@ columns: | |
- a | ||
- b | ||
- c | ||
contains_none: | ||
- a | ||
- b | ||
starts_with: 'prefix ' | ||
ends_with: ' suffix' | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
/** | ||
* JBZoo Toolbox - Csv-Blueprint. | ||
* | ||
* This file is part of the JBZoo Toolbox project. | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @license MIT | ||
* @copyright Copyright (C) JBZoo.com, All rights reserved. | ||
* @see https://github.com/JBZoo/Csv-Blueprint | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JBZoo\CsvBlueprint\Rules\Cell; | ||
|
||
final class ContainsNone extends AbstractCellRule | ||
{ | ||
public function getHelpMeta(): array | ||
{ | ||
return [ | ||
[], | ||
[self::DEFAULT => ['[ a, b ]', 'All the strings must NOT be part of a CSV value.']], | ||
]; | ||
} | ||
|
||
public function validateRule(string $cellValue): ?string | ||
{ | ||
if ($cellValue === '') { | ||
return null; | ||
} | ||
|
||
$exclusions = $this->getOptionAsArray(); | ||
if (\count($exclusions) === 0) { | ||
return 'Rule must contain at least one exclusion value in schema file.'; | ||
} | ||
|
||
foreach ($exclusions as $exclusion) { | ||
if (\strpos($cellValue, $exclusion) !== false) { | ||
return "Value \"<c>{$cellValue}</c>\" must not contain any of the following:" . | ||
' "<green>["' . \implode('", "', $exclusions) . '"]</green>"'; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
/** | ||
* JBZoo Toolbox - Csv-Blueprint. | ||
* | ||
* This file is part of the JBZoo Toolbox project. | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @license MIT | ||
* @copyright Copyright (C) JBZoo.com, All rights reserved. | ||
* @see https://github.com/JBZoo/Csv-Blueprint | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JBZoo\PHPUnit\Rules\Cell; | ||
|
||
use JBZoo\CsvBlueprint\Rules\Cell\ContainsNone; | ||
use JBZoo\PHPUnit\Rules\TestAbstractCellRule; | ||
|
||
use function JBZoo\PHPUnit\isSame; | ||
|
||
final class ContainsNoneTest extends TestAbstractCellRule | ||
{ | ||
protected string $ruleClass = ContainsNone::class; | ||
|
||
public function testPositive(): void | ||
{ | ||
$rule = $this->create([]); | ||
isSame('', $rule->test('')); | ||
|
||
$rule = $this->create(['a', 'b', 'c']); | ||
isSame('', $rule->test('')); | ||
isSame('', $rule->test('q')); | ||
} | ||
|
||
public function testNegative(): void | ||
{ | ||
$rule = $this->create([]); | ||
isSame( | ||
'Rule must contain at least one exclusion value in schema file.', | ||
$rule->test('ac'), | ||
); | ||
|
||
$rule = $this->create(['a', 'b', 'c']); | ||
isSame( | ||
'Value "a" must not contain any of the following: "["a", "b", "c"]"', | ||
$rule->test('a'), | ||
); | ||
|
||
$rule = $this->create(['a', 'b', 'c']); | ||
isSame( | ||
'Value "ddddb" must not contain any of the following: "["a", "b", "c"]"', | ||
$rule->test('ddddb'), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters