azolee/validator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

azolee/validator

最新稳定版本:v3.2.0

Composer 安装命令:

composer require azolee/validator

包简介

A Laravel like framework agnostic Validation package for PHP8.2

README 文档

README

Latest Version

The Laravel-like Validator is a PHP validation library designed to help you validate data structures with ease. It supports various validation rules, custom rules, and nested data validation.

Installation

You can install the package via Composer:

composer require azolee/validator

Usage

For a complete list of available validation rules, please refer to the Validation Rules document.

Basic Usage

To use the validator, you need to define your validation rules and the data to be validated. Then, call the Validator::make method.

use Azolee\Validator\Validator;

$validationRules = [
    'user.name' => 'string',
    'user.age' => 'numeric',
    'user.email' => ['email', 'not_null'],
    'user.website' => ['url'],
    'user.password' => ['password:ulds', 'min:8'],
    'user.password_confirmation' => ['same:user.password'],
    'user.is_active' => ['boolean', 'not_null'],
    'address' => 'array',
    'address.city' => 'string',
    'address.street' => ['string', 'different:address.city', 'different:address.street2', 'different:address.no'],
    'address.street2' => 'not_null',
    'address.no' => 'string',
    'images.*.url' => 'string',
    'images.*.role' => ['string', 'in:profile_photo,album_photo'],
];

$dataToValidate = [
    'user' => [
        'name' => 'John Doe',
        'email' => 'user@email.com',
        'password' => 'SecretPasswd#1',
        'password_confirmation' => 'secret',
        'website' => 'https://github.com',
        'age' => 30,
        'is_active' => true,
    ],
    'address' => [
        'city' => 'New York',
        'street' => 'First Avenue',
        'street2' => '',
        'no' => '52A',
    ],
    'images' => [
        [
            'url' => 'image1.jpg'
            'role' => 'profile_photo',
        ],
        [
            'url' => 'image2.jpg'
            'role' => 'album_photo',
            'description' => 'This is a photo of me.',
        ],
    ],
];

$result = Validator::make($validationRules, $dataToValidate);

if ($result->isFailed()) {
    echo "Validation failed!";
    print_r($result->getFailedRules());
} else {
    echo "Validation passed!";
}

Custom Rules

You can also define custom validation rules using closures.

$validationRules = [
    'user.name' => [
        function ($data) {
            return $data !== 'John Doe';
        },
        'string',
    ],
];

$dataToValidate = [
    'user' => [
        'name' => 'John Smith'
    ],
];

$result = Validator::make($validationRules, $dataToValidate);

if ($result->isFailed()) {
    echo "Validation failed!";
    print_r($result->getFailedRules());
} else {
    echo "Validation passed!";
}

Exception Handling

The validator can throw exceptions for invalid rules or data if the silent parameter is set to false.

use Azolee\Validator\Exceptions\InvalidValidationRule;
use Azolee\Validator\Exceptions\ValidationException;

try {
    $validationRules = [
        'name' => 123, // Invalid rule
    ];
    $dataToValidate = [
        'name' => 'John Doe',
    ];

    Validator::config(['silent' => false])->make($validationRules, $dataToValidate);
} catch (InvalidValidationRule $e) {
    echo "Caught an InvalidValidationRule exception: " . $e->getMessage();
} catch (ValidationException $e) {
    echo "Caught a ValidationException: " . $e->getMessage();
}

Additional Examples

For more detailed examples, please refer to the following documents:

Testing

To run the tests, use PHPUnit:

vendor/bin/phpunit

License

This package is open-sourced software licensed under the MIT license.

统计信息

  • 总下载量: 74
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 10
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 10
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-10-16