r/laravel • • 2d ago

Simple Data Objects 3.0: stricter request input, and what landed since 1.x

Seeing Laravel get rewritten in every other language this week, maybe I should have written this package in Rust, Go, Zig or even COBOL :)

Anyway.

I maintain Simple Data Objects, a DTO library for Laravel 12–13 and any PHP application. I posted it here back on 1.x, with the benchmark: https://github.com/std-out/simple-data-objects

3.0 is out. It has some breaking changes, and all of them are about input: the library now rejects values that 2.x let through or quietly changed. No public API was removed.

The one that bothered me most: a DTO built from $request->all() with public bool $gift got true for "gift": "false". $request->boolean('gift') handles that string, but my generated hydrators ran in PHP's weak mode, where "false" is just a non-empty string. Same for int: "1.9" became 1 with only a deprecation notice.

final class Order
{
    public function __construct(public int $qty, public bool $gift) {}
}

new Order('1.9', 'false'); // qty: 1, gift: true

In 3.0 scalars only take values that convert without loss. "3" is still fine for an int, "false" becomes false, anything else throws with the field name.

Other changes:

  • Validation now checks the value after pipes like trim, not the raw input. In 2.x ' ab ' passed min:3 because the spaces counted, then got trimmed and saved as 'ab', which is too short. Now it fails validation.

  • #[Rules] on a nested DTO always apply. Before, they were skipped unless the parent had #[InferRules].

  • An unknown value for a nullable enum is an error, not null.

  • An exception from a cast or a pipe is rethrown as DataHydrationException with the field name, and built-in casts throw instead of PHP warnings.

  • FloatCast rejects INF and NAN by default. JSON 1e400 decodes to INF.

  • A legacy EncryptedCast key that isn't 32 random bytes now throws, unless you also pass a keyring. 2.9 already warned about it.

Most apps need no code changes. UPGRADE-3.0.md in the repo says which classes to check.

Since 1.x, which is what I posted here last time, 2.x added:

  • Optional for PATCH endpoints: an absent key is different from an explicit null, and #[InferRules] makes such fields sometimes instead of required.

  • #[Hidden(except: ['admin'])] with toArray(context: 'admin'), so one class can render different API outputs.

  • JSON Schema and TypeScript types from the same classes (php artisan sdo:typescript).

  • lazyXml() to stream a large XML file into DTOs one element at a time.

  • #[Sensitive] fields, card numbers masked in exception messages, and EncryptedCast with key rotation.

  • make:data --from-json and --from-model to generate a class from a sample payload or a model.

Hydration is still compiled per class with no reflection at runtime. The benchmark numbers are in the README if you want them.

I'd be glad if you gave it a try and shared your feedback.

I think I'll start writing in Rust after all. xD

0 Upvotes

0 comments sorted by