|
15 | 15 | use Spatie\LaravelData\Support\DataConfig;
|
16 | 16 | use Spatie\LaravelData\Support\DataProperty;
|
17 | 17 | use Spatie\LaravelData\Support\Validation\DataRules;
|
| 18 | +use Spatie\LaravelData\Support\Validation\EnsurePropertyMorphable; |
18 | 19 | use Spatie\LaravelData\Support\Validation\PropertyRules;
|
19 |
| -use Spatie\LaravelData\Support\Validation\RequiresPropertyMorphableClassRule; |
20 | 20 | use Spatie\LaravelData\Support\Validation\RuleDenormalizer;
|
21 | 21 | use Spatie\LaravelData\Support\Validation\RuleNormalizer;
|
22 | 22 | use Spatie\LaravelData\Support\Validation\ValidationContext;
|
@@ -75,7 +75,7 @@ public function execute(
|
75 | 75 | );
|
76 | 76 |
|
77 | 77 | if ($dataProperty->isForMorph) {
|
78 |
| - $rules[] = new RequiresPropertyMorphableClassRule($dataClass); |
| 78 | + $rules[] = new EnsurePropertyMorphable($dataClass); |
79 | 79 | }
|
80 | 80 |
|
81 | 81 | $dataRules->add($propertyPath, $rules);
|
@@ -109,14 +109,18 @@ protected function propertyMorphableDataClass(
|
109 | 109 | $pipeline = $this->dataConfig->getResolvedDataPipeline($class);
|
110 | 110 |
|
111 | 111 | try {
|
112 |
| - $properties = Arr::only($pipeline->execute( |
| 112 | + // Attempt to cast properties |
| 113 | + $properties = $pipeline->execute( |
113 | 114 | $path->isRoot() ? $fullPayload : Arr::get($fullPayload, $path->get(), []),
|
114 | 115 | $creationContext
|
115 |
| - ), $dataClass->propertyMorphablePropertyNames); |
| 116 | + ); |
116 | 117 | } catch (\Throwable $exception) {
|
117 | 118 | return null;
|
118 | 119 | }
|
119 | 120 |
|
| 121 | + // Restrict to only morphable properties |
| 122 | + $properties = Arr::only($properties, $dataClass->propertyMorphablePropertyNames); |
| 123 | + |
120 | 124 | // Only morph if all properties are present
|
121 | 125 | if (count($properties) !== count($dataClass->propertyMorphablePropertyNames)) {
|
122 | 126 | return null;
|
|
0 commit comments