anshu-krishna/data-validator

一个用于简化复杂数据验证的 PHP 库。

2.8.1 2024-09-15 21:14 UTC

This package is auto-updated.

Last update: 2024-09-15 21:16:22 UTC


README

一个用于简化复杂数据验证的 PHP 库。

注意:版本 >=2.0 与旧版本不兼容。

安装

composer require anshu-krishna/data-validator

特性

  • 支持的类型

    • bool : 布尔值
    • email : 包含电子邮件地址的字符串
    • float : 浮点值
    • hex : 包含十六进制值的字符串
    • int : 整数值
    • ipv4 : 包含 IPv4 地址的字符串
    • ipv6 : 包含 IPv6 地址的字符串
    • mac : 包含 MAC 地址的字符串
    • mixed : 任何值
    • null : 空值
    • number : 整数或浮点值
    • string : 字符串值
    • timestamp : 包含时间戳的字符串。
      例如:'2021-01-31', 'today', 'yesterday', '01-Jan-2021', 'January 1, 2021 05:00:10 AM GMT+05:30', 等。
    • timestamp_utc : 包含时间戳的字符串(它将被转换为 UTC 时区)。
      例如:'2021-01-31', 'today', 'yesterday', '01-Jan-2021', 'January 1, 2021 05:00:10 AM GMT+05:30', '2024-06-15T20:12:52+05:30:10', 等。
    • unsigned : Int >= 0
    • url : 包含 URL 的字符串
    • uuid : 包含 UUIDv4 的字符串
  • 还可以添加自定义数据类型。例如,请参阅 ./examples

  • 可以为数据项设置多个替代数据类型。例如:'int|float|null''ipv4|ipv6',等。

  • 支持 Ranger/Formatter 验证数据

示例

<?php
require_once 'vendor/autoload.php';

use Krishna\DataValidator\ComplexException;
use Krishna\DataValidator\OutOfBoundAction;
use Krishna\DataValidator\Validator;
try {
   $dv = new Validator([
      'a' => 'int',
      // a is required and must be an integer
      
      '?b' => 'float',
      // b is optional and must be a float
      
      '??c' => 'unsigned',
      // c is optional and must be an unsigned integer;
      // if not provided, it will be set to null
      
      'd' => 'null|int|uuid',
      // d is required and must be an integer or a UUIDv4 or null
   ], OutOfBoundAction::Trim);
}
catch (ComplexException $e) {
   echo 'Unable to create validator';
   var_dump($th->getInfo());
   die();
}

$result = $dv->validate([
   'a' => 10,
   'b' => '20.5',
   'd' => 'f47ac10b-58cc-4372-a567-0e02b2c3d479',
]);
var_dump($result);
/*
Output:
object(Krishna\DataValidator\Returner)[14]
public readonly mixed 'value' => 
array (size=4)
   'a' => int 10
   'b' => float 20.5
   'c' => null
   'd' => string 'f47ac10b-58cc-4372-a567-0e02b2c3d479' (length=36)
public readonly mixed 'error' => null
public readonly bool 'valid' => boolean true
*/

$result = $dv->validate([
   'a' => '10',
   'b' => 20,
   'c' => 5,
   'd' => 'f47ac10b-58cc-4372-a567-0e02b2c3d479',
]);
var_dump($result);
/*
Output:
object(Krishna\DataValidator\Returner)[7]
public readonly mixed 'value' => 
array (size=4)
   'a' => int 10
   'b' => float 20
   'c' => int 5
   'd' => string 'f47ac10b-58cc-4372-a567-0e02b2c3d479' (length=36)
public readonly mixed 'error' => null
public readonly bool 'valid' => boolean true
*/

$result = $dv->validate([
   'a' => '0x10',
   'd' => 20,
   'e' => 30,
]);
var_dump($result);
/*
Output:
object(Krishna\DataValidator\Returner)[17]
public readonly mixed 'value' => 
array (size=3)
   'a' => int 16
   'c' => null
   'd' => int 20
public readonly mixed 'error' => null
public readonly bool 'valid' => boolean true
*/

$result = $dv->validate([
   'b' => 'abc',
   'c' => 'def',
   'd' => 'null',
]);
var_dump($result);
/*
Output:
object(Krishna\DataValidator\Returner)[7]
public readonly mixed 'value' => null
public readonly mixed 'error' => 
object(Krishna\DataValidator\ErrorReader)[14]
   public 0 => string '[a]: Missing' (length=12)
   public 1 => string '[b]: Expected 'float'' (length=21)
   public 2 => string '[c]: Expected 'unsigned'' (length=24)
public readonly bool 'valid' => boolean false
*/

请参阅 ./examples 了解更多信息。

文档/示例正在建设中