arefshojaei / class-validator
PHP Class validator ( Attribute base ) library
1.4.4
2026-06-16 15:20 UTC
Requires
- php: ^8.2
Requires (Dev)
- phpunit/phpunit: ^10
README
ðĄïļ Class Validator for PHP
A lightweight, powerful, and modern PHP 8.2+ validation library that uses Attributes to define validation rules directly inside your classes.
âĻ Features
- ð·ïļ Attribute-based validation using native PHP 8 attributes
- ð§Đ Validate entire objects and class properties
- ð Custom error messages for every validation rule
- ⥠Static validation methods for quick checks
- ðĶ Simple Composer installation
- ðŠķ Lightweight with zero unnecessary dependencies
- ð Strongly typed and modern PHP syntax
- ð ïļ Easy to extend with custom validation rules
ðĨ Installation
Install using Composer
composer require arefshojaei/class-validator
Clone from GitHub
git clone https://github.com/ArefShojaei/Class-validator.git
cd Class-validator
ð Quick Start
1. Create a DTO or Entity with Validation Attributes
<?php use Validator\Rules\{ Required, IsEmail, IsNumber, IsPositive, IsString, Length }; class User { #[IsString] #[Required] #[Length(min: 2, max: 50)] public string $name; #[Required] #[IsEmail] public string $email; #[Required] #[IsNumber] #[IsPositive] public int $age; }
2. Validate the Object
use Validator\Validator; $user = new User(); $user->name = "Aref"; $user->email = "invalid-email"; $user->age = 31; $validator = new Validator(); $isValid = $validator->validate($user); if (!$isValid) { print_r($validator->getErrors()); }
Output:
Array
(
[email] => Array
(
[IsEmail] => Invalid Email address!
)
)
ð§Đ Available Validation Rules
String Rules
| Rule | Description |
|---|---|
| IsString | Checks if the value is a string |
| Length | Validates minimum and maximum string length |
| Min | Checks minimum string length |
| Max | Checks maximum string length |
| IsLowercase | Checks lowercase strings |
| IsUppercase | Checks uppercase strings |
| Equals | Checks exact equality |
| NotEquals | Checks inequality |
Number Rules
| Rule | Description |
|---|---|
| IsNumber | Checks if the value is a number |
| IsPositive | Checks positive numbers |
| IsNegative | Checks negative numbers |
Collection Rules
| Rule | Description |
|---|---|
| IsArray | Validates arrays |
| Count | Checks array size |
| Contains | Checks if an array contains values |
| NotContains | Checks if an array does not contain values |
| Unique | Ensures all array values are unique |
Type & State Rules
| Rule | Description |
|---|---|
| Required | Checks whether a property exists |
| IsNull | Checks null values |
| IsNotEmpty | Checks that the value is not empty |
| IsEmpty | Checks empty values |
| IsBoolean | Validates booleans |
| IsObject | Validates objects |
| IsInstance | Checks object instance type |
Format Rules
| Rule | Description |
|---|---|
| IsEmail | Validates email addresses |
| IsUrl | Validates URLs |
| IsDate | Validates date formats |
| IsJSON | Validates JSON strings |
| IsIn | Checks allowed values |
| IsNotIn | Checks forbidden values |
ðĻ Custom Error Messages
Override default validation messages:
use Validator\Rules\Required; use Validator\Rules\IsEmail; class User { #[Required(message: "Name is required.")] public string $name; #[Required(message: "Email is required.")] #[IsEmail(message: "Please enter a valid email address.")] public string $email; }
⥠Static Validation
Use validation rules without creating a class:
use Validator\Validator; Validator::isEmail("test@gmail.com"); // true Validator::isEmail("invalid-email"); // "Invalid Email address!" Validator::length("Hello", 10, 20); // "The value must have the specified length!"
ðĄ Example Use Cases
This library is useful for:
- Request DTO validation
- API input validation
- Configuration validation
- Domain entities validation
- Form data validation
- Command-line input validation
ðĨ Why Class Validator?
Unlike traditional validation approaches that separate rules from data, Class Validator keeps validation rules close to your class properties using PHP Attributes.
This provides:
- Better readability
- Cleaner architecture
- Less duplicated code
- Better IDE support
- More maintainable applications
ðĪ Contributing
Contributions are welcome.
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature
- Commit your changes:
git commit -m "Add amazing feature"
- Push your branch:
git push origin feature/amazing-feature
- Open a Pull Request.
ðĻâðŧ Author
Aref Shojaei
- ð§ Email: arefshojaei82@gmail.com
- ð GitHub: @ArefShojaei
- ðĶ Packagist: arefshojaei/class-validator
â Show Your Support
If this project helps your PHP development workflow, consider giving it a Star â on GitHub.
Your support helps the project grow.