DNS Validation Feature #10
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Enhanced the Laravel Domain Validation package to support optional DNS record verification, allowing developers to validate not just domain format but also the existence and values of DNS records.
New Features
1. DNS Record Type Checking
Added support for checking the existence of various DNS record types:
2. String Syntax Support
Developers can now use a simple string-based syntax for DNS checks:
3. Fluent API
Introduced a chainable fluent API for more control:
4. DNS Record Value Verification
Added ability to verify that DNS records resolve to expected values:
Multiple Records Matching: 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. This allows you to chain multiple calls to verify multiple specific values:
5. Enhanced Error Messages
Implemented specific error messages for different validation scenarios:
6. Backward Compatibility
The enhancement maintains full backward compatibility:
Technical Changes
Modified Files
src/Validator/Domain.phpChanges:
ValidatorAwareRuleinterfaceConditionable,MacroablerequireA(?string $expectedValue = null)requireAaaa(?string $expectedValue = null)requireCname(?string $expectedValue = null)requireTxt(?string $expectedValue = null)requireMx(?string $expectedValue = null)requireAny()parseParameters(array $parameters)hasAnyDnsRecords(string $domain)hasDnsRecord(string $domain, string $type)dnsRecordMatchesValue(string $domain, string $type, string $expectedValue)extractRecordValue(array $record, string $type)matchesExpectedValue(string $recordValue, string $expectedValue)getDnsConstant(string $type)trans(string $key, array $replace = [])passes()method with DNS checking logicmessage()method with specific error messagestests/DomainTest.phpAdded Tests:
testLocalhost()- Verify localhost handlingtestDnsCheckWithParameters()- Test string syntax with single parametertestDnsCheckWithMultipleParameters()- Test string syntax with multiple parameterstestDnsCheckAny()- Test 'any' DNS checktestFluentApiRequireMx()- Test fluent API MX checktestFluentApiRequireA()- Test fluent API A record checktestFluentApiChaining()- Test method chainingtestFluentApiRequireAny()- Test requireAny methodtestDnsCheckFailsForInvalidDomain()- Test DNS validation failuretestDnsCheckWithExpectedValue()- Test value matchingtestErrorMessage()- Test error message generationtestDnsErrorMessage()- Test DNS-specific error messagesMethods
requireA(?string $expectedValue = null): selfRequire A record. Optionally verify it points to a specific IP.
requireAaaa(?string $expectedValue = null): selfRequire AAAA record. Optionally verify it points to a specific IPv6 address.
requireCname(?string $expectedValue = null): selfRequire CNAME record. Optionally verify it points to a specific target.
requireTxt(?string $expectedValue = null): selfRequire TXT record. Optionally verify it contains a specific value.
requireMx(?string $expectedValue = null): selfRequire MX record. Optionally verify it points to a specific mail server.
requireAny(): selfRequire any DNS record (A, AAAA, CNAME, TXT, MX, NS, or SOA).
Translation Keys
Added support for custom translation keys:
validation.domain- Invalid domain format messagevalidation.domain_dns- Missing DNS record type messagevalidation.domain_dns_any- Missing any DNS record messagevalidation.domain_dns_value- DNS record value mismatch messageUsage Examples
Before (Still Supported)
After - String Syntax
After - Fluent API
Breaking Changes
None - This is a fully backward-compatible enhancement.
Performance Considerations
Testing
All tests pass:
Run tests:
Future Enhancements
Potential improvements for future versions:
Credits
Based on the Laravel Email validation rule pattern and inspired by: