qdenka / easyvalidation
EasyValidation โ a package for simple, fast, and extendable data validation (email, URL, number, date, credit card, IP, phone, etc.).
Requires
- php: >=7.4
- ext-intl: *
- psr/container: ^1.1
Requires (Dev)
- phpstan/phpstan: ^1.0
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2025-06-14 17:30:26 UTC
README
EasyValidation is a lightweight PHP library for validating common data types. Built with DDD, SOLID, and DRY principles, it offers both a factory-based API and convenient static helper methods.
๐ Table of Contents
โจ Features
- โ Validate emails (standard, Gmail-only, disposable)
- โ Validate URLs, numbers, dates
- โ Validate IPs (IPv4 & IPv6)
- โ Validate UUIDs (v1โv5)
- โ Validate JSON and Base64 strings
- โ Validate international phone numbers (E.164)
- ๐ฆ PSR-4 autoloading
- ๐ Factory-based API + static helpers
- โ๏ธ Easily extendable with new validators
โ๏ธ Requirements
- PHP ^7.4 || ^8.0
๐ Installation
Install with Composer:
composer require qdenka/easyvalidation
Autoloading is configured via PSR-4 (QDenka\EasyValidation\
).
๐ ๏ธ Usage
Factory API
Use ValidatorFactory
to retrieve any validator by its type key:
use QDenka\EasyValidation\Application\Validators\ValidatorFactory; \$types = ['email','google_email','disposable_email','url','number','date','ip','uuid','json','base64','phone']; foreach (\$types as \$type) { \$validator = ValidatorFactory::create(\$type); \$value = 'test_value_for_' . \$type; if (\$validator && \$validator->validate(\$value)) { echo "[\$type] valid\n"; } else { echo "[\$type] invalid\n"; } }
Static Helpers
For quick checks, use the Validator
facade:
use QDenka\EasyValidation\Infrastructure\Factories\Validator; if (Validator::isValidEmail('user@example.com')) { echo "Valid email"; } if (Validator::isGoogleMail('user@gmail.com')) { echo "It's a Gmail address"; } if (!Validator::isDisposableEmail('temp@mailinator.com')) { echo "Disposable email detected"; } // Other helpers: Validator::isValidUrl('https://example.com'); Validator::isValidNumber('123.45'); Validator::isValidDate('2023-04-23'); Validator::isValidIp('2001:db8::1'); Validator::isValidUuid('550e8400-e29b-41d4-a716-446655440000'); Validator::isValidJson('{"foo":1}'); Validator::isValidBase64(base64_encode('hello')); Validator::isValidPhone('+1234567890');
๐ Available Validators
Type | Static Helper | Description |
---|---|---|
email |
isValidEmail() |
RFC-compliant email |
google_email |
isGoogleMail() |
Gmail & Googlemail domains |
disposable_email |
!isDisposableEmail() |
Blacklisted disposable domains |
url |
isValidUrl() |
HTTP/HTTPS URLs |
number |
isValidNumber() |
Numeric values |
date |
isValidDate() |
Date in Y-m-d (configurable format) |
ip |
isValidIp() |
IPv4 & IPv6 addresses |
uuid |
isValidUuid() |
UUID v1โv5 |
json |
isValidJson() |
JSON string parsing |
base64 |
isValidBase64() |
Base64-encoded data |
phone |
isValidPhone() |
International E.164 phone numbers |
โ๏ธ Configuration
Disposable Domains List
Customize disposable email domains by editing config/disposable_domains.php
:
return [ 'mailinator.com', '10minutemail.com', // add your domains here ];
Ensure all domains are lowercase.
๐งฉ Extending
To add a new validator:
- Implement
ValidatorInterface
insrc/Domain/YourType/YourValidator.php
. - Add your class to
VALIDATOR_MAP
insrc/Infrastructure/Factories/Validator.php
. - (Optional) Add a static helper in the same class.
- Write tests under
tests/
following existing patterns.
๐งช Testing
Run PHPUnit:
vendor/bin/phpunit --colors
All tests must pass before merging.
๐ค Contributing
- Fork repository
- Create a feature branch (
git checkout -b feature/NewValidator
) - Commit changes (
git commit -m 'Add NewValidator'
) - Push (
git push origin feature/NewValidator
) - Open a Pull Request
Follow PSR-12 coding standards and include tests.
๐ License
This project is licensed under the MIT License. See LICENSE for details.