graze / config-validation
验证并填充数组和对象
0.2
2018-08-06 14:18 UTC
Requires
- php: ^5.6 | ^7.0
- respect/validation: ^1.1
Requires (Dev)
- graze/standards: ^2.0
- phpunit/phpunit: ^5.7.21 | ^6 | ^7
- squizlabs/php_codesniffer: ^3.0
This package is auto-updated.
Last update: 2024-09-24 04:28:46 UTC
README
Config Validation使用简短语法检查输入与代码定义的方案是否匹配。
然后使用默认数据填充可选字段。
安装
通过Composer
composer require graze/config-validation
用法
长路径定义
use Respect\Validation\Validator as v; $validator = (new ArrayValidator()) ->required('key', v::stringType()) ->optional('parent.child', v::intVal(), 1) ->optional('parent.second', v::stringType()->date());
使用子元素
$validator = Validator::arr() ->required('key', v::stringType()) ->addChild('parent', Validator::arr() ->optional('child', v::intVal(), 1) ->optional('second', v::stringType()->date() );
使用您的验证器进行验证
function thing (array $input) { return $validator->validate($input); } thing(['key' => 'value']) // ['key' => 'value', 'parent' => ['child' => 1, 'second' => null]] thing(); // throws new ConfigValidationFailed thing(['key' => 'input', ['parent' => ['child' => 2]]) // ['key' => 'input', ['parent' => ['child' => 2, 'second' => null]] thing(['key' => 'input', ['parent' => ['second' => '111']]) // throws new ConfigValidationFailed('expected data for parent.second') thing(['key' => 'input', ['parent' => ['second' =>
使用变量键验证数组项
$childValidator = Validate::object() ->required('key->item', v::intVal()->min(2)->max(4)) ->optional('key->second', v::stringType(), 'name'); $validator = Validate::object() ->required('items', v::arrayVal()->each( $childValidator->getValidator() )); function thing ($input) { return $validator->validate($input); } thing((object) ['items' => [ (object) ['key' => 3], ]]); // (object) ['items' => [ // (object) ['key' => (object) ['item' => 3, 'second' => 'name']] // ]]
测试
make build test
贡献
请参阅CONTRIBUTING以获取详细信息。
安全性
如果您发现任何与安全相关的问题,请通过电子邮件security@graze.com而不是使用问题跟踪器来报告。
鸣谢
许可证
MIT许可证(MIT)。请参阅许可证文件获取更多信息。