{"id":13623955,"url":"https://github.com/serkin/Volan","last_synced_at":"2025-04-15T20:32:55.066Z","repository":{"id":31477408,"uuid":"35041507","full_name":"serkin/volan","owner":"serkin","description":"Light and extendable schema validation library","archived":false,"fork":false,"pushed_at":"2018-06-19T09:43:43.000Z","size":115,"stargazers_count":44,"open_issues_count":0,"forks_count":9,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-10-31T08:25:46.197Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"hstcscolor/awesome-erlang-cn","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/serkin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-05-04T15:05:20.000Z","updated_at":"2024-10-06T20:50:30.000Z","dependencies_parsed_at":"2022-09-03T16:40:56.826Z","dependency_job_id":null,"html_url":"https://github.com/serkin/volan","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serkin%2Fvolan","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serkin%2Fvolan/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serkin%2Fvolan/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serkin%2Fvolan/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/serkin","download_url":"https://codeload.github.com/serkin/volan/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223684894,"owners_count":17185737,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T21:01:37.329Z","updated_at":"2024-11-08T12:30:36.557Z","avatar_url":"https://github.com/serkin.png","language":"PHP","readme":"# Light PHP validation library\nFor 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. \n`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.\n\n[![Build Status](https://img.shields.io/travis/serkin/volan.svg?style=flat-square)](https://travis-ci.org/serkin/parser)\n[![Coverage Status](https://img.shields.io/coveralls/serkin/volan/master.svg?style=flat-square)](https://coveralls.io/r/serkin/volan?branch=master)\n[![Scrutinizer Code Quality](https://img.shields.io/scrutinizer/g/serkin/volan.svg?style=flat-square)](https://scrutinizer-ci.com/g/serkin/volan/?branch=master)\n[![Latest Stable Version](https://poser.pugx.org/serkin/volan/v/stable)](https://packagist.org/packages/serkin/volan)\n[![Total Downloads](https://poser.pugx.org/serkin/volan/downloads)](https://packagist.org/packages/serkin/volan)\n[![Latest Unstable Version](https://poser.pugx.org/serkin/volan/v/unstable)](https://packagist.org/packages/serkin/volan)\n[![License](https://poser.pugx.org/serkin/volan/license)](https://packagist.org/packages/serkin/volan)\n[![SensioLabsInsight](https://insight.sensiolabs.com/projects/c15e651a-4e91-44b3-8b8f-393a0a4ac70b/small.png)](https://insight.sensiolabs.com/projects/c15e651a-4e91-44b3-8b8f-393a0a4ac70b)\n\n- [Volan](#light-php-validation-library)\n\t- [Installation](#installation)\n\t- [Usage](#usage)\n\t- [Predefined validatorse](#predefined-validators)\n\t- [Custom validators](#custom-validators)\n\t- [Usage with other libraries](#usage-with-other-libraries)\n\t- [Tips](#tips)\n\t    - [Allow extra keys in data](#allow-extra-keys-in-data)\n\t    - [Allow required fields be omitted](#allow-required-fields-be-omitted)\n\t    - [Logging](#logging)\n\t    - [PSR compatible class names](#psr-compatible-class-names)\n\t    - [Relational structure](#relational-structure)\n\t- [Dependencies](#dependencies)\n\t- [Contribution](#contribution)\n\t- [Licence](#licence)\n\t- [Tests](#tests)\n\n## Installation\n---\nvia Composer:\n\n``` bash\ncomposer require serkin/volan dev-master\n```\n\n## Usage\n---\nAll you have to do is to specify `_type` field for each node. `_type` is a reference to a validation class\n\n```php\ninclude 'vendor/autoload.php';\n\n$schema = [\n    'root' =\u003e [ // Schema must begins with 'root'\n        'title' =\u003e [\n            '_type' =\u003e 'required_string'\n        ],\n        'price' =\u003e [\n            '_type' =\u003e 'number'\n        ],\n        'author' =\u003e [\n            '_type' =\u003e 'string'\n        ],\n        'instock' =\u003e [\n            '_type' =\u003e 'required_boolean'\n        ],\n        'info' =\u003e  [\n            '_type' =\u003e 'array',\n            'isbn' =\u003e [\n                '_type' =\u003e 'string'\n            ],\n            'pages' =\u003e [\n                '_type' =\u003e 'number'\n            ]\n        ],\n        'comments' =\u003e [\n            '_type' =\u003e 'nested_array',\n            'user_id' =\u003e [\n                '_type' =\u003e 'required_number'\n            ],\n            'comment' =\u003e [\n                '_type' =\u003e 'required_string'\n            ]\n        ]\n    ]\n];\n\n$book = [\n    'title' =\u003e 'The Idiot', // Cannot be omitted\n    'instock' =\u003e true, // Cannot be omitted and has to be bool type\n    'info' =\u003e ['isbn' =\u003e '978-0451531520'],\n    //  'price' can be omitted but if present has to be numeric type \n    'comments' =\u003e [ // Nested array check nested elements\n        [\n            'user_id' =\u003e 1,\n            'comment' =\u003e 'Good book',\n            // 'extra_field' =\u003e 'bad field' \n            // By default if key not present in schema validation stops and returns false \n        ],\n        [\n            'user_id' =\u003e 2,\n            'comment' =\u003e 'I like it'\n        ]\n    ]\n];\n\n$validator = new \\Volan\\Volan($schema);\n$result = $validator-\u003evalidate($book);\n\n// if $result-\u003eisValid() === false you can get full information about invalid node\nvar_dump($result-\u003egetErrorInfo());\n```\n## Predefined validators\n### Strings\n* `string`: string\n* `required_string`: string that has to be present\n\n### Arrays\n* `array`: array\n* `required_array`: array that has to be present\n* `nested_array`: array with nested arrays\n* `required_nested_array`: array with nested arrays has to be present\n\n### Bool\n* `boolean`: boolean\n* `required_boolean`: boolean that has to be present\n\n### Numbers\n* `number`: int or float\n* `required_number`: int or float that has to be present\n\n## Custom validators\nIf you need extra validators you can create them extending `\\Volan\\Validator\\AbstractValidator` class\n* Create folder `src/Volan/Validator` in your library\n* Add your custom validator `src/Volan/Validator/mongoid_validator.php`. Example for `mongoid` validator:\n``` php\nnamespace Volan\\Validator;\nclass MongoidValidator extends AbstractValidator\n{\n    public function isValid($nodeData)\n    {\n        return ($nodeData instanceof \\MongoId);\n    }\n}\n```\n* Add autoload to composer.json\n``` json\n\"autoload\": {\n        \"psr-4\": {\n            \"Volan\\\\Validator\\\\\": \"src/Volan/Validator/\"\n        }\n    }\n```\n\n## Usage with other libraries\nIf you want to use other validation libraries with `Volan` it is easy. Let's take a look how it works with [Respect validation engine](https://github.com/Respect/Validation) \n``` php\nnamespace Volan\\Validator;\nuse Respect\\Validation\\Validator as v;\n\nclass IntBetween10And20Validator extends AbstractValidator\n{\n    public function isValid($nodeData)\n    {\n        return v::int()-\u003ebetween(10, 20)-\u003evalidate($nodeData);\n        \n    }\n}\n```\n\n## Tips\n### Allow extra keys in data\nIf you want allow extra keys in array you can define it in constructor\n``` php\n$validator = new \\Volan\\Volan($schema, $strictMode = false);\n```\n\n### Allow `required` fields be omitted\nIn mongoDB when you update just several fields in collection you cannot pass validation cause required fields may be missing.\nYou can tell validator consider all required validation as optional.\n\n``` php\n$validator = new \\Volan\\Volan($schema);\n$validator-\u003esetRequiredMode(false);\n$result = $validator-\u003evalidate($book);\n```\n### Logging\nIf you want see validation process set logger\n``` php\n\n\n$validator = new \\Volan\\Volan($schema);\n\n$result = $validator-\u003evalidate($book);\n$result-\u003egetLog();\n\n```\n\n### PSR compatible class names\n\nYou can use PSR compatible names for validation classes.\nPrevious example with `mongoid` validation class can be rewritten like:\n``` php\nnamespace Volan\\Validator;\n\nclass MongoidValidator extends AbstractValidator\n{\n    public function isValid($nodeData)\n    {\n        return ($nodeData instanceof \\MongoId);\n    }\n}\n```\nHere we changed `mongoid_validator` to `MongoidValidator`.\nExample with `int_between_10_and_20_validator` be rewritten to `IntBetween10And20Validator`\n\n### Relational structure\nLet's imagine we have field in our book data\n``` php\n...\n'categories' =\u003e [new \\MongoId('111111111111111111111111'),new \\MongoId('111111111111111111111112')]\n...\n```\nand we want ensure that all elements not only instances of `MongoId` but actually present in our database.\nIt is easy. Our validator will be look like:\nnamespace Volan\\Validator;\n``` php\nclass ArrayOfMongoids extends AbstractValidator\n{\n    public function isValid($nodeData)\n    {\n        foreach($nodeData as $id) {\n            if($id !instanceof \\MongoId) || !$this-\u003epresentInCategoryCollection($id))\n                return false;\n            }\n        }\n        \n        return true;\n    }\n    \n    public function presentInCategoryCollection($id)\n    {\n        // Getting connection and so on\n\n        $collection = $db-\u003eselectCollection('categories');\n        return (bool)$collection-\u003efindOne(['_id' =\u003e $id]);\n    }\n    \n}\n```\nnow in schema we add \n``` php\n...\n'categories' =\u003e ['_type' =\u003e 'array_of_mongoids']\n...\n```\n\n## Dependencies\n* PHP: \u003e= 5.4\n\n## Contribution\nPlease see [CONTRIBUTING](CONTRIBUTING.md) for details.\n\n## Licence\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n\n## Tests\n\n    phpunit\n\n\nOr with Docker\n\n    docker run --rm -v \"$PWD\":/var/src/ serkin/php7 vendor/bin/phpunit --debug\n\n\nCode style\n\n    docker run --rm -v \"$PWD\":/var/src/ serkin/php7 vendor/bin/php-cs-fixer fix src\n","funding_links":[],"categories":["目录","Table of Contents","PHP","过滤和验证 Filtering and Validation"],"sub_categories":["过滤和验证 Filtering and Validation","Filtering and Validation","Filtering, Sanitizing and Validation"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fserkin%2FVolan","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fserkin%2FVolan","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fserkin%2FVolan/lists"}