Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 47dc2aa

Browse files
committed
feat(docs): added Url rule documentation
1 parent 0e7995c commit 47dc2aa

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

docs/03-rules.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
## String Rules
1616

1717
- [Email](03x-rules-email.md)
18+
- [URL](03x-rules-url.md)
1819

1920
## Comparison Rules
2021

docs/03x-rules-url.md

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# URL
2+
3+
Validates that a value is a valid URL address.
4+
5+
```php
6+
Url(
7+
array $protocols = ['http', 'https'],
8+
bool $allowRelativeProtocol = false,
9+
?callable $normalizer = null,
10+
string $message = 'The {{ name }} value is not a valid URL address, {{ value }} given.'
11+
);
12+
```
13+
14+
## Basic Usage
15+
16+
```php
17+
Validator::url()->validate('https://example.com'); // true
18+
19+
// Only allow the https protocol
20+
Validator::url(protocols: ['https'])->validate('http://example.com'); // false
21+
// Or allow the ftp protocol too
22+
Validator::url(protocols: ['https', 'ftp'])->validate('ftp://example.com'); // true
23+
24+
// Allow relative protocol
25+
Validator::url()->validate('//example.com'); // false
26+
Validator::url(allowRelativeProtocol: true)->validate('//example.com'); // true
27+
Validator::url(allowRelativeProtocol: true)->validate('https://example.com'); // true
28+
```
29+
30+
## Options
31+
32+
### `protocols`
33+
34+
type: `array` default: `['http', 'https']`
35+
36+
Set this option to define the allowed protocols.
37+
38+
### `allowRelativeProtocol`
39+
40+
type: `bool` default: `false`
41+
42+
If this option is `true`, inclusion of a protocol in the URL will be optional.
43+
44+
### `normalizer`
45+
46+
type: `callable` default: `null`
47+
48+
Allows to define a `callable` that will be applied to the value before checking if it is valid.
49+
50+
For example, use `trim`, or pass your own function, to ignore whitespace in the beginning or end of a URL address:
51+
52+
```php
53+
Validator::url()->validate('https://example.com '); // false
54+
55+
Validator::url(normalizer: 'trim')->validate('https://example.com '); // true
56+
Validator::url(normalizer: fn($value) => trim($value))->validate('https://example.com '); // true
57+
```
58+
59+
### `message`
60+
61+
type `string` default: `The {{ name }} value is not a valid URL address, {{ value }} given.`
62+
63+
Message that will be shown if the input value is not a valid URL address.
64+
65+
The following parameters are available:
66+
67+
| Parameter | Description |
68+
|-------------------|---------------------------|
69+
| `{{ value }}` | The current invalid value |
70+
| `{{ name }}` | Name of the invalid value |
71+
| `{{ protocols }}` | Allowed protocols |
72+
73+
## Changelog
74+
75+
- `0.6.0` Created

0 commit comments

Comments
 (0)