sixlive/json-schema-assertions

v2.2.0 2024-08-28 15:30 UTC

This package is auto-updated.

Last update: 2024-08-28 15:30:32 UTC


README

Packagist Version Packagist Downloads StyleCI

PHP的JSON Schema断言。底层使用swaggest/php-json-schema

框架集成

安装

您可以通过composer安装此包

> composer require --dev sixlive/json-schema-assertions

使用方法

如果您正在使用外部模式引用,例如$ref: 'bar.json,您必须通过文件路径或使用配置路径解析来引用模式。

├── schemas
│   ├── bar.json
│   └── foo.json

您可以使用AssertsJsonSchema特质或手动构建模式断言。

use sixlive\JsonSchemaAssertions\Concerns\AssertJsonSchema;

class ExampleTest extends TestCase
{
    use AssertsJsonSchema;

    public function setUp()
    {
        parent::setUp();

        $this->setJsonSchemaBasePath(__DIR__.'/../Schemas');
    }

    /** @test */
    function it_has_a_valid_response()
    {
        $this->schemaAssertion
            ->schema('foo')
            ->assert('{"foo": "bar"}');
    }
}
/** @test */
public function it_has_a_valid_response()
{
    $schema = [
        'type' => 'object',
        'properties' => [
           'foo' => [
                'type' => 'string',
           ],
         ],
         'required' => [
            'foo',
        ],
    ];

    // Schema as an array
    (new SchemaAssertion)->schema($schema)->assert('{"foo": "bar"}');

    // Schema from raw JSON
    (new SchemaAssertion)->schema(json_encode($schema))->assert('{"foo": "bar"}');

    // Schema from a file
    (new SchemaAssertion)->schema(__DIR__.'/../schemas/foo.json'))
        ->assert('{"foo": "bar"}');

    // Remote schema
    (new SchemaAssertion)->schema('https://docs.foo.io/schemas/foo.json')
        ->assert('{"foo": "bar"}')

    // Schema from a path
    (new SchemaAssertion(__DIR__.'/../schemas/'))
        ->schema('foo')
        ->assert('{"foo": "bar"}');
}

测试

> composer test

变更日志

有关最近更改的更多信息,请参阅CHANGELOG

贡献

有关详细信息,请参阅CONTRIBUTING

代码风格

除了php-cs-fixer规则外,StyleCI还会应用Laravel预设

代码检查

> composer styles:lint

修复

> composer styles:fix

安全性

如果您发现任何与安全性相关的问题,请通过电子邮件oss@tjmiller.co联系,而不是使用问题跟踪器。

致谢

许可协议

MIT许可(MIT)。有关更多信息,请参阅许可文件