Skip to content

Commit

Permalink
Allow customising offensive word blacklist and whitelist - closes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
DivineOmega committed May 10, 2018
1 parent 8cfa32d commit f426c2b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))],
]);
```
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
24 changes: 24 additions & 0 deletions tests/Unit/RuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace DivineOmega\LaravelOffensiveValidationRule\Tests\Unit;

use DivineOmega\IsOffensive\OffensiveChecker;
use DivineOmega\LaravelOffensiveValidationRule\Offensive;
use DivineOmega\LaravelOffensiveValidationRule\Tests\TestCase;
use Validator;
Expand Down Expand Up @@ -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());
}

}
}

0 comments on commit f426c2b

Please sign in to comment.