diff --git a/tests/ValidationTest.php b/tests/ValidationTest.php index 4dffe2a2..0d3f96db 100644 --- a/tests/ValidationTest.php +++ b/tests/ValidationTest.php @@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Foundation\Application; use Illuminate\Http\Request; +use Illuminate\Support\Collection; use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Validator as ValidatorFacade; use Illuminate\Validation\Rule; @@ -70,6 +71,11 @@ use Spatie\LaravelData\Tests\Fakes\ValidationAttributes\PassThroughCustomValidationAttribute; use Spatie\LaravelData\Tests\TestSupport\DataValidationAsserter; +class TestValidationDataWithCollectionNestedDataWithFieldReference extends Data +{ + public DataWithReferenceFieldValidationAttribute $nested; +} + it('can validate a string', function () { $dataClass = new class () extends Data { public string $property; @@ -649,12 +655,7 @@ public static function rules(Application $app): array ); }); -test('can use a reference to another field in a collection with nested data', function () { - class TestValidationDataWithCollectionNestedDataWithFieldReference extends Data - { - public DataWithReferenceFieldValidationAttribute $nested; - } - +test('can use a reference to another field in a data collection with nested data', function () { $dataClass = new class () extends Data { #[DataCollectionOf(TestValidationDataWithCollectionNestedDataWithFieldReference::class)] public DataCollection $collection; @@ -686,6 +687,71 @@ class TestValidationDataWithCollectionNestedDataWithFieldReference extends Data ); }); +test('can use a reference to another field in a collection with nested data', function () { + $dataClass = new class () extends Data { + /** @var Collection */ + public Collection $collection; + }; + + DataValidationAsserter::for($dataClass) + ->assertOk([ + 'collection' => [ + ['nested' => ['check_string' => '0']], + ], + ]) + ->assertErrors([ + 'collection' => [ + ['nested' => ['check_string' => '1']], + ], + ]) + ->assertRules( + rules: [ + 'collection' => ['present', 'array'], + 'collection.0.nested' => ['required', 'array'], + 'collection.0.nested.check_string' => ['boolean'], + 'collection.0.nested.string' => ['string', 'required_if:collection.0.nested.check_string,1'], + ], + payload: [ + 'collection' => [ + ['nested' => ['check_string' => '1']], + ], + ] + ); +}); + +test('can use a reference to another field in an array with nested data', function () { + + $dataClass = new class () extends Data { + /** @var array */ + public array $collection; + }; + + DataValidationAsserter::for($dataClass) + ->assertOk([ + 'collection' => [ + ['nested' => ['check_string' => '0']], + ], + ]) + ->assertErrors([ + 'collection' => [ + ['nested' => ['check_string' => '1']], + ], + ]) + ->assertRules( + rules: [ + 'collection' => ['present', 'array'], + 'collection.0.nested' => ['required', 'array'], + 'collection.0.nested.check_string' => ['boolean'], + 'collection.0.nested.string' => ['string', 'required_if:collection.0.nested.check_string,1'], + ], + payload: [ + 'collection' => [ + ['nested' => ['check_string' => '1']], + ], + ] + ); +}); + it('can reference to the root validated object in nested data', function () { class TestDataWithRootReferenceFieldValidationAttribute extends Data {