{"id":19300007,"url":"https://github.com/zfegg/content-validation","last_synced_at":"2026-03-07T14:01:57.126Z","repository":{"id":43002581,"uuid":"70584990","full_name":"zfegg/content-validation","owner":"zfegg","description":"Content validation Middleware ","archived":false,"fork":false,"pushed_at":"2024-08-22T06:11:30.000Z","size":103,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-10-19T02:55:42.434Z","etag":null,"topics":["json-schema","php","validation"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zfegg.png","metadata":{"files":{"readme":"README-zh.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2016-10-11T11:06:53.000Z","updated_at":"2024-08-22T06:07:12.000Z","dependencies_parsed_at":"2025-04-23T05:00:24.838Z","dependency_job_id":null,"html_url":"https://github.com/zfegg/content-validation","commit_stats":{"total_commits":58,"total_committers":3,"mean_commits":"19.333333333333332","dds":"0.18965517241379315","last_synced_commit":"026ec7d4e32d0b5a26c620560b419345dbbdb16b"},"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"purl":"pkg:github/zfegg/content-validation","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zfegg%2Fcontent-validation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zfegg%2Fcontent-validation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zfegg%2Fcontent-validation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zfegg%2Fcontent-validation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zfegg","download_url":"https://codeload.github.com/zfegg/content-validation/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zfegg%2Fcontent-validation/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30216492,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-07T13:35:13.914Z","status":"ssl_error","status_checked_at":"2026-03-07T13:35:13.569Z","response_time":53,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["json-schema","php","validation"],"created_at":"2024-11-09T23:13:24.472Z","updated_at":"2026-03-07T14:01:57.107Z","avatar_url":"https://github.com/zfegg.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"PSR-15 内容验证器\n===========================\n\n[English](README.md)\n\n[![GitHub Actions: Run tests](https://github.com/zfegg/content-validation/workflows/qa/badge.svg)](https://github.com/zfegg/content-validation/actions?query=workflow%3A%22qa%22)\n[![Coverage Status](https://coveralls.io/repos/github/zfegg/content-validation/badge.svg?branch=master)](https://coveralls.io/github/zfegg/content-validation?branch=master)\n[![Latest Stable Version](https://poser.pugx.org/zfegg/content-validation/v/stable.png)](https://packagist.org/packages/zfegg/content-validation)\n\n基于 PSR-15 内容验证中间件。\n内容验证使用 [`opis/json-schema`](https://packagist.org/packages/opis/json-schema).\n\n安装使用\n---------\n\n用 composer 安装.\n\n```bash\ncomposer require zfegg/content-validation\n```\n\n使用\n-----\n\n### `Opis\\JsonSchema\\Validator` 工厂配置\n\n```php\n// config.php\nreturn [\n    Opis\\JsonSchema\\Validator::class =\u003e [\n        'resolvers' =\u003e [\n            'protocolDir' =\u003e [\n                // foo-schema://host/foo.create.json =\u003e schema/dir/foo.create.json\n                ['foo-schema', 'host',  'schema/dir'],\n            ],\n            'protocol' =\u003e [\n            ],\n            'prefix' =\u003e [\n               ['prefix1', 'path/to/dir'],\n               ['prefix2', 'path/to/dir'],\n            ],\n            'file' =\u003e [\n               ['SchemaFoo', 'path/to/file'],\n               ['SchemaBar', 'path/to/file2'],\n            ],\n            'raw' =\u003e [\n               ['{\"type\":\"object\", ...}', 'schema id 1'],\n               ['{\"type\":\"object\", ...}', 'schema id 2'],\n            ]\n        ],\n        'filters' =\u003e [\n            'foo-filter' =\u003e ['filter' =\u003e 'FilterFilterName', 'types' =\u003e ['integer']],\n        ],\n        'filtersNS' =\u003e [\n            'foo-ns' =\u003e 'FilterResolverName',\n        ],\n    ]\n]\n```\n\n### Mezzio\n\n在 `config.php` 中添加 `ConfigProvider`.\n\n```php\n\n$aggregator = new ConfigAggregator(\n  [\n    // ...\n    \\Zfegg\\ContentValidation\\ConfigProvider::class,\n  ]\n);\n\nreturn $aggregator-\u003egetMergedConfig();\n```\n\n\n```php\n$app-\u003epost(\n  '/api/users', \n   [\n   \\Zfegg\\ContentValidation\\ContentValidationMiddleware::class,\n    function (\\Psr\\Http\\Message\\ServerRequestInterface $request) {\n        $data = $request-\u003egetParsedBody(); // Get valid data.\n    }\n], 'api.users.create')\n-\u003esetOptions(['schema' =\u003e 'path-to-json-schema.json'])\n//-\u003esetOptions([  \n//   // or set json-schema object. \n//  'schema' =\u003e (object) [\n//        'type' =\u003e 'object',\n//        'properties' =\u003e (object) [\n//             'age' =\u003e (object) [\n//                 'type' =\u003e 'integer'\n//              ]\n//        ],\n//        'required' =\u003e ['age']\n//   ]\n// ])\n;\n```\n\n无效请求将响应 422状态码.\n\n```shell\ncurl \"http://host/api/users\" -d 'username=foo'\n\nHTTP/1.1 422\n\n{\n  \"status\": 422,\n  \"detail\": \"Failed Validation\",\n  \"validation_messages\": {\n    \"age\": [\n      \"The required properties (age) are missing\"\n    ]\n  }\n}\n```\n\n\n### Slim \n\n```php\n$app-\u003epost(\n  '/api/users', \n  function (\\Psr\\Http\\Message\\ServerRequestInterface $request) {\n        $data = $request-\u003egetParsedBody(); // Get valid data.\n  }\n)\n-\u003eadd(\\Zfegg\\ContentValidation\\ContentValidationMiddleware::class)\n-\u003esetArgument('schema', 'path-to-json-schema.json')\n;\n```\n\n验证器\n--------\n\n- [`DbalRecordExistsFilter`](src/Opis/Filter/DbalRecordExistsFilter.php): 使用 `doctrine/dbal` 验证DB记录是否存在.  \n   json-schema `$filters`配置:\n  ```json5\n  {\n      \"$func\": \"dbal-exists\",\n      \"$vars\": {\n        \"db\": \"db\",          // IoC容器中的 DBAL 对象.\n        \"sql\": \"select ...\", // 自定义 SQL\n        \"table\": \"foo\",      // 表名称\n        \"field\": \"key\",      // 字段名称\n        \"exists\": true       // 检查记录存在或不存在. 默认: false\n      }\n  }\n  ```\n- [`DoctrineRecordExistsFilter`](src/Opis/Filter/DoctrineRecordExistsFilter.php): 使用 `doctrine/orm` 验证DB记录是否存在。  \n  json-schema `$filters`配置:\n  ```json5\n  {\n      \"$func\": \"orm-exists\",\n      \"$vars\": {\n        \"db\": \"orm.default\",   // IoC容器中的 ORM 对象.\n        \"dql\": \"select ...\",   // 自定义 DQL\n        \"entity\": \"Foo\",       // 实体名称\n        \"field\": \"key\",        // 字段名称\n        \"exists\": true         // 检查记录存在或不存在. 默认: false\n      }\n  }\n  ```\n- [`RecordExistsFilter`](src/Opis/Filter/RecordExistsFilter.php): 使用 `PDO` 验证DB记录是否存在。  \n  json-schema `$filters`配置:\n  ```json5\n  {\n      \"$func\": \"db-exists\",\n      \"$vars\": {\n        \"db\": \"db\",          // IoC容器中的 PDO 对象.\n        \"sql\": \"select ...\", // 自定义 SQL\n        \"table\": \"foo\",      // 表名称\n        \"field\": \"key\",      // 字段名称\n        \"exists\": true       // 检查记录存在或不存在. 默认: false\n      }\n  }\n  ```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzfegg%2Fcontent-validation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzfegg%2Fcontent-validation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzfegg%2Fcontent-validation/lists"}