serkin/volan
Composer 安装命令:
composer require serkin/volan
包简介
PHP array schema validator
关键字:
README 文档
README
For everyone who uses MongoDB or other NoSQL solution and cares about what client sends to his/her database and looking for validation library written in PHP. Volan validates array against given shema and provides you with full information about invalid nodes. Can be used with logging so you can see the whole validation process.
Installation
via Composer:
composer require serkin/volan dev-master
Usage
All you have to do is to specify _type field for each node. _type is a reference to a validation class
include 'vendor/autoload.php'; $schema = [ 'root' => [ // Schema must begins with 'root' 'title' => [ '_type' => 'required_string' ], 'price' => [ '_type' => 'number' ], 'author' => [ '_type' => 'string' ], 'instock' => [ '_type' => 'required_boolean' ], 'info' => [ '_type' => 'array', 'isbn' => [ '_type' => 'string' ], 'pages' => [ '_type' => 'number' ] ], 'comments' => [ '_type' => 'nested_array', 'user_id' => [ '_type' => 'required_number' ], 'comment' => [ '_type' => 'required_string' ] ] ] ]; $book = [ 'title' => 'The Idiot', // Cannot be omitted 'instock' => true, // Cannot be omitted and has to be bool type 'info' => ['isbn' => '978-0451531520'], // 'price' can be omitted but if present has to be numeric type 'comments' => [ // Nested array check nested elements [ 'user_id' => 1, 'comment' => 'Good book', // 'extra_field' => 'bad field' // By default if key not present in schema validation stops and returns false ], [ 'user_id' => 2, 'comment' => 'I like it' ] ] ]; $validator = new \Volan\Volan($schema); $result = $validator->validate($book); // if $result->isValid() === false you can get full information about invalid node var_dump($result->getErrorInfo());
Predefined validators
Strings
string: stringrequired_string: string that has to be present
Arrays
array: arrayrequired_array: array that has to be presentnested_array: array with nested arraysrequired_nested_array: array with nested arrays has to be present
Bool
boolean: booleanrequired_boolean: boolean that has to be present
Numbers
number: int or floatrequired_number: int or float that has to be present
Custom validators
If you need extra validators you can create them extending \Volan\Validator\AbstractValidator class
- Create folder
src/Volan/Validatorin your library - Add your custom validator
src/Volan/Validator/mongoid_validator.php. Example formongoidvalidator:
namespace Volan\Validator; class MongoidValidator extends AbstractValidator { public function isValid($nodeData) { return ($nodeData instanceof \MongoId); } }
- Add autoload to composer.json
"autoload": { "psr-4": { "Volan\\Validator\\": "src/Volan/Validator/" } }
Usage with other libraries
If you want to use other validation libraries with Volan it is easy. Let's take a look how it works with Respect validation engine
namespace Volan\Validator; use Respect\Validation\Validator as v; class IntBetween10And20Validator extends AbstractValidator { public function isValid($nodeData) { return v::int()->between(10, 20)->validate($nodeData); } }
Tips
Allow extra keys in data
If you want allow extra keys in array you can define it in constructor
$validator = new \Volan\Volan($schema, $strictMode = false);
Allow required fields be omitted
In mongoDB when you update just several fields in collection you cannot pass validation cause required fields may be missing. You can tell validator consider all required validation as optional.
$validator = new \Volan\Volan($schema); $validator->setRequiredMode(false); $result = $validator->validate($book);
Logging
If you want see validation process set logger
$validator = new \Volan\Volan($schema); $result = $validator->validate($book); $result->getLog();
PSR compatible class names
You can use PSR compatible names for validation classes. Previous example with mongoid validation class can be rewritten like:
namespace Volan\Validator; class MongoidValidator extends AbstractValidator { public function isValid($nodeData) { return ($nodeData instanceof \MongoId); } }
Here we changed mongoid_validator to MongoidValidator. Example with int_between_10_and_20_validator be rewritten to IntBetween10And20Validator
Relational structure
Let's imagine we have field in our book data
... 'categories' => [new \MongoId('111111111111111111111111'),new \MongoId('111111111111111111111112')] ...
and we want ensure that all elements not only instances of MongoId but actually present in our database. It is easy. Our validator will be look like: namespace Volan\Validator;
class ArrayOfMongoids extends AbstractValidator { public function isValid($nodeData) { foreach($nodeData as $id) { if($id !instanceof \MongoId) || !$this->presentInCategoryCollection($id)) return false; } } return true; } public function presentInCategoryCollection($id) { // Getting connection and so on $collection = $db->selectCollection('categories'); return (bool)$collection->findOne(['_id' => $id]); } }
now in schema we add
... 'categories' => ['_type' => 'array_of_mongoids'] ...
Dependencies
- PHP: >= 5.4
Contribution
Please see CONTRIBUTING for details.
Licence
The MIT License (MIT). Please see License File for more information.
Tests
phpunit Or with Docker
docker run --rm -v "$PWD":/var/src/ serkin/php7 vendor/bin/phpunit --debug Code style
docker run --rm -v "$PWD":/var/src/ serkin/php7 vendor/bin/php-cs-fixer fix src serkin/volan 适用场景与选型建议
serkin/volan 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 25.82k 次下载、GitHub Stars 达 44, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「validation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 serkin/volan 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 serkin/volan 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 serkin/volan 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Adds request-parameter validation to the SLIM 3.x PHP framework
A jQuery augmented PHP library for creating and validating HTML forms
A Laravel validator for delimiter-separated list of emails.
A CakePHP behavior to validate foreign keys
PHP Validator for stuff.
Stand-alone PHP Class for Data Sanitization and Validation
统计信息
- 总下载量: 25.82k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 44
- 点击次数: 7
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-04