Vection Validator 提供数据和模式验证。已包含多种不同的验证器,可以以链式方式应用。

v0.3.4 2020-06-13 12:09 UTC

This package is auto-updated.

Last update: 2024-09-29 05:22:05 UTC


README

release QA PHPStan

Vection Validator

PHP 数据和模式验证器

此 vection 组件为 PHP 提供数据模式验证器。您可以使用它从验证简单值到通过链式验证器验证大数据数组。此组件还提供基于模式的数据结构验证。vection 组件的灵活性和开放/封闭特性允许扩展和修改此组件的大多数类或定义自定义验证器类。此组件可用于以下场景

  • 简单值验证
  • 通过链式验证器验证数据集
  • 扩展自定义验证器
  • 基于模式的数据结构验证

安装

Vection Components 仅支持通过 composer 安装。因此,首先确保您的 composer 已安装、配置并准备好使用。

$ composer require vection-framework/validator

简单值验证

$validator = new Vection\Component\Validator\Validator\Date("H:i:s");

# Each validator returns null on success or an object of Violation on fail
$violation = $validator->validate("17:12-43");

if( $violation ){
    $violation->getMessage();   // or
    $violation->getValue();     // or
    echo $violation; // Date "17:12-43" is invalid or does not match format "H:i:s".
} 

通过链式验证器验证数据集

// e.g. the POST request input
$data = [
    'name' => 'John Doe',
    'age' => 42,
    'date' => '2019-02-03'
];
    
$chain = new Vection\Component\Validator\ValidatorChain();
    
$chain('name')
    ->notNull()
    ->alphaNumeric()
    ->betweenLength(3, 20)
;

$chain('age')
    ->notNull()
    ->numeric()
    ->range(0, 100)
;

$chain('date')
    ->nullable()
    ->date("Y-m-d")
;

$chain->verify($data);

if( $violations = $chain->getViolations() ){
    //here we got an array of Violation objects
    print json_encode($violations);
    # output: {"name": {"value":"2019-02-03", "message":"..."}, "age": {...}, ...}
} 

使用自定义验证器实现

class CustomValidator extends Vection\Component\Validator\Validator { ... }

$customValidator = new CustomValidator(...);
$customValidator->validate(...);

// or

$chain = new ValidatorChain();

$chain('xxx')
    ->notNull()
    ->use($customValidator)
;

基于模式的数据结构验证

模式验证器使用 JSON 模式定义通过 JSON/yaml 文件、JSON/yaml 字符串或数据数组验证给定的数据结构。

每个模式的基础

模式始终从一个对象开始,该对象必须具有属性和可选的可重用模板定义。

{
    "@type": "object",
    "@properties": {},
    "@templates": {}
}
字符串
{
    "@type": "object",
    "@properties": {
        "foo": {
            "@type": "string",
            "@required": true,
            "@allowed": "yes|no"
        }       
    }
}
整数/浮点数
{
    "@type": "object",
    "@properties": {
        "foo": {
            "@type": "integer",
            "@required": true,
            "@range": "0..100"
        },
        "bar": "float"  
    }
}
具有固定属性名称的对象

使用 @properties 为固定属性名称确保这些键使用值设置。

{
    "@type": "object",
    "@properties": {
        "foo": {
            "@type": "string"
        },
        "bar": {
            "@type": "integer",
            "@required": true
        }       
    }
}
具有可变属性名称的对象

使用 @property 为可变属性名称允许具有不同名称但相同属性模式定义的多个属性。

{
    "@type": "object",
    "@property": {
        "@type": "string",
        "@required": true
    }
}
属性数组

使用数组类型定义一个数组属性,该属性包含由 @property 中定义的模式指定的元素。

{
    "@type": "object",
    "@properties": {
        "foo": {
            "@type": "array",  
            "@property": {
                "@type": "object",
                "@properties": {
                    "foo": {
                        "@type": "string"
                    },
                    "bar": {
                        "@type": "integer",
                        "@required": true
                    } 
                }
            }   
        }
    }
}

模式属性验证器

模式定义可以使用此组件的验证器来验证模式属性值。

{
    "@type": "object",
    "@properties": {
        "foo": {
            "@type": "string",
            "@validator": "alphaNumeric"
        },
        "bar": {
            "@type": "string",
            "@validator": {
                "@name": "email"
            }   
        },
        "xxx": {
            "@type": "string",
            "@validator": {
                "@name": "betweenLength",
                "@constraints": {
                    "min": 3,
                    "max": 30
                }   
            }   
        }       
    }
}

模式属性模板

可以使用模板在不同的属性上重复使用。模板在模式根目录中定义,是一个包含多个名称模板的对象。

{
    "@type": "object",
    "@properties": {
        "books": {
            "@type": "array",
            "@property": {
                "@template": "book"  
            }   
        }   
    },
    "@templates": {
        "book": {
            "@type": "object",
            "@properties": {
                "name": {
                    "@type": "string",
                    "@required": true
                },
                "description": {
                    "@type": "string"
                },
                "summary": "string",
                "pages": "integer"
            }
        }
    }
}