arefshojaei/class-validator

PHP Class validator ( Attribute base ) library

Maintainers

Package info

github.com/ArefShojaei/Class-validator

pkg:composer/arefshojaei/class-validator

Transparency log

Statistics

Installs: 14

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

1.4.4 2026-06-16 15:20 UTC

This package is auto-updated.

Last update: 2026-06-16 15:20:54 UTC


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.

PHP Version License Packagist

âœĻ 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.

  1. Fork the repository
  2. Create a feature branch:
git checkout -b feature/amazing-feature
  1. Commit your changes:
git commit -m "Add amazing feature"
  1. Push your branch:
git push origin feature/amazing-feature
  1. Open a Pull Request.

ðŸ‘Ļ‍ðŸ’ŧ Author

Aref Shojaei

⭐ Show Your Support

If this project helps your PHP development workflow, consider giving it a Star ⭐ on GitHub.

Your support helps the project grow.