Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zfegg/content-validation
Content validation Middleware
https://github.com/zfegg/content-validation
json-schema php validation
Last synced: about 2 months ago
JSON representation
Content validation Middleware
- Host: GitHub
- URL: https://github.com/zfegg/content-validation
- Owner: zfegg
- License: mit
- Created: 2016-10-11T11:06:53.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2024-08-22T06:11:30.000Z (4 months ago)
- Last Synced: 2024-09-23T08:18:57.378Z (3 months ago)
- Topics: json-schema, php, validation
- Language: PHP
- Homepage:
- Size: 101 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README-zh.md
- License: LICENSE
Awesome Lists containing this project
README
PSR-15 内容验证器
===========================[English](README.md)
[![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)
[![Coverage Status](https://coveralls.io/repos/github/zfegg/content-validation/badge.svg?branch=master)](https://coveralls.io/github/zfegg/content-validation?branch=master)
[![Latest Stable Version](https://poser.pugx.org/zfegg/content-validation/v/stable.png)](https://packagist.org/packages/zfegg/content-validation)基于 PSR-15 内容验证中间件。
内容验证使用 [`opis/json-schema`](https://packagist.org/packages/opis/json-schema).安装使用
---------用 composer 安装.
```bash
composer require zfegg/content-validation
```使用
-----### `Opis\JsonSchema\Validator` 工厂配置
```php
// config.php
return [
Opis\JsonSchema\Validator::class => [
'resolvers' => [
'protocolDir' => [
// foo-schema://host/foo.create.json => schema/dir/foo.create.json
['foo-schema', 'host', 'schema/dir'],
],
'protocol' => [
],
'prefix' => [
['prefix1', 'path/to/dir'],
['prefix2', 'path/to/dir'],
],
'file' => [
['SchemaFoo', 'path/to/file'],
['SchemaBar', 'path/to/file2'],
],
'raw' => [
['{"type":"object", ...}', 'schema id 1'],
['{"type":"object", ...}', 'schema id 2'],
]
],
'filters' => [
'foo-filter' => ['filter' => 'FilterFilterName', 'types' => ['integer']],
],
'filtersNS' => [
'foo-ns' => 'FilterResolverName',
],
]
]
```### Mezzio
在 `config.php` 中添加 `ConfigProvider`.
```php
$aggregator = new ConfigAggregator(
[
// ...
\Zfegg\ContentValidation\ConfigProvider::class,
]
);return $aggregator->getMergedConfig();
``````php
$app->post(
'/api/users',
[
\Zfegg\ContentValidation\ContentValidationMiddleware::class,
function (\Psr\Http\Message\ServerRequestInterface $request) {
$data = $request->getParsedBody(); // Get valid data.
}
], 'api.users.create')
->setOptions(['schema' => 'path-to-json-schema.json'])
//->setOptions([
// // or set json-schema object.
// 'schema' => (object) [
// 'type' => 'object',
// 'properties' => (object) [
// 'age' => (object) [
// 'type' => 'integer'
// ]
// ],
// 'required' => ['age']
// ]
// ])
;
```无效请求将响应 422状态码.
```shell
curl "http://host/api/users" -d 'username=foo'HTTP/1.1 422
{
"status": 422,
"detail": "Failed Validation",
"validation_messages": {
"age": [
"The required properties (age) are missing"
]
}
}
```### Slim
```php
$app->post(
'/api/users',
function (\Psr\Http\Message\ServerRequestInterface $request) {
$data = $request->getParsedBody(); // Get valid data.
}
)
->add(\Zfegg\ContentValidation\ContentValidationMiddleware::class)
->setArgument('schema', 'path-to-json-schema.json')
;
```验证器
--------- [`DbalRecordExistsFilter`](src/Opis/Filter/DbalRecordExistsFilter.php): 使用 `doctrine/dbal` 验证DB记录是否存在.
json-schema `$filters`配置:
```json5
{
"$func": "dbal-exists",
"$vars": {
"db": "db", // IoC容器中的 DBAL 对象.
"sql": "select ...", // 自定义 SQL
"table": "foo", // 表名称
"field": "key", // 字段名称
"exists": true // 检查记录存在或不存在. 默认: false
}
}
```
- [`DoctrineRecordExistsFilter`](src/Opis/Filter/DoctrineRecordExistsFilter.php): 使用 `doctrine/orm` 验证DB记录是否存在。
json-schema `$filters`配置:
```json5
{
"$func": "orm-exists",
"$vars": {
"db": "orm.default", // IoC容器中的 ORM 对象.
"dql": "select ...", // 自定义 DQL
"entity": "Foo", // 实体名称
"field": "key", // 字段名称
"exists": true // 检查记录存在或不存在. 默认: false
}
}
```
- [`RecordExistsFilter`](src/Opis/Filter/RecordExistsFilter.php): 使用 `PDO` 验证DB记录是否存在。
json-schema `$filters`配置:
```json5
{
"$func": "db-exists",
"$vars": {
"db": "db", // IoC容器中的 PDO 对象.
"sql": "select ...", // 自定义 SQL
"table": "foo", // 表名称
"field": "key", // 字段名称
"exists": true // 检查记录存在或不存在. 默认: false
}
}
```