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

6
6
7
-
A domain validator rule for Laravel 10.x and higher.
7
+
A domain validator rule for Laravel 10.x and higher with optional DNS record verification.
8
8
9
-
## Usage
9
+
## Features
10
10
11
+
- Validates domain format using regex
12
+
- Optional DNS record checking (A, AAAA, CNAME, TXT, MX records)
13
+
- Verify DNS records resolve to expected values
14
+
- Fluent API for chaining multiple DNS checks
15
+
- String-based validation syntax support
16
+
- Designed for external domains with DNS records
17
+
18
+
## Installation
19
+
20
+
```bash
21
+
composer require dacoto/laravel-domain-validation
11
22
```
23
+
24
+
## Basic Usage
25
+
26
+
### Simple Domain Validation
27
+
28
+
Validates that the input is a properly formatted domain:
29
+
30
+
```php
12
31
use dacoto\DomainValidator\Validator\Domain;
13
32
14
33
public function rules()
@@ -18,3 +37,226 @@ public function rules()
18
37
];
19
38
}
20
39
```
40
+
41
+
### String Syntax with DNS Checks
42
+
43
+
You can use the string syntax to check for the presence of DNS records:
44
+
45
+
```php
46
+
public function rules()
47
+
{
48
+
return [
49
+
50
+
// Require DNS records to exist (any type)
51
+
'hostname' => ['required', 'domain:dns'],
52
+
// Require MX records
53
+
'domain' => ['required', 'domain:mx'],
54
+
55
+
// Require multiple DNS record types
56
+
'website' => ['required', 'domain:a,mx'],
57
+
58
+
];
59
+
}
60
+
```
61
+
62
+
### Fluent API
63
+
64
+
For more control, use the fluent API:
65
+
66
+
```php
67
+
use dacoto\DomainValidator\Validator\Domain;
68
+
69
+
public function rules()
70
+
{
71
+
return [
72
+
// Check for DNS records
73
+
'hostname' => ['required', (new Domain)->requireDns()],
74
+
// Check for MX records
75
+
'email_domain' => ['required', (new Domain)->requireMx()],
76
+
77
+
// Chain multiple DNS checks
78
+
'website' => ['required', (new Domain)->requireA()->requireMx()],
79
+
80
+
];
81
+
}
82
+
```
83
+
84
+
## DNS Record Type Checks
85
+
86
+
### Available DNS Record Types
87
+
88
+
-`dns` - Checks for presence of any DNS record (A, AAAA, CNAME, TXT, MX, NS, or SOA)
89
+
-`a` - IPv4 address records
90
+
-`aaaa` - IPv6 address records
91
+
-`cname` - Canonical name records
92
+
-`txt` - Text records
93
+
-`mx` - Mail exchange records
94
+
95
+
### Verifying DNS Records Match Expected Values
96
+
97
+
You can verify that DNS records resolve to specific values using the fluent API:
98
+
99
+
```php
100
+
use dacoto\DomainValidator\Validator\Domain;
101
+
102
+
public function rules()
103
+
{
104
+
return [
105
+
// Require A record pointing to specific IP
106
+
'domain' => ['required', (new Domain)->requireA('192.0.2.1')],
107
+
108
+
// Require MX record pointing to specific mail server
109
+
'email_domain' => ['required', (new Domain)->requireMx('mail.example.com')],
110
+
111
+
// Multiple checks with specific values
112
+
'website' => [
113
+
'required',
114
+
(new Domain)
115
+
->requireA('192.0.2.1')
116
+
->requireMx('mail.example.com')
117
+
],
118
+
];
119
+
}
120
+
```
121
+
122
+
#### Multiple Expected Values
123
+
124
+
When a domain has multiple DNS records of the same type (e.g., multiple A records for load balancing), the validator passes if **ANY** of the records match the expected value. You can chain multiple calls to verify multiple specific values:
125
+
126
+
```php
127
+
public function rules()
128
+
{
129
+
return [
130
+
// Verify domain has BOTH specific IPs
131
+
// (each requireA checks if that IP exists among the A records)
132
+
'cdn_domain' => [
133
+
'required',
134
+
(new Domain)
135
+
->requireA('104.16.132.229')
136
+
->requireA('104.16.133.229')
137
+
],
138
+
139
+
// Example with Google's public DNS (dns.google.com)
140
+
// which resolves to both 8.8.8.8 and 8.8.4.4
141
+
'google_dns' => [
142
+
'required',
143
+
(new Domain)
144
+
->requireA('8.8.8.8') // Passes if this IP is found
145
+
->requireA('8.8.4.4') // Also passes if this IP is found
0 commit comments