Skip to content

Commit a85c848

Browse files
committed
Use laravel helpers
1 parent 32bc605 commit a85c848

File tree

6 files changed

+45
-52
lines changed

6 files changed

+45
-52
lines changed

README.md

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -35,90 +35,80 @@ You can install the package via composer:
3535
composer require jacobfitzp/laravel-tiptap-validation
3636
```
3737

38-
And then add the below service provider to `config/app.php`
38+
And then add the service provider to `config/app.php`
3939
```php
40-
JacobFitzp\LaravelTiptapValidation\TiptapValidationServiceProvider::class
40+
JacobFitzp\LaravelTiptapValidation\TiptapValidationServiceProvider::class,
4141
```
4242

4343
## Usage
4444

45-
### TiptapContent
45+
### Content
4646

4747
The `TiptapContent` rule is used to validate the basic structure and format, as well as limit what nodes and marks are allowed.
4848

4949
Simply call `TiptapValidation::content()` within your rules.
5050

5151
```php
52-
Validator::make($data, [
53-
'content' => TiptapValidation::content(),
54-
])
52+
TiptapValidation::content()
5553
```
5654

57-
You can also specify the nodes and marks you want to whitelist or blacklist:
55+
<details>
56+
<summary>Blacklisting</summary>
5857

59-
#### Blacklist nodes / marks
58+
Only nodes and marks which are not specified in the blacklist will be allowed, anything else will fail validation.
6059

6160
```php
62-
Validator::make($data, [
63-
'content' => TiptapValidation::content()
64-
->blacklist()
65-
// The node types you want to blacklist
66-
->nodes([ ... ])
67-
// The mark types you want to blacklist
68-
->marks([ ... ]),
69-
])
61+
TiptapValidation::content()
62+
->blacklist()
63+
->nodes('orderedList', 'listItem')
64+
->marks('italic', 'link')
7065
```
66+
</details>
7167

72-
#### Whitelist nodes / marks
68+
69+
<details>
70+
<summary>Whitelisting</summary>
71+
72+
Only specified nodes and marks are allowed, anything not in the whitelist will fail validation.
7373

7474
```php
75-
Validator::make($data, [
76-
'content' => TiptapValidation::content()
77-
->whitelist()
78-
// The node types you want to whitelist
79-
->nodes([ ... ])
80-
// The mark types you want to whitelist
81-
->marks([ ... ]),
82-
])
75+
TiptapValidation::content()
76+
->whitelist()
77+
->nodes('text', 'paragraph')
78+
->marks('bold')
8379
```
80+
</details>
8481

85-
#### Extension
82+
<details>
83+
<summary>Extension</summary>
8684

8785
Instead of having to configure the rule each time, you can create an extension that has your default preferences set.
8886

8987
```php
9088
class MyCustomTiptapValidationRule extends TiptapContent
9189
{
9290
protected TiptapValidationRuleMode $mode = TiptapValidationRuleMode::WHITELIST;
93-
9491
protected array $nodes = ['text', 'paragraph', 'table'];
95-
9692
protected array $marks = ['italic', 'link'];
9793
}
9894
```
9995

10096
This can then be used without the need for further configuration:
10197

10298
```php
103-
Validator::make($data, [
104-
'content' => MyCustomTiptapValidationRule::create(),
105-
])
99+
MyCustomTiptapValidationRule::create(),
106100
```
101+
</details>
107102

108-
### TiptapContainsText
103+
### Contains Text
109104

110105
The `TiptapContainsText` rule is used for verifying that the content contains text, and meets an optional character count requirements.
111106

112107
```php
113-
Validator::make($data, [
114-
'content' => TiptapValidation::containsText()
115-
// Minimum character requirement
116-
->minimum(12)
117-
// Maximum character requirement
118-
->maximum(156)
119-
// Minimum and maximum character requirement
120-
->between(12, 156),
121-
])
108+
TiptapValidation::containsText()
109+
->minimum(12) // Minimum character requirement
110+
->maximum(156) // Maximum character requirement
111+
->between(12, 156) // Minimum and maximum character requirement
122112
```
123113

124114
### Configuration

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
],
1818
"require": {
1919
"php": "^8.1",
20+
"laravel/helpers": "^1.6",
2021
"spatie/laravel-package-tools": "^1.15.0"
2122
},
2223
"require-dev": {

src/Helpers/TiptapContentHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static function attemptDecode(mixed $value): ?array
1616
try {
1717
$value = json_decode($value, true, 512, JSON_THROW_ON_ERROR);
1818

19-
if (! empty($value) && is_array($value)) {
19+
if (is_array($value) && filled($value)) {
2020
return $value;
2121
}
2222
} catch (\Exception $exception) {

src/Helpers/TiptapTextHelper.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ public static function countCharacters(array $nodes): int
1414
$characters += strlen($node[$textProperty] ?? '');
1515
}
1616

17-
foreach ($node['marks'] ?? [] as $mark) {
17+
foreach (array_get($node, 'marks', []) as $mark) {
1818
foreach ($textProperties as $textProperty) {
1919
$characters += strlen($mark[$textProperty] ?? '');
2020
}
2121
}
2222

2323
// Nested content
24-
if (! empty($node['content'])) {
25-
$characters += self::countCharacters($node['content']);
24+
if (filled(array_get($node, 'content'))) {
25+
$characters += self::countCharacters(array_get($node, 'content'));
2626
}
2727
}
2828

src/Rules/TiptapContainsText.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
6565
$value = TiptapContentHelper::attemptDecode($value);
6666

6767
// Invalid content
68-
if (empty($value) || ! is_array($value)) {
68+
if (! is_array($value) || blank($value)) {
6969
$fail(trans('tiptap-validation::messages.tiptapContainsText.noText'));
7070

7171
return;
7272
}
7373

7474
// Count text characters in content
75-
$length = TiptapTextHelper::countCharacters($value['content'] ?? []);
75+
$length = TiptapTextHelper::countCharacters(array_get($value, 'content', []));
7676

7777
// No text found
7878
if ($length === 0) {

src/Rules/TiptapContent.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function blacklist(): TiptapContent
9191
public function validate(string $attribute, mixed $value, Closure $fail): void
9292
{
9393
// Skip empty values
94-
if (empty($value)) {
94+
if (blank($value)) {
9595
return;
9696
}
9797

@@ -116,8 +116,8 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
116116

117117
// Begin content validation
118118
if (
119-
! empty($value['content']) &&
120-
! $this->validateNodes($value['content'])
119+
filled(array_get($value, 'content')) &&
120+
! $this->validateNodes(array_get($value, 'content'))
121121
) {
122122
$fail(trans('tiptap-validation::messages.tiptapContent'));
123123
}
@@ -145,13 +145,15 @@ protected function validateNodes(array $nodes): bool
145145
collect($nodes)
146146
->each(function (array $node) use (&$passes) {
147147
if (
148-
! empty($node['content']) &&
149-
! $this->validateNodes($node['content'])
148+
filled(array_get($node, 'content')) &&
149+
! $this->validateNodes(array_get($node, 'content'))
150150
) {
151151
$passes = false;
152152

153153
return false;
154154
}
155+
156+
return true;
155157
});
156158

157159
return $passes;

0 commit comments

Comments
 (0)