Repository navigation
RequestData
Viames Marino edited this page Oct 19, 2026
·
2 revisions
Pair\Api\RequestData is a small contract for custom endpoint request objects.
Use it when a controller should validate input and map it into an explicit typed object before running domain logic.
interface RequestData {
public static function fromArray(array $data): static;
}fromArray() receives the fields selected and validated by the rules passed to Request::validateObjectOrResponse(). An empty rule array produces an empty data array; it does not infer rules from the request object's constructor or forward the entire request.
use Pair\Api\RequestData;
final readonly class CreateOrderRequest implements RequestData {
public function __construct(
public int $customerId,
public float $amount,
public string $currency
) {}
public static function fromArray(array $data): static
{
return new self(
(int)$data['customerId'],
(float)$data['amount'],
strtoupper((string)$data['currency'])
);
}
}Controller usage:
$payload = $this->request->validateObjectOrResponse(CreateOrderRequest::class, [
'customerId' => 'required|int',
'amount' => 'required|numeric|min:0.01',
'currency' => 'required|string|max:3',
]);
if ($payload instanceof \Pair\Api\ApiErrorResponse) {
return $payload;
}
return \Pair\Api\ApiResponse::jsonResponse([
'customerId' => $payload->customerId,
], 201);-
RequestDatadoes not perform validation by itself. - Keep normalization and type casting inside
fromArray(). - Supply rules for every field the mapper needs. Unknown rules are not enforced, and optional
nullfields are omitted from the validated array; see Request for the exact validation boundaries. - Required fields can rely on their validation rules; optional fields need defaults or explicit presence checks before array access.
- The mapper's class must exist and implement
RequestData; invalid classes raiseInvalidArgumentException. Exceptions thrown byfromArray()propagate to the caller rather than becoming field-validation errors. -
validateObjectOrResponse()returns anApiErrorResponseinstead of terminating immediately when validation fails. -
Request::validate()remains the legacy terminate-on-error bridge.
See also: Request, ApiController, ApiResponse.
Home · Setup · Configuration
Build with Pair 4
- Generator
- Controllers and responses
- Read models
- ActiveRecord and queries
- Forms and validation
- PairUI.js and HTML fragments
APIs and authentication
- API guide and CRUD
- Middleware and rate limits
- Users, sessions, and ACL
- Passkeys and social login
- Native mobile setup
Integrate and operate
- Integrations
- Runtime extensions and adapter keys
- Cache and cache stores
- Logs and observability
- PWA and push
- Packages and distribution
- Upgrade to v4