Skip to content

Add validation rules, lazy properties, and readonly properties#78

Merged
dereuromark merged 2 commits intomasterfrom
feature/validation-lazy-readonly
Feb 8, 2026
Merged

Add validation rules, lazy properties, and readonly properties#78
dereuromark merged 2 commits intomasterfrom
feature/validation-lazy-readonly

Conversation

@dereuromark
Copy link
Contributor

Adds three new features to close competitive gaps with libraries like spatie/laravel-data and cuyz/valinor:

Validation Rules

Fields now support built-in validation constraints via the config builder:

Field::string('name')->required()->minLength(2)->maxLength(100)
Field::string('email')->pattern('/^[^@]+@[^@]+$/')
Field::int('age')->min(0)->max(150)
Field::float('score')->min(0.0)->max(100.0)

Rules are checked during construction (both fast-path and runtime). Null fields skip validation. On failure, throws InvalidArgumentException with descriptive message.

Lazy Properties

DTO and collection fields can defer hydration until first getter access:

Field::dto('customer', 'Customer')->asLazy()
Field::collection('items', 'OrderItem')->singular('item')->asLazy()

Raw array data is stored in $_lazyData during construction. The getter hydrates on demand. toArray() returns raw data directly if not yet hydrated — avoiding unnecessary object creation for pass-through scenarios.

Readonly Properties

DTOs can generate public readonly properties for language-level immutability:

Dto::create('Config')->readonlyProperties()->fields(
    Field::string('host')->required(),
    Field::int('port')->default(8080),
)

Generates public readonly ?string $host instead of protected ?string $host. The with*() methods use reconstruct-from-array to work around PHP's readonly limitations. Implies immutable.

Changes

  • FieldBuilder: validation rule methods (minLength, maxLength, min, max, pattern) and asLazy()
  • DtoBuilder: readonlyProperties() method (implies immutable)
  • Builder/FieldCompletor: new field defaults and metadata keys
  • Dto.php: validation rules in validate(), $_lazyData for lazy fields
  • Templates: readonly property declarations, lazy getters, reconstruct-from-array with*() methods
  • Tests: 38 new tests (ValidationTest, LazyTest, ReadonlyTest, config builder tests)
  • Docs: updated ConfigBuilder.md and Validation.md

Features:
- Validation rules: minLength, maxLength, min, max, pattern for field constraints
- Lazy properties: defer DTO/collection hydration until first getter access
- Readonly properties: public readonly with reconstruct-from-array for with*() methods

Config API:
- Field::string('name')->minLength(2)->maxLength(50)
- Field::int('age')->min(0)->max(150)
- Field::string('email')->pattern('/regex/')
- Field::dto('nested', 'Other')->asLazy()
- Dto::create('X')->readonlyProperties()

Changes:
- FieldBuilder: validation rule methods and lazy flag
- DtoBuilder: readonlyProperties() method (implies immutable)
- Builder/FieldCompletor: new field defaults and metadata keys
- Dto.php: validation rules in validate(), $_lazyData for lazy fields
- Templates: readonly property declarations, lazy getters, reconstruct-from-array with*() methods
- Tests: ValidationTest, LazyTest, ReadonlyTest + config builder tests
- Docs: updated ConfigBuilder.md and Validation.md
@codecov
Copy link

codecov bot commented Feb 8, 2026

Codecov Report

❌ Patch coverage is 95.89041% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.93%. Comparing base (4496cdb) to head (48f9927).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
src/Engine/XmlEngine.php 60.00% 2 Missing ⚠️
src/Generator/Builder.php 66.66% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master      #78      +/-   ##
============================================
+ Coverage     82.56%   82.93%   +0.37%     
- Complexity     1333     1375      +42     
============================================
  Files            40       40              
  Lines          3252     3323      +71     
============================================
+ Hits           2685     2756      +71     
  Misses          567      567              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

- dto.xsd: add minLength, maxLength, min, max, pattern, lazy field attributes and readonlyProperties DTO attribute
- dto.schema.json: add same fields for YAML/NEON validation
- XmlEngine: cast lazy/readonlyProperties as bool, minLength/maxLength as int, min/max as numeric
@dereuromark dereuromark merged commit 1db1605 into master Feb 8, 2026
12 checks passed
@dereuromark dereuromark deleted the feature/validation-lazy-readonly branch February 8, 2026 12:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant