Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with nested data validation #491

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions tests/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
use Spatie\LaravelData\WithData;

use function Spatie\Snapshots\assertMatchesSnapshot;
use Spatie\LaravelData\Tests\Fakes\DataWithDatetimeImmutable;
use Spatie\LaravelData\Attributes\Validation\Nullable;

it('can create a resource', function () {
$data = new SimpleData('Ruben');
Expand Down Expand Up @@ -568,6 +570,36 @@ public function __construct(
]);
});

it('can validate nested data collection date from json string', function () {

$objectWithDataCollection = new class () extends Data {
#[DataCollectionOf(DataWithDatetimeImmutable::class), Nullable]
public ?DataCollection $collection;

public bool $boolean;
};

$firstJson = '{"date_to": "2020-05-16", "date_from": "2020-05-17"}';
$secondJson = '{"date_to": "2020-05-16", "date_from": "2020-05-15"}';

expect(DataWithDatetimeImmutable::from(json_decode($firstJson, true)))
->date_to
->toBeInstanceOf(DateTimeImmutable::class)
->toEqual(DateTimeImmutable::createFromFormat('Y-m-d', '2020-05-16'));

expect(fn() => DataWithDatetimeImmutable::validateAndCreate(json_decode($secondJson, true)))
->toThrow(ValidationException::class);

$jsonArray = json_decode('{"boolean":true,"collection":[{"date_to":"2020-05-16","date_from":"2020-05-15"},{"date_to":"2020-05-16","date_from":"2020-05-17"}]}', true);

expect($objectWithDataCollection::validateAndCreate($jsonArray))
->boolean->toBeTrue();

expect(fn() => $objectWithDataCollection::validateAndCreate($jsonArray))
->toThrow(ValidationException::class);

});

it('can get the data object without transforming', function () {
$data = new class (
$dataObject = new SimpleData('Test'),
Expand Down
19 changes: 19 additions & 0 deletions tests/Fakes/DataWithDatetimeImmutable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Spatie\LaravelData\Tests\Fakes;

use DateTimeImmutable;
use Spatie\LaravelData\Attributes\Validation\AfterOrEqual;
use Spatie\LaravelData\Attributes\Validation\Required;
use Spatie\LaravelData\Attributes\WithCast;
use Spatie\LaravelData\Casts\DateTimeInterfaceCast;
use Spatie\LaravelData\Data;

class DataWithDatetimeImmutable extends Data
{
#[Required, BeforeOrEqual('date_to'), WithCast(DateTimeInterfaceCast::class, format: 'Y-m-d')]
public DateTimeImmutable $date_from;

#[Required, AfterOrEqual('date_from'), WithCast(DateTimeInterfaceCast::class, format: 'Y-m-d')]
public DateTimeImmutable $date_to;
}
Loading