Skip to content

Commit f426c2b

Browse files
committed
Allow customising offensive word blacklist and whitelist - closes #2
1 parent 8cfa32d commit f426c2b

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

README.md

+17
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,20 @@ $request->validate([
3838
'username' => ['required', new Offensive],
3939
]);
4040
```
41+
42+
### Custom word lists
43+
44+
If the defaults are too strict (or not strict enough), you can optionally specify a custom list
45+
of offensive words and custom whitelist. Below is an example of using a custom blacklist and whitelist.
46+
47+
```php
48+
use DivineOmega\LaravelOffensiveValidationRule\Offensive;
49+
use DivineOmega\IsOffensive\OffensiveChecker;
50+
51+
$blacklist = ['moist', 'stinky', 'poo'];
52+
$whitelist = ['poop'];
53+
54+
$request->validate([
55+
'username' => ['required', new Offensive(new OffensiveChecker($blacklist, $whitelist))],
56+
]);
57+
```

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"type": "library",
55
"require": {
66
"illuminate/contracts": "^5.5",
7-
"divineomega/is_offensive": "^1.3.2"
7+
"divineomega/is_offensive": "^1.4.0"
88
},
99
"require-dev": {
1010
"orchestra/testbench": "^3.5",

tests/Unit/RuleTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace DivineOmega\LaravelOffensiveValidationRule\Tests\Unit;
44

5+
use DivineOmega\IsOffensive\OffensiveChecker;
56
use DivineOmega\LaravelOffensiveValidationRule\Offensive;
67
use DivineOmega\LaravelOffensiveValidationRule\Tests\TestCase;
78
use Validator;
@@ -51,4 +52,27 @@ public function testNotOffensiveValues()
5152
$this->assertTrue($this->getValidator($value)->passes(), 'Failed asserting that \''.$value.'\' is not offensive.');
5253
}
5354
}
55+
56+
private function getCustomBlacklistAndWhitelistValidator($value)
57+
{
58+
$blacklist = ['moist', 'stinky', 'poo'];
59+
$whitelist = ['poop'];
60+
61+
return Validator::make(['value' => $value], ['value' => new Offensive(new OffensiveChecker($blacklist, $whitelist))]);
62+
}
63+
64+
public function testCustomBlacklistAndWhitelist()
65+
{
66+
$passingValues = ['cheese', 'poop', 'poops'];
67+
$failingValues = ['moist', 'moistness', 'stinky', 'poo', 'poos'];
68+
69+
foreach($passingValues as $value) {
70+
$this->assertTrue($this->getCustomBlacklistAndWhitelistValidator($value)->passes());
71+
}
72+
73+
foreach($failingValues as $value) {
74+
$this->assertFalse($this->getCustomBlacklistAndWhitelistValidator($value)->passes());
75+
}
76+
77+
}
5478
}

0 commit comments

Comments
 (0)