From f426c2b6396767695f7a525ee4c490e9df4dc618 Mon Sep 17 00:00:00 2001 From: Jordan Hall Date: Thu, 10 May 2018 13:02:10 +0100 Subject: [PATCH] Allow customising offensive word blacklist and whitelist - closes #2 --- README.md | 17 +++++++++++++++++ composer.json | 2 +- tests/Unit/RuleTest.php | 24 ++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 092a245..19e3100 100644 --- a/README.md +++ b/README.md @@ -38,3 +38,20 @@ $request->validate([ 'username' => ['required', new Offensive], ]); ``` + +### Custom word lists + +If the defaults are too strict (or not strict enough), you can optionally specify a custom list +of offensive words and custom whitelist. Below is an example of using a custom blacklist and whitelist. + +```php +use DivineOmega\LaravelOffensiveValidationRule\Offensive; +use DivineOmega\IsOffensive\OffensiveChecker; + +$blacklist = ['moist', 'stinky', 'poo']; +$whitelist = ['poop']; + +$request->validate([ + 'username' => ['required', new Offensive(new OffensiveChecker($blacklist, $whitelist))], +]); +``` diff --git a/composer.json b/composer.json index 3788f22..72a70ac 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "type": "library", "require": { "illuminate/contracts": "^5.5", - "divineomega/is_offensive": "^1.3.2" + "divineomega/is_offensive": "^1.4.0" }, "require-dev": { "orchestra/testbench": "^3.5", diff --git a/tests/Unit/RuleTest.php b/tests/Unit/RuleTest.php index bcd2cef..f01b70e 100644 --- a/tests/Unit/RuleTest.php +++ b/tests/Unit/RuleTest.php @@ -2,6 +2,7 @@ namespace DivineOmega\LaravelOffensiveValidationRule\Tests\Unit; +use DivineOmega\IsOffensive\OffensiveChecker; use DivineOmega\LaravelOffensiveValidationRule\Offensive; use DivineOmega\LaravelOffensiveValidationRule\Tests\TestCase; use Validator; @@ -51,4 +52,27 @@ public function testNotOffensiveValues() $this->assertTrue($this->getValidator($value)->passes(), 'Failed asserting that \''.$value.'\' is not offensive.'); } } + + private function getCustomBlacklistAndWhitelistValidator($value) + { + $blacklist = ['moist', 'stinky', 'poo']; + $whitelist = ['poop']; + + return Validator::make(['value' => $value], ['value' => new Offensive(new OffensiveChecker($blacklist, $whitelist))]); + } + + public function testCustomBlacklistAndWhitelist() + { + $passingValues = ['cheese', 'poop', 'poops']; + $failingValues = ['moist', 'moistness', 'stinky', 'poo', 'poos']; + + foreach($passingValues as $value) { + $this->assertTrue($this->getCustomBlacklistAndWhitelistValidator($value)->passes()); + } + + foreach($failingValues as $value) { + $this->assertFalse($this->getCustomBlacklistAndWhitelistValidator($value)->passes()); + } + + } }