jacobfitzp/laravel-tiptap-validation

Laravel validation rules for the Tiptap WYSIWYG editor.

1.1.3 2023-08-29 17:49 UTC

README

68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f4c61726176656c25323054697074617025323076616c69646174696f6e2e706e673f7468656d653d6c69676874267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d6a61636f626669747a702532466c61726176656c2d7469707461702d76616c69646174696f6e267061747465726e3d617263686974656374267374796c653d7374796c655f31266465736372697074696f6e3d4261636b2d656e642b5469707461702b656469746f722b76616c69646174696f6e2b72756c6573266d643d312673686f7757617465726d61726b3d3026666f6e7453697a653d3735707826696d616765733d68747470732533412532462532466c61726176656c2e636f6d253246696d672532466c6f676f6d61726b2e6d696e2e737667

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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.

Please note

This package only works with JSON output, not the raw HTML. You can read more about outputting Tiptap JSON content here.

Installation

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,

Usage

Content

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()

Blacklisting

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')

Whitelisting

Only specified nodes and marks are allowed, anything not in the whitelist will fail validation.

TiptapValidation::content()
    ->whitelist()
    ->nodes('text', 'paragraph')
    ->marks('bold')

Extension

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(),

Contains Text

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

Configuration

Error messages

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

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.