jord-jd / laravel-offensive-validation-rule
Laravel validation rule that checks if a string is offensive.
Package info
github.com/Jord-JD/laravel-offensive-validation-rule
pkg:composer/jord-jd/laravel-offensive-validation-rule
Fund package maintenance!
Requires
- php: >=7.1
- illuminate/contracts: ^5.5 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0 || ^12.0 || ^13.0
- jord-jd/is_offensive: ^5.0
Requires (Dev)
- phpunit/phpunit: ^7.5 || ^8.5 || ^9.6 || ^10.5 || ^11.5 || ^12.5
Replaces
This package is auto-updated.
Last update: 2026-07-18 06:00:20 UTC
README
This package provides a Laravel validation rule that checks if a string is offensive. It can be useful to check user supplied data that may be publicly displayed, such as usernames or comments.
Installation
To install, just run the following Composer command.
composer require jord-jd/laravel-offensive-validation-rule
The package supports Illuminate/Laravel 5.5 through 13 and PHP 7.1 through current PHP 8.x releases.
Usage
The following code snippet shows an example of how to use the offensive validation rule.
use JordJD\LaravelOffensiveValidationRule\Offensive; $request->validate([ 'username' => ['required', new Offensive], ]);
This rule only decides whether string content is offensive. Non-string values
pass so Laravel rules such as string, array, or integer can report the
appropriate type error. Objects with __toString() are checked as strings.
Custom validation message
Pass an optional message as the second constructor argument. Laravel replaces
:attribute as usual.
$request->validate([ 'username' => [ 'required', 'string', new Offensive(null, 'Please choose a different :attribute.'), ], ]);
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.
use JordJD\LaravelOffensiveValidationRule\Offensive; use JordJD\IsOffensive\OffensiveChecker; $blacklist = ['moist', 'stinky', 'poo']; $whitelist = ['poop']; $rule = new Offensive( new OffensiveChecker($blacklist, $whitelist), 'Please choose a different :attribute.' ); $request->validate(['username' => ['required', 'string', $rule]]);
