You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+44-9Lines changed: 44 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -38,15 +38,6 @@ it('throws validation error', function () {
38
38
});
39
39
```
40
40
41
-
Of course you can provide your own custom validation Rules:
42
-
43
-
```php
44
-
it('throws validation error', function () {
45
-
$this->postJson('/')
46
-
->assertJsonValidationErrorRule('foo', new MyCustomRule());
47
-
});
48
-
```
49
-
50
41
It supports as well dynamic rules, such as `between`, `size`, `max` etc. You just need to specify the type of rule you want to apply:
51
42
52
43
```php
@@ -71,6 +62,50 @@ it('throws validation error', function () {
71
62
});
72
63
```
73
64
65
+
### Custom rules
66
+
If you want to test a custom rule, make sure it implements the interface `DaniloPolani\JsonValidation\Contracts\HasRuleMessage`, needed to extract the failing message to check against.
67
+
68
+
For example, a custom Rule would look like this:
69
+
```php
70
+
<?php
71
+
72
+
namespace App\Rules;
73
+
74
+
use Closure;
75
+
use DaniloPolani\JsonValidation\Contracts\HasRuleMessage;
76
+
use Illuminate\Contracts\Validation\ValidationRule;
77
+
78
+
class Uppercase implements ValidationRule, HasRuleMessage
79
+
{
80
+
/**
81
+
* Run the validation rule.
82
+
*/
83
+
public function validate(string $attribute, mixed $value, Closure $fail): void
84
+
{
85
+
if (strtoupper($value) !== $value) {
86
+
$fail($this->message());
87
+
}
88
+
}
89
+
90
+
public function message(): string
91
+
{
92
+
return 'The :attribute must be uppercase.';
93
+
}
94
+
}
95
+
```
96
+
97
+
And then you can use it in your assert function:
98
+
99
+
100
+
Of course you can provide your own custom validation Rules:
101
+
102
+
```php
103
+
it('throws validation error', function () {
104
+
$this->postJson('/')
105
+
->assertJsonValidationErrorRule('foo', new MyCustomRule());
0 commit comments