morebec/validator

Validator 组件提供了验证数据的功能。

0.2.0 2020-03-06 11:12 UTC

This package is auto-updated.

Last update: 2024-09-06 22:46:41 UTC


README

Build Status

Validator 组件使得数据验证变得简单。其主要用途是为了确保表单、Rest API 和配置文件中的数据处于适当且有效的状态。

许多验证库依赖于异常机制。这迫使开发者逐个检查多个相关数据字段。该组件相反使用可以一起用于多份数据的验证错误。例如,这可以一次性返回给定表单的所有错误。

安装

composer require morebec/validator

用法

use Morebec\Validator\Rule as Assert;
use Morebec\Validator\Validator;


// Validate a single rule for a given field
Validator::validate($form['email_address'], new Assert\One(
    new Assert\IsString('The email address field was expected to be a string')
));

// Validate Multiple Rules for a given field
Validator::validate($form['email_address'], new Assert\All([
    new Assert\IsString('The email address field was expected to be a string'),
    new Assert\NotBlank('The email address field was expected not to be blank'),
    new Assert\IsEmail('The email address field was expected to be a valid email address'),
]));


// Ensure At least one rule is valid
Validator::validate($form['email_address'], new Assert\AtLeastOne([
    new Assert\IsString('The email address field was expected to be a string'),
    new Assert\NotBlank('The email address field was expected not to be blank'),
    new Assert\IsEmail('The email address field was expected to be a valid email address'),
]));

运行测试

php vendor/bin/phpunit --bootstrap vendor/autoload.php tests

贡献

感谢您的关注 :)
要贡献,请阅读 CONTRIBUTING.md 指南。

获取帮助

要获取帮助,请在此存储库上打开一个新问题。
对于 Morebec 团队成员,请使用适当的内部渠道。