Skip to main content
A data object can contain other data objects or collections of data objects. The package will make sure that also for these data objects validation rules will be generated. Let’s take a look again at the data object from the nesting section:
The validation rules for this class would be:

Validating a nested collection of data objects

When validating a data object like this:
In this case the validation rules for AlbumData would look like this:
The NestedRules class is a Laravel validation rule that will validate each item within the collection for the rules defined on the data class for that collection.

Nullable and Optional nested data

If we make the nested data object nullable, the validation rules will change depending on the payload provided:
If no value for the nested object key was provided or the value is null, the validation rules will be:
If, however, a value was provided (even an empty array), the validation rules will be:

Optional nested data

The same happens when a property is made optional:
There’s a small difference compared against nullable, though. When no value was provided for the nested object key, the validation rules will be:
However, when a value was provided (even an empty array or null), the validation rules will be:
We’ve written a blog post on the reasoning behind these variable validation rules based upon payload. And they are also the reason why calling getValidationRules on a data object always requires a payload to be provided.