mesolaries/smart-api-bundle
Smart API Bundle is a Laravel/PHP package for building cleaner, more consistent APIs with less boilerplate. It streamlines common API concerns such as structured responses, validation, and reusable endpoints, helping teams ship maintainable JSON APIs faster.
Install the package with Composer and publish any package config if your version provides it. Then locate the two main extension points: the validation map (where request body rules are declared) and the normalizer/processor registrations.
First use case: validate a JSON request body where one or two fields are optional and should fall back to a default instead of failing. Define the validation map for that endpoint, attach the new default option to the optional rule, and leave $throwOnUndefined enabled initially so missing required fields fail loudly during development.
Start by reading:
$throwOnUndefined and rule-level default.A minimal flow looks like:
// Conceptual shape; use the package's exact rule syntax.
final class CreateUserRequest
{
protected array $validationMap = [
'name' => ['required', 'string'],
'nickname' => [
'rules' => ['string', 'max:255'],
'default' => 'anonymous',
],
'age' => ['nullable', 'integer'],
];
}
Use this package first for API endpoints with partial payloads, imported data, or frontend forms where the difference between “missing” and “invalid” matters.
Use a validation map per request type or endpoint. Keep the map close to the controller/action or in a dedicated request class if the package supports it, so required fields, defaults, and undefined-field behavior are visible in one place.
Common day-to-day patterns:
default to rules for fields that may be omitted. Use defaults for presentation or backward-compatible behavior, not for domain invariants that should always be supplied by the client.$throwOnUndefined = true for public APIs and machine-to-machine contracts. Set it to false for internal or legacy endpoints that may send partial bodies, where removing undefined parameters is acceptable.null. Since v1.1 runs normalizers before validation, normalizers should accept raw input and avoid assuming validated data.$throwOnUndefined modes if the endpoint can be configured either way.A typical workflow:
default to optional rules.$throwOnUndefined per request or config.default and $throwOnUndefined interact. A rule-level default may prevent an exception for a missing value, while $throwOnUndefined controls whether undefined parameters are rejected or removed. Test the combination explicitly; do not assume one overrides the other in every case.$throwOnUndefined = false, downstream has(), filled(), array_key_exists(), and array access checks may behave differently than before validation. Use the validated body consistently.$throwOnUndefined = true to expose keys that are being silently removed. Once the client contract is fixed, switch back to the intended mode.$throwOnUndefined are config-driven, run the Laravel config cache refresh in deployment and test with cached config enabled.How can I help you explore Laravel packages today?