qdenka/easyvalidation

EasyValidation โ€” a package for simple, fast, and extendable data validation (email, URL, number, date, credit card, IP, phone, etc.).

1.0 2025-05-14 17:20 UTC

This package is auto-updated.

Last update: 2025-06-14 17:30:26 UTC


README

Latest Stable Version License Total Downloads

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:

  1. Implement ValidatorInterface in src/Domain/YourType/YourValidator.php.
  2. Add your class to VALIDATOR_MAP in src/Infrastructure/Factories/Validator.php.
  3. (Optional) Add a static helper in the same class.
  4. Write tests under tests/ following existing patterns.

๐Ÿงช Testing

Run PHPUnit:

vendor/bin/phpunit --colors

All tests must pass before merging.

๐Ÿค Contributing

  1. Fork repository
  2. Create a feature branch (git checkout -b feature/NewValidator)
  3. Commit changes (git commit -m 'Add NewValidator')
  4. Push (git push origin feature/NewValidator)
  5. 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.