Configurable Laravel validation rule for Tiptap editor content.
$rules = [
'tiptap_content' => [
'required',
TiptapValidation::content()
->whitelist()
->nodes('text', 'paragraph')
->marks('bold', 'italic', 'link'),
TiptapValidation::containsText()
->between(18, 256),
],
];
Validate Tiptap content in your back-end to prevent unwanted elements and styling from being used.
This package only works with JSON output, not the raw HTML. You can read more about outputting Tiptap JSON content here.
You can install the package via composer:
composer require jacobfitzp/laravel-tiptap-validation
And then add the service provider to config/app.php
JacobFitzp\LaravelTiptapValidation\TiptapValidationServiceProvider::class,
The TiptapContent
rule is used to validate the basic structure and format, as well as limit what nodes and marks are allowed.
Simply call TiptapValidation::content()
within your rules.
TiptapValidation::content()
Only nodes and marks which are not specified in the blacklist will be allowed, anything else will fail validation.
TiptapValidation::content()
->blacklist()
->nodes('orderedList', 'listItem')
->marks('italic', 'link')
Only specified nodes and marks are allowed, anything not in the whitelist will fail validation.
TiptapValidation::content()
->whitelist()
->nodes('text', 'paragraph')
->marks('bold')
Instead of having to configure the rule each time, you could simply create an extension that has your default preferences set.
class MyCustomTiptapValidationRule extends TiptapContent
{
protected TiptapValidationRuleMode $mode = TiptapValidationRuleMode::WHITELIST;
protected array $nodes = ['text', 'paragraph', 'table'];
protected array $marks = ['italic', 'link'];
}
This can then be used without the need for further configuration:
MyCustomTiptapValidationRule::make(),
The TiptapContainsText
rule is used for verifying that the content contains text, and meets an optional character count requirements.
TiptapValidation::containsText()
->minimum(12) // Minimum character requirement
->maximum(156) // Maximum character requirement
->between(12, 156) // Minimum and maximum character requirement
First publish the translation files:
php artisan vendor:publish --provider="JacobFitzp\LaravelTiptapValidation\TiptapValidationServiceProvider" --tag="tiptap-validation-translations"
And then you can configure the error messages in lang/vendor/tiptap-validation/messages.php
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.