PHPackages                             dacoto/laravel-domain-validation - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. dacoto/laravel-domain-validation

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

dacoto/laravel-domain-validation
================================

A domain validator rule for Laravel

4.3.0(4mo ago)23191.4k↓51.4%9MITPHPPHP ^8.1CI failing

Since Apr 5Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/dacoto/laravel-domain-validation)[ Packagist](https://packagist.org/packages/dacoto/laravel-domain-validation)[ GitHub Sponsors](https://github.com/dacoto97)[ RSS](/packages/dacoto-laravel-domain-validation/feed)WikiDiscussions main Synced 3d ago

READMEChangelog (10)Dependencies (4)Versions (13)Used By (0)

Laravel Domain Validation
=========================

[](#laravel-domain-validation)

[![GitHub Workflow Status (branch)](https://camo.githubusercontent.com/32834b824dd10040af36104b0ee0b8f56ace631929ffe0147b617e6b257aca9f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6461636f746f2f6c61726176656c2d646f6d61696e2d76616c69646174696f6e2f72756e2d74657374732e796d6c)](https://camo.githubusercontent.com/32834b824dd10040af36104b0ee0b8f56ace631929ffe0147b617e6b257aca9f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6461636f746f2f6c61726176656c2d646f6d61696e2d76616c69646174696f6e2f72756e2d74657374732e796d6c)[![GitHub](https://camo.githubusercontent.com/26400858f5c3f42b0f083898585a50b1bd366c32ad632a2264aace34602d219f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6461636f746f2f6c61726176656c2d646f6d61696e2d76616c69646174696f6e)](https://camo.githubusercontent.com/26400858f5c3f42b0f083898585a50b1bd366c32ad632a2264aace34602d219f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6461636f746f2f6c61726176656c2d646f6d61696e2d76616c69646174696f6e)[![GitHub release (latest by date)](https://camo.githubusercontent.com/c4ef248f9d79a7255a6706ee49ce7969827a33d1cb30f430f700fb2d512d1d52/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f6461636f746f2f6c61726176656c2d646f6d61696e2d76616c69646174696f6e)](https://camo.githubusercontent.com/c4ef248f9d79a7255a6706ee49ce7969827a33d1cb30f430f700fb2d512d1d52/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f6461636f746f2f6c61726176656c2d646f6d61696e2d76616c69646174696f6e)

A domain validator rule for Laravel 10.x and higher with optional DNS record verification.

Features
--------

[](#features)

- Validates domain format using regex
- Optional DNS record checking (A, AAAA, CNAME, TXT, MX records)
- Verify DNS records resolve to expected values
- Fluent API for chaining multiple DNS checks
- String-based validation syntax support
- Designed for external domains with DNS records

Installation
------------

[](#installation)

```
composer require dacoto/laravel-domain-validation
```

Basic Usage
-----------

[](#basic-usage)

### Simple Domain Validation

[](#simple-domain-validation)

Validates that the input is a properly formatted domain:

```
use dacoto\DomainValidator\Validator\Domain;

public function rules()
{
    return [
        'domain' => ['required', 'string', new Domain],
    ];
}
```

### String Syntax with DNS Checks

[](#string-syntax-with-dns-checks)

You can use the string syntax to check for the presence of DNS records:

```
public function rules()
{
    return [

        // Require DNS records to exist (any type)
        'hostname' => ['required', 'domain:dns'],
                // Require MX records
        'domain' => ['required', 'domain:mx'],

        // Require multiple DNS record types
        'website' => ['required', 'domain:a,mx'],

    ];
}
```

### Fluent API

[](#fluent-api)

For more control, use the fluent API:

```
use dacoto\DomainValidator\Validator\Domain;

public function rules()
{
    return [
        // Check for DNS records
        'hostname' => ['required', (new Domain)->requireDns()],
        // Check for MX records
        'email_domain' => ['required', (new Domain)->requireMx()],

        // Chain multiple DNS checks
        'website' => ['required', (new Domain)->requireA()->requireMx()],

    ];
}
```

DNS Record Type Checks
----------------------

[](#dns-record-type-checks)

### Available DNS Record Types

[](#available-dns-record-types)

- `dns` - Checks for presence of any DNS record (A, AAAA, CNAME, TXT, MX, NS, or SOA)
- `a` - IPv4 address records
- `aaaa` - IPv6 address records
- `cname` - Canonical name records
- `txt` - Text records
- `mx` - Mail exchange records

### Verifying DNS Records Match Expected Values

[](#verifying-dns-records-match-expected-values)

You can verify that DNS records resolve to specific values using the fluent API:

```
use dacoto\DomainValidator\Validator\Domain;

public function rules()
{
    return [
        // Require A record pointing to specific IP
        'domain' => ['required', (new Domain)->requireA('192.0.2.1')],

        // Require MX record pointing to specific mail server
        'email_domain' => ['required', (new Domain)->requireMx('mail.example.com')],

        // Multiple checks with specific values
        'website' => [
            'required',
            (new Domain)
                ->requireA('192.0.2.1')
                ->requireMx('mail.example.com')
        ],
    ];
}
```

#### Multiple Expected Values

[](#multiple-expected-values)

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:

```
public function rules()
{
    return [
        // Verify domain has BOTH specific IPs
        // (each requireA checks if that IP exists among the A records)
        'cdn_domain' => [
            'required',
            (new Domain)
                ->requireA('104.16.132.229')
                ->requireA('104.16.133.229')
        ],

        // Example with Google's public DNS (dns.google.com)
        // which resolves to both 8.8.8.8 and 8.8.4.4
        'google_dns' => [
            'required',
            (new Domain)
                ->requireA('8.8.8.8')  // Passes if this IP is found
                ->requireA('8.8.4.4')  // Also passes if this IP is found
        ],
    ];
}
```

Advanced Examples
-----------------

[](#advanced-examples)

### Form Request Validation

[](#form-request-validation)

```
use Illuminate\Foundation\Http\FormRequest;
use dacoto\DomainValidator\Validator\Domain;

class CreateWebsiteRequest extends FormRequest
{
    public function rules()
    {
        return [
            'domain' => [
                'required',
                'string',
                (new Domain)->requireA()->requireMx()
            ],
        ];
    }
}
```

### Manual Validator

[](#manual-validator)

```
use Illuminate\Support\Facades\Validator;
use dacoto\DomainValidator\Validator\Domain;

$validator = Validator::make($request->all(), [
    'domain' => ['required', new Domain('mx', 'a')],
]);

if ($validator->fails()) {
    return redirect()->back()->withErrors($validator);
}
```

### Conditional DNS Checks

[](#conditional-dns-checks)

```
use dacoto\DomainValidator\Validator\Domain;

public function rules()
{
    return [
        'domain' => [
            'required',
            (new Domain)
                ->requireA()
                ->when($this->check_email, function ($rule) {
                    return $rule->requireMx();
                })
        ],
    ];
}
```

Error Messages
--------------

[](#error-messages)

The validator provides specific error messages for different validation failures:

- **Invalid format**: "The {attribute} is not a valid domain."
- **Missing DNS records**: "The {attribute} must have valid {TYPE} records."
- **Missing any DNS**: "The {attribute} must have valid DNS records."
- **Value mismatch**: "The {attribute} must have a {TYPE} record pointing to {value}."

### Custom Error Messages

[](#custom-error-messages)

You can customize error messages in your language files:

```
// resources/lang/en/validation.php

return [
    'domain' => 'The :attribute must be a valid domain name.',
    'domain_dns' => 'The :attribute must have valid :type DNS records.',
    'domain_dns_any' => 'The :attribute must have valid DNS records configured.',
    'domain_dns_value' => 'The :attribute must have a :type record pointing to :value.',
];
```

Requirements
------------

[](#requirements)

- PHP 8.1 or higher
- Laravel 10.x or higher

Testing
-------

[](#testing)

Run the test suite:

```
composer test
```

The package includes comprehensive tests covering:

- ✅ Basic domain format validation
- ✅ String-based syntax (via constructor: `new Domain('a')`, `new Domain('a', 'mx')`)
- ✅ Fluent API methods (chaining, expected values)
- ✅ All DNS record types (A, AAAA, CNAME, TXT, MX, ANY)
- ✅ Multiple expected values (load balancing scenarios)
- ✅ Error messages and edge cases
- ✅ Uses reliable test domain (dns.google.com)

**Test Results:** 20 tests, 37 assertions, 100% passing ✅

License
-------

[](#license)

This package is open-sourced software licensed under the MIT license.

###  Health Score

57

—

FairBetter than 98% of packages

Maintenance77

Regular maintenance activity

Popularity45

Moderate usage in the ecosystem

Community17

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 78.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~215 days

Recently: every ~277 days

Total

11

Last Release

125d ago

Major Versions

1.2.0 → 2.0.02020-12-12

2.2.0 → 3.0.02023-02-16

3.0.0 → 4.0.02024-02-28

PHP version history (4 changes)1.0.0PHP ^7.2

2.0.0PHP ^7.2|^8.0

3.0.0PHP ^8.0

4.0.0PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/fe58dc29067fa49a3c68086edeba0f324ce342118432e34a5556954c7d3a8fbf?d=identicon)[dacoto](/maintainers/dacoto)

---

Top Contributors

[![dacoto](https://avatars.githubusercontent.com/u/16915053?v=4)](https://github.com/dacoto "dacoto (30 commits)")[![jonathandey](https://avatars.githubusercontent.com/u/634009?v=4)](https://github.com/jonathandey "jonathandey (2 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (2 commits)")[![Copilot](https://avatars.githubusercontent.com/in/1143301?v=4)](https://github.com/Copilot "Copilot (1 commits)")[![scottcharlesworth](https://avatars.githubusercontent.com/u/8984512?v=4)](https://github.com/scottcharlesworth "scottcharlesworth (1 commits)")[![nickmoline](https://avatars.githubusercontent.com/u/1545057?v=4)](https://github.com/nickmoline "nickmoline (1 commits)")[![dbfx](https://avatars.githubusercontent.com/u/182394?v=4)](https://github.com/dbfx "dbfx (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/dacoto-laravel-domain-validation/health.svg)

```
[![Health](https://phpackages.com/badges/dacoto-laravel-domain-validation/health.svg)](https://phpackages.com/packages/dacoto-laravel-domain-validation)
```

###  Alternatives

[propaganistas/laravel-phone

Adds phone number functionality to Laravel based on Google's libphonenumber API.

3.0k39.7M146](/packages/propaganistas-laravel-phone)[spatie/laravel-honeypot

Preventing spam submitted through forms

1.6k6.8M75](/packages/spatie-laravel-honeypot)[intervention/validation

Additional validation rules for the Laravel framework

6827.2M20](/packages/intervention-validation)[laravel/mcp

Rapidly build MCP servers for your Laravel applications.

77022.3M151](/packages/laravel-mcp)[axlon/laravel-postal-code-validation

Worldwide postal code validation for Laravel

3893.6M1](/packages/axlon-laravel-postal-code-validation)[proengsoft/laravel-jsvalidation

Validate forms transparently with Javascript reusing your Laravel Validation Rules, Messages, and FormRequest

1.1k2.4M50](/packages/proengsoft-laravel-jsvalidation)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
