mcvalidator / mcvalidator-php
PHP 中总是会出现 [未定义索引] 的问题
v0.0.12-alpha
2018-05-03 22:27 UTC
Requires
- heterogeny/heterogeny-php: >=0.0.0 <1.0.0
- jeremeamia/superclosure: ^2.4
- symfony/yaml: ^4.0
Requires (Dev)
- phpunit/phpunit: ^7.1
This package is auto-updated.
Last update: 2024-09-28 08:53:55 UTC
README
PHP 中总是会出现 [未定义索引] 的问题。
McValidator 旨在提供一种验证和清理数据的方法
示例 1
use McValidator as MV; // Builder is important because we can chain validator with them without // too much work. $builder = MV\valid('rule/is-string'); // Build the pipe $pipe = $builder->build(); // Pump the value through the pipe // Result contains the value and also informations about // the runtime, such as errors and messages $result = $pipe->pump(10); // Gets the runtime state $state = $result->getState(); // Gets the message of the head(first item) of errors echo $state->getErrors()->head()->getMessage(); // Value is not a string // We need more! $builder2 = MV\shape_of([ 'a' => $builder ]); $pipe2 = $builder2->build(); $result2 = $pipe2->pump(dict([ 'a' => 10 ])); // Gets the runtime state $state2 = $result2->getState(); // Gets the message of the head(first item) of errors echo $state2->getErrors()->head()->getMessage(); // outputs `Value is not a string` // Gets the field path of the error! echo $state2->getErrors()->head()->getStringPath('/' // <-- separator); // outputs `$/a` // `$` means root of path
你现在可能会想:这堆垃圾太烦人了,比 j**a 的代码冗长得多。我们知道,McValidator 并不旨在“简单”和“不冗长”,而是旨在提供方法来
- 编写验证器,并提供运行时信息
- 跟踪错误,而不会让你因为从某处抛出异常而不知所措
- 运行时不会抛出任何异常,除非结构配置错误
- 值历史,是的,每个值都有一个值历史,这个历史会在每次更改时更新,目前处于开发中
- 结构序列化,这有助于缓存,我们也可以缓存闭包,为什么不行呢?这是 PHP。这也很好,因为构建验证器结构可能很昂贵,如果缓存验证器,你可以在每次请求时使用它而无需重新构建(这是 PHP)。
- 一次编写,到处验证,这个库的主要目的是提供一个编写验证器并在 JavaScript(web、node)、Python、Ruby 等语言中使用它的方法。我们正在编写一份宣言,以定义 McValidator 的实现模式。
- 我们支持 YAML,你可以仅使用 YAML 来构建你的验证器,这将使你更高效,因为你可以编写一个 YAML 文件,它将在 McValidator 的任何其他实现中都是有效的。
- 我们尊重数据结构,McValidator 与 Heterogeny 一起在 PHP 上工作,因此尊重数据应该是怎样的,数组就是数组,字典就是字典,这样至少在 PHP 上可以减少混淆。 我们永远不会支持任何混合这些结构的方法,不要为此创建问题或 PR。
- 你可以在地狱的深度进行验证,McValidator 提供了一种跟踪嵌套结构中错误的方法,当我们说“深”时,我们是指真正的“深”。一个错误路径的例子:
a/0/b/1/c
,这意味着一个错误(发生在字段c
,元素1
的字段b
,元素0
的字段a
),这是可能的,因为 McValidator 是关于链和递归的,我们的代码中有硬核或类似的东西。 - 不可变性是我们的重点,清理或验证不会改变它无法触及的数据
值类型表
NonExistentValue
:未发送且不存在的数据,例如:为形状配置但未“注入”的键ExplicitNonExistentValue
:显式发送为null
的值,例如:为形状配置但“注入”为null
的键InvalidValue
:无效的值,其来源是部分,例如:在部分中发送并报告为无效的值
目前无法保证性能。