Skip to content

Commit

Permalink
Merge pull request #19 from zfegg/readme
Browse files Browse the repository at this point in the history
Update doc
  • Loading branch information
Moln committed May 24, 2023
2 parents 026ec7d + 5e748e3 commit 33d437e
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 10 deletions.
141 changes: 141 additions & 0 deletions README-zh.md
@@ -0,0 +1,141 @@
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记录是否存在
- [`DoctrineRecordExistsFilter`](src/Opis/Filter/DoctrineRecordExistsFilter.php): 使用 `doctrine/orm` 验证DB记录是否存在
- [`RecordExistsFilter`](src/Opis/Filter/RecordExistsFilter.php): 使用 `PDO` 验证DB记录是否存在
27 changes: 17 additions & 10 deletions README.md
@@ -1,18 +1,17 @@
PSR-15 Content Validation / PSR-15 内容验证器
PSR-15 Content Validation
===========================

[简体中文](README-zh.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)

Content validation for PSR-15 middleware.
Based on [`opis/json-schema`](https://packagist.org/packages/opis/json-schema).


基于 PSR-15 内容验证中间件。
内容验证使用 [`opis/json-schema`](https://packagist.org/packages/opis/json-schema).

Installation / 安装使用
Installation
-----------------------

Install via composer.
Expand All @@ -21,7 +20,7 @@ Install via composer.
composer require zfegg/content-validation
```

Usage / 使用
Usage
--------------

### `Opis\JsonSchema\Validator` factory config.
Expand Down Expand Up @@ -62,7 +61,7 @@ return [

### Mezzio

Add `ConfigProvider` in 'config.php'. / 在 `config.php` 中添加 `ConfigProvider`.
Add `ConfigProvider` in 'config.php'.

```php

Expand Down Expand Up @@ -102,7 +101,7 @@ $app->post(
;
```

Invalid request will response status 422. / 无效请求将响应 422状态码.
Invalid request will response status 422.

```shell
curl "http://host/api/users" -d 'username=foo'
Expand Down Expand Up @@ -133,4 +132,12 @@ $app->post(
->add(\Zfegg\ContentValidation\ContentValidationMiddleware::class)
->setArgument('schema', 'path-to-json-schema.json')
;
```
```


Validators
----------

- [`DbalRecordExistsFilter`](src/Opis/Filter/DbalRecordExistsFilter.php) : Use `doctrine/dbal` to check record exists.
- [`DoctrineRecordExistsFilter`](src/Opis/Filter/DoctrineRecordExistsFilter.php) : Use `doctrine/orm` to check record exists.
- [`RecordExistsFilter`](src/Opis/Filter/RecordExistsFilter.php) : Use `PDO` to check record exists.

0 comments on commit 33d437e

Please sign in to comment.