承接 arefshojaei/class-validator 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

arefshojaei/class-validator

Composer 安装命令:

composer require arefshojaei/class-validator

包简介

PHP Class validator ( Attribute base ) library

README 文档

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.

统计信息

  • 总下载量: 14
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 6
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-07-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固