Declaring the head

Requests

Declare a FormRequest whose input is validated in the client's revision and upgraded to the head.

A form request implements Revisioned, uses RevisionedRequest, and declares the head of its body in body() and of its query string in queryParameters().

app/Http/Requests/StoreOrderRequest.php
use Ark4ne\ApiRevisions\Laravel\Requests\RevisionedRequest;
use Ark4ne\ApiRevisions\Revisions\Revision;
use Ark4ne\ApiRevisions\Schema\Revisioned;
use Ark4ne\ApiRevisions\Schema\Schema;

final class StoreOrderRequest extends FormRequest implements Revisioned
{
    use RevisionedRequest;

    protected function body(): array
    {
        return [
            'title' => Schema::string()->required()->max(255),
            'status' => Schema::enum(OrderStatus::class)
                ->was(ApiRevision::Winter25, name: 'state'),
            ...Schema::since(ApiRevision::Spring26, [
                'first_name' => Schema::string()->required(),
                'last_name' => Schema::string()->required(),
            ], default: null),
            ...Schema::until(ApiRevision::Winter25, [
                'name' => Schema::string()->required(),
            ]),
        ];
    }

    protected function queryParameters(): array
    {
        return [
            'notify' => Schema::bool()
                ->was(ApiRevision::Winter25, name: 'send_email'),
        ];
    }

    protected function upgrade(Revision $to, array $input): array
    {
        if (
            $to === ApiRevision::Spring26
            && is_string($input['name'] ?? null)
        ) {
            [$input['first_name'], $input['last_name']]
                = explode(' ', $input['name'], 2) + [1 => ''];
        }

        return $input;
    }
}

Do not write rules(): it is generated.

How a request is handled

  1. Validate in the client's revision. The rules are generated from the shape of that revision. Error keys use the names the client knows: a winter-25 client sending an invalid state gets an error on state.
  2. Upgrade one revision at a time. Each step renames and converts the fields (an up: closure receives typed values), calls upgrade($to, $input), adds the defaults of the fields added in $to, and drops the fields that no longer exist.
  3. Return the head. validated() and safe() return the head input, body and query, serialized back to raw values ('paid', not OrderStatus::Paid).
POST
/orders
Api-Revision: winter-25
{ "title": "Lamp", "state": "lost", "total": "9.99" }
Response422 Unprocessable Content
{
  "message": "The selected state is invalid. (and 1 more error)",
  "errors": {
    "state": ["The selected state is invalid."],
    "name": ["The name field is required."]
  }
}
Read the head from validated() or safe(), never from input(), all() or $request->field: those still hold what the client sent, in its revision.

The upgrade hook

upgrade(Revision $to, array $input) handles what a was() cannot express, such as splitting a field. It is called once per step, with $to the newer revision of the step and the input already renamed and converted to it. It runs before the defaults of the fields added in $to are filled; the fields that no longer exist in $to are dropped after it.

An input the upgrade rejects (a ConversionException thrown by an up: closure) becomes a validation error on the client's key.

Defaults and missing keys

  • A field added in a later revision is never sent by older clients. since(R, default: ...) gives what the controller receives instead. A required() field added later must declare one, or the check fails (required-since-without-default); default: null gives an explicit null.
  • Conversions and defaults apply to present keys only, and a default never fills a key sent as null: a PATCH keeps "not sent" and "sent as null" apart.

Store and update

Store and update requests share their fields through a static method returning the array; each adjusts required() and sometimes().

JSON:API users declare a write request with the same trait: see Write requests across revisions.