Packages · Open source
Laravel Data Definition
Wraps an array payload, like a webhook body or an API response, in a small immutable object. You name the deep paths and their types once, and a value of the wrong type throws an exception. It is never quietly turned into something else.
Webhooks from ad platforms and CRMs, where the email is three levels down, every number is a string, and each vendor formats the timestamp differently.
Before
$payload = [
'created_time' => '2026-03-02T09:15:00Z',
'campaign' => ['utm_source' => 'google'],
'contact' => ['fields' => [
'email' => 'ana@example.com',
'monthly_budget' => 'n/a',
]],
];
$email = $payload['contact']['fields']['email'];
$source = $payload['campaign']['utm_source'];
$at = Carbon::parse($payload['created_time']);
$budget = (int) $payload['contact']['fields']['monthly_budget'];
echo "{$email} via {$source} on {$at->format('j M Y')}", PHP_EOL;
echo "budget: {$budget}", PHP_EOL;ana@example.com via google on 2 Mar 2026
budget: 0That reads the payload. But this lead typed n/a for the budget, and (int) turned it into 0. Nothing failed, so that zero is what gets saved. Here is the same payload read with the package. You write a small class once, and then you read the lead by its short names.
After
use Keaton\DataDefinition\DataDefinition;
use Keaton\DataDefinition\Exceptions\CastFailed;
class Lead extends DataDefinition
{
protected array $aliases = [
'email' => 'contact.fields.email',
'budget' => 'contact.fields.monthly_budget',
'source' => 'campaign.utm_source',
'at' => 'created_time',
];
protected array $casts = [
'budget' => 'int',
'at' => 'datetime',
];
}
$lead = Lead::make($payload);
echo "{$lead->email} via {$lead->source} on {$lead->at->format('j M Y')}", PHP_EOL;
try {
echo "budget: {$lead->budget}", PHP_EOL;
} catch (CastFailed $e) {
echo $e->getMessage(), PHP_EOL;
}ana@example.com via google on 2 Mar 2026
Lead: cannot cast 'budget' to int: got 'n/a' (expected an integer or a plain integer string).The first line is the same. The budget wasn't a number, so the package threw an exception that names the field and the value, and there is no zero to save. The paths and the types now live in one class, so every part of your app reads a lead the same way.
What it does
- Cast a value to a backed enum. A value that isn't one of its cases throws, so you notice when an API starts sending a new status.
- Cast a sub-object to another definition, and a list to a collection of them.
- Use your own cast classes. They implement CastsAttributes, the same contract you use for an Eloquent model.
- getOrFail() throws when a value is missing, and the message names the path. A missing key and a null are not the same thing.
- toArray() and json() return the declared values by their short names, so you can pass a definition to a response.
- makeMany() wraps every item of a list or a generator.