The package will automatically infer validation rules from the data object. For example, for the following data class:
class ArtistData extends Data{
public function __construct(
public string $name,
public int $age,
public ?string $genre,
) {
}
}
The package will generate the following validation rules:
[
'name' => ['required', 'string'],
'age' => ['required', 'integer'],
'genre' => ['nullable', 'string'],
]
All these rules are inferred by RuleInferrers
, special classes that will look at the types of properties and will add rules based upon that.
Rule inferrers are configured in the data.php
config file:
'rule_inferrers' => [
Spatie\LaravelData\RuleInferrers\SometimesRuleInferrer::class,
Spatie\LaravelData\RuleInferrers\NullableRuleInferrer::class,
Spatie\LaravelData\RuleInferrers\RequiredRuleInferrer::class,
Spatie\LaravelData\RuleInferrers\BuiltInTypesRuleInferrer::class,
Spatie\LaravelData\RuleInferrers\AttributesRuleInferrer::class,
],
By default, five rule inferrers are enabled:
-
SometimesRuleInferrer will add a
sometimes
rule when the property is optional
-
NullableRuleInferrer will add a
nullable
rule when the property is nullable
-
RequiredRuleInferrer will add a
required
rule when the property is not nullable
-
BuiltInTypesRuleInferrer will add a rules which are based upon the built-in php types:
- An
int
or float
type will add the numeric
rule
- A
bool
type will add the boolean
rule
- A
string
type will add the string
rule
- A
array
type will add the array
rule
-
AttributesRuleInferrer will make sure that rule attributes we described above will also add their rules
It is possible to write your rule inferrers. You can find more information here.