Skip to content

sixlive/laravel-json-schema-assertions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

57141a0 · Feb 21, 2025

History

35 Commits
Feb 21, 2025
Nov 15, 2021
Jul 22, 2018
Apr 28, 2020
Sep 2, 2024
Jul 17, 2018
Jul 17, 2018
Sep 2, 2024
Nov 15, 2021
Jul 17, 2018
Jul 17, 2018
Apr 28, 2020
Jul 17, 2018
Jul 17, 2018
Dec 17, 2020
Feb 21, 2025
Sep 2, 2024

Repository files navigation

Laravel JSON Schema Assertions

Packagist Version Packagist Downloads StyleCI

JSON Schema schema assertions for Laravel test responses. Uses swaggest/php-json-schema under the hood.

Installation

You can install the package via composer:

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

This package uses Laravel's package discovery to register the service provider to the framework. If you are using an older version of Laravel or do not use package discovery see below.

Provider registration
// config/app.php

'providers' => [
    sixlive\Laravel\JsonSchemaAssertions\ServiceProvider::class,
]

Configuration

Publish the packages config file:

> php artisan vendor:publish --provider="sixlive\Laravel\JsonSchemaAssertions\ServiceProvider" --tag="config"

This is the contents of the file which will be published at config/json-schema-assertions:

return [
    'schema_base_path' => base_path('schemas'),
];

Usage

If you are making use of external schema refrences e.g. $ref: 'bar.json, you must reference the schema through file path or using the config path resolution.

├── app
├── bootstrap
├── config
├── database
├── public
├── resources
├── routes
├── schemas
│   ├── bar.json
│   └── foo.json
├── storage
├── tests
└── vendor
/** @test */
public function it_has_a_valid_response()
{
    $schema = [
        'type' => 'object',
        'properties' => [
           'foo' => [
                'type' => 'string',
           ],
         ],
         'required' => [
            'foo',
        ],
    ];

    $response = $this->get('/foo');

    // Schema as an array
    $response->assertJsonSchema($schema);

    // Schema from raw JSON
    $response->assertJsonSchema(json_encode($schema));

    // Schema from a file
    $response->assertJsonSchema(base_path('schemas/foo.json'));

    // Schema from config path
    $response->assertJsonSchema('foo');

    // Remote schema
    $response->assertJsonSchema('https://docs.foo.io/schemas/foo.json');
}

Testing

> composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Code Style

In addition to the php-cs-fixer rules, StyleCI will apply the Laravel preset.

Linting

> composer styles:lint

Fixing

> composer styles:fix

Security

If you discover any security related issues, please email oss@tjmiller.co instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.