Introduction
When you accept user input in your web applications, you may want to validate it to ensure it doesn't contain profanity. I've needed to implement this functionality in several past projects, and I'm sure many of you have, too.
One way to filter profanity in your Laravel apps is to use Squeaky, a new package by Jon Purvis.
Squeaky is a Laravel package containing a validation rule which ensures values don't contain profanity.
I've written about Jon's other PHP packages before, and I'm always impressed with the quality of his work. So I thought I'd write a quick article about Squeaky since it's a package I think many of you will find useful.
Articles about Jon's other PHP packages:
- Profanify: Prevent Profanity in Your PHP Codebase
- Lawman: Pest Architecture Testing for Saloon API Integrations
Squeaky piggybacks off Jon's other PHP package, Profanify, which is a Pest plugin for preventing profanity in your PHP codebase. For example, it checks you haven't used any profanity in your class names, method names, or comments. So you might want to check that out if you're interested in this topic.
How to Filter Profanity in Laravel
Let's look at how to use Squeaky to filter profanity in Laravel.
Installation
To get started with using Squeaky, you'll first need to install it. You can do this using Composer by running the following command in your project's root:
1composer require jonpurvis/squeaky
It should now be installed and ready to use in your Laravel application.
Using the Rule
To use the rule, you can create a new instance of it and pass it to your validator.
For example, let's imagine we have a blogging platform, and we allow users to leave comments on blog posts. We'll want to ensure that the comments don't contain any profanity.
So, we'll assume we have a form request class which handles the validation of the comment. The form request may look something like this:
app/Http/Requests/StoreCommentRequest.php
1declare(strict_types=1);23namespace App\Http\Requests;45use JonPurvis\Squeaky\Rules\Clean;6use Illuminate\Foundation\Http\FormRequest;78final class StoreCommentRequest extends FormRequest9{10 // ...1112 public function rules(): array13 {14 return [15 'comment' => ['required', 'string', new Clean()],16 ];17 }18}
As we can see in the code example, we've created an instance of the JonPurvis\Squeaky\Rules\Clean
rule and added it as a validation rule for the comment
field. This will ensure that the comment doesn't contain any profanity.
If no profanity is found, the validation will pass. If profanity is found, the validation will fail with a message like this:
1The comment field is not clean
Specifying the Locales
By default, Squeaky will use the locale of your Laravel application. In a lot of cases, this will be sufficient.
But there may be times when you want to specify the locales you want to check against. For example, if your application supports multiple languages, you might want to check for profanity in each of those languages.
At the time of writing this article, Squeaky supports the following languages:
- English
- Italian
- Arabic
- Portuguese
- Dutch
To specify the locales to validate against, you can pass them as an array to the JonPurvis\Squeaky\Rules\Clean
rule:
app/Http/Requests/StoreCommentRequest.php
1declare(strict_types=1);23namespace App\Http\Requests;45use JonPurvis\Squeaky\Enums\Locale;6use JonPurvis\Squeaky\Rules\Clean;7use Illuminate\Foundation\Http\FormRequest;89final class StoreCommentRequest extends FormRequest10{11 // ...1213 public function rules(): array14 {15 return [16 'comment' => ['required', 'string', new Clean([Locale::English])],17 ];18 }19}
In the example above, we're only checking for profanity in the English language.
Alternatively, you can pass multiple locales if you want to check against more than one language:
app/Http/Requests/StoreCommentRequest.php
1declare(strict_types=1);23namespace App\Http\Requests;45use JonPurvis\Squeaky\Enums\Locale;6use JonPurvis\Squeaky\Rules\Clean;7use Illuminate\Foundation\Http\FormRequest;89final class StoreCommentRequest extends FormRequest10{11 // ...1213 public function rules(): array14 {15 return [16 'comment' => [17 'required',18 'string',19 new Clean([Locale::English, Locale::Italian]),20 ],21 ];22 }23}
In this example, we're checking for profanity in both English and Italian. If profanity is found in either language, the validation will fail.
Use Squeaky as Part of a Wider Strategy
Squeaky is a fantastic package, and I'll be using it in my projects moving forward.
But it's important to remember that detecting profanity is a complex topic. It's not always as simple as filtering out a list of words. People can be cunning and creative with their language and find ways to bypass filters. I'm sure you've seen some creative ways people bypass filters on games and social media platforms.
So, although packages like Squeaky can be incredibly useful and catch a lot of profanity, it should be used as part of a wider strategy to moderate user input. For example, you might want to consider manual moderation or provide a way for users to report inappropriate content. But this is all dependent on the context of your application.
If you have any ideas or strategies to make Squeaky's filtering more robust and powerful, I'm sure Jon would love to hear them. So make sure to check out the package on GitHub: https://github.com/JonPurvis/squeaky.
Conclusion
Hopefully, this article has given you a quick introduction to Squeaky. If you're interested in filtering profanity in your Laravel applications, it's definitely worth checking out.
If you enjoyed reading this post, I'd love to hear about it. Likewise, if you have any feedback to improve the future ones, I'd also love to hear that too.
You might also be interested in checking out my 220+ page ebook "Battle Ready Laravel" which covers similar topics in more depth.
Or, you might want to check out my other 440+ page ebook "Consuming APIs in Laravel" which teaches you how to use Laravel to consume APIs from other services.
If you're interested in getting updated each time I publish a new post, feel free to sign up for my newsletter below.
Keep on building awesome stuff! ๐