Requests
A form request implements Revisioned, uses RevisionedRequest, and declares the head of its body in body() and of
its query string in queryParameters().
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
- 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-25client sending an invalidstategets an error onstate. - Upgrade one revision at a time. Each step renames and converts the fields (an
up:closure receives typed values), callsupgrade($to, $input), adds the defaults of the fields added in$to, and drops the fields that no longer exist. - Return the head.
validated()andsafe()return the head input, body and query, serialized back to raw values ('paid', notOrderStatus::Paid).
{ "title": "Lamp", "state": "lost", "total": "9.99" }
{
"message": "The selected state is invalid. (and 1 more error)",
"errors": {
"state": ["The selected state is invalid."],
"name": ["The name field is required."]
}
}
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. Arequired()field added later must declare one, or the check fails (required-since-without-default);default: nullgives an explicitnull. - Conversions and defaults apply to present keys only, and a default never fills a key sent as
null: aPATCHkeeps "not sent" and "sent asnull" 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.