simplecomplex / validate
浅层和递归验证(对象/数组的元素)。
2.5.1
2019-07-08 10:55 UTC
Requires
- php: >=7.0
- simplecomplex/utils: ^1.11 || ^2.0 || dev-develop
Suggests
- simplecomplex/inspect: Great for logging; better variable dumps and traces.
- dev-master
- 2.5.1
- 2.5
- 2.4.3
- 2.4.2
- 2.4.1
- 2.4
- 2.3.1
- 2.3
- 2.2
- 2.1
- 2.0.1
- 2.0
- 1.0.2
- 1.0.1
- 1.0
- 0.9
- dev-rename-ruleset-for-recursive-2
- dev-feature/rename-ruleset-for-recursive
- dev-feature/split-recursive-vs-checked
- dev-feature/interface-class-renaming
- dev-feature/interface-naming-todo
- dev-feature/stdClass-get_class-not-instanceof
- dev-dev-3.x
- dev-abandoned/multiple-enums-by-type
- dev-develop
This package is auto-updated.
Last update: 2024-09-18 18:42:43 UTC
README
几乎验证任何类型的PHP数据结构。
简单的浅层验证
测试一个变量是否与Validate的60多个规则方法之一匹配
- 它是否为特定类型或类?
- 它的值是否匹配一个模式?
- 它是空的吗?
- 是一个容器(数组|对象|可遍历的)?
- 是一个数值索引数组?
测试一组规则
验证规则集是一系列规则(Validate方法)
– 实际上,您可以将上述所有问题合并为一个单一问题。
您还可以声明“如果它不是一个[对象|string| whatever],则null|false|0将完全适用”。
递归验证对象/数组
验证规则集可以嵌套 – 无限。
$validate->challenge($subject, $ruleSet)根据规则集(+后代规则集)遍历主题,
并检查主题在任一级别/位置是否符合该级别/位置的规则。
记录验证失败的原因
$validate->challengeRecording($subject, $ruleSet)记录所有错误,
并返回违规列表(包括检测位置和错误内容)。
使用示例
SimpleComplex Http使用验证规则集来检查HTTP(REST)响应的主体。
规则集在JSON中定义(可读和可移植),并作为PHP ValidationRuleSet缓存(使用SimpleComplex Cache)。
非规则标志
多维对象/数组规则集支持比正确的验证规则更多。
- 可选:桶允许缺失
- allowNull:允许值为null
- alternativeEnum:应使主题通过的一组替代(标量|null)值列表,
即使违反了另一条规则 - tableElements:主题(对象|数组)必须包含这些键,
每个键都有一个子规则集 - listItems:主题(对象|数组)必须是一个由多个桶组成的列表,
它们都具有相同类型/模式/结构
alternativeEnum非常适合定义“桶必须是对象或null”。
tableElements和listItems可以共存。
如果您有一个 – 类似于形状不良的 – 可能包含列表项以及非列表元素的对象,则相关。
来自XML的数据可能很容易具有这种结构(XML很糟糕 ;-)
tableElements子标志
- (obj|arr) rulesByElements (必需):键的规则集表;
对于每个键都有一个规则集 - (bool) exclusive:对象或数组必须只包含由
rulesByElements指定的键 - (arr) whitelist:对象或数组 – 除了
rulesByElements之外 – 必须只包含这些键 - (arr) blacklist:对象或数组不得包含这些键
listItems子标志
- (obj|arr) listItems (必需):每个列表项必须遵守的规则集
- (int) minOccur:必须至少有那么多列表项
- (int) maxOccur:猜猜
示例
// We wanna see some bicycles... class Bicycle { public $wheels = 0; public $saddle; public $sound = ''; public $accessories = []; public $various; public function __construct($wheels, $saddle, $sound, $accessories, $various) { $this->wheels = $wheels; $this->saddle = $saddle; $this->sound = $sound; $this->accessories = $accessories; $this->various = $various; } } // Not just any kind of bicycles. They must comply to this rule set: $rule_set = [ 'class' => [ 'Bicycle' ], 'tableElements' => [ //'exclusive' => true, //'whitelist' => ['unspecified_1'], //'blacklist' => ['saddle', 'accessories'], 'rulesByElements' => [ 'wheels' => [ 'integer', 'range' => [ 1, 3 ] ], 'saddle' => [ 'integer', 'alternativeEnum' => [ // Wrongly defined as nested; confused by enum which // formally requires to be nested. // But ValidationRuleSet fixes this, as it does with // the opposite for (un-nested) enum. [ true, ] ] ], 'sound' => [ 'string' => true, 'enum' => [ // Counterintuitive nesting, but formally correct because // the allowed values array is second arg to the enum() // method. // ValidationRuleSet fixes un-nested instance. [ 'silent', 'swooshy', 'clattering', ] ] ], 'accessories' => [ 'array', 'tableElements' => [ 'rulesByElements' => [ 'luggageCarrier' => [ 'boolean', 'optional' ], 'lights' => [ 'numeric', 'optional' ] ] ] ], 'various' => [ 'array', 'optional', // allowNull and alternativeEnum with null // are two ways of allowing null. 'allowNull' => true, 'alternativeEnum' => [ null, ], 'listItems' => [ 'maxOccur' => 5, 'itemRules' => [ 'string', 'alternativeEnum' => [ // Correctly defined as un-nested array. true, false, ] ] ] ] ] ] ];
要求
- PHP >=7.0
- SimpleComplex 工具类