API revisions

Write requests across revisions

Declare a request body whose members change with the revision of the client.

The body a client sends changes with its revision too. The request uses the RevisionedRequest of ark4ne/laravel-api-revisions and declares its body with descriptors, as a resource declares its attributes: each member that changed declares its history.

app/Http/Requests/PostStoreRequest.php
use Ark4ne\ApiRevisions\Laravel\Requests\RevisionedRequest;
use Ark4ne\ApiRevisions\Schema\Revisioned;
use Ark4ne\ApiRevisions\Schema\Schema;
use Ark4ne\JsonApi\Http\Body;
use Ark4ne\JsonApi\Http\JsonApiFormRequest;

final class PostStoreRequest extends JsonApiFormRequest implements Revisioned
{
    use RevisionedRequest;

    protected string $resource = PostResource::class;

    protected function body(): array
    {
        return $this->resourceObject(
            attributes: [
                'title' => Schema::string()->required()->max(255)
                    ->was(ApiRevision::Labels, name: 'label')
                    ->was(
                        ApiRevision::Initial,
                        name: 'name',
                        schema: Schema::string()->required()->max(80),
                    ),
                'summary' => Schema::string()
                    ->nullable()
                    ->since(ApiRevision::Labels, default: ''),
            ],
            relationships: [
                'author' => Body::toOne(UserResource::class),
                'tags' => Body::toMany(TagResource::class),
            ],
        );
    }
}
  • The body is validated in the names of the revision of the client, and its errors point at the members it sent.
  • The controller reads it in the names of the head: $request->jsonApi()->attribute('title'), ->ids('author'). A member added since the client's revision is given its default.
  • include and fields stay validated at the revision of the request.

The resource object

  • resourceObject() declares the resource object of the resource of the request: data.type is its type.
  • id: takes the descriptor of data.id (Schema::string()->required() to update a resource).
  • data.attributes and data.relationships are required when one of their members is.
  • Body::toOne() and Body::toMany() declare a relationship object and the types its linkage accepts (resource classes or types). {"data": null} clears a to-one relationship, {"data": []} a to-many one.

A request that is not a JsonApiFormRequest calls Body::resource(PostResource::class, …).