定制 webdevcave/schema-validator 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

webdevcave/schema-validator

最新稳定版本:v0.1-alpha

Composer 安装命令:

composer require webdevcave/schema-validator

包简介

Schema validation, 'Zod' like validation

README 文档

README

codecov StyleCI

A simple schema validation library for PHP. This package allows you to define rules for your data and validate it easily.

Table of Contents

Installation

To install the Schema Validator PHP library, you can use Composer. Run the following command:

composer require webdevcave/schema-validator-php

Usage

Basic Usage

use Webdevcave\SchemaValidator\Validator;

$validator = Validator::string()
    ->min(3)
    ->max(50);

// Validate the data
$isValid = $validator->validate('Carlos');

if ($isValid) {
    echo "Data is valid!";
} else {
    echo "Data is invalid!";
}

Dataset Validation Example

The library also allows you to define more complex validation rules for nested structures or arrays. For example:

use Webdevcave\SchemaValidator\Validator;

$validator = Validator::array([
    'name' => Validator::string()
        ->min(3)
        ->max(50),
    'email' => Validator::string()
        ->pattern('/^\w+@(\w+|\.)+$/'),
    'address' => Validator::array([
        'street' => Validator::string()->max(200),
        'city' => Validator::string()->max(50),
        'postal_code' => Validator::string()
            ->min(1)
            ->max(15),
    ])
]);

// Data to be validated
$data = [
    'name' => 'Alice Johnson',
    'email' => 'alice.johnson@example.com',
    'address' => [
        'street' => '123 Maple St',
        'city' => 'Somewhere',
        'postal_code' => '12345'
    ]
];

// Validate the data
$isValid = $validator->validate($data);

if ($isValid) {
    echo "Data is valid!";
} else {
    echo "Data is invalid!";
}

For objects, just use Validator::object() in a similar way.

Validation Error Handling

If the data is invalid, you can get more detailed error information:

use Webdevcave\SchemaValidator\Validator;

// Data to be validated
$data = [
    'name' => 'John Doe',
    'age' => 'thirty'
];

// Define the validation schema
$validator = Validator::array([
    'name'  => Validator::string(),
    'age'   => Validator::numeric()
        ->integer('Only integer numbers are allowed')
        ->positive('Age field must be positive')
]);

// Validate the data
if (!$validator->validate($data)) {
    // Get and print the validation errors
    print_r($validator->errorMessages());
}

This will output something like:

Array
(
    [age] => Array 
    (
        [0] => Only integer numbers are allowed,
        [1] => Age field must be positive,   
    )
)

Contributing

We welcome contributions to this project! If you'd like to help improve the Schema Validator PHP library, please follow these steps:

How to Contribute

  1. Fork this repository to your GitHub account.
  2. Create a new branch for your feature or fix.
  3. Make your changes and test them thoroughly.
  4. Submit a pull request with a description of your changes and why they're needed.

Code Style

Please follow the PSR-12 coding standards for PHP when making contributions.

Issues

If you encounter any bugs or have suggestions for improvements, please open an issue in the GitHub repository.

License

This project is licensed under the MIT License - see the LICENSE file for details.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-01-14