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

Issue with nested data validation #491

wants to merge 1 commit into from

Conversation

wit3
Copy link

@wit3 wit3 commented Jul 3, 2023

#344 (reply in thread)

PR for check bug on nested data validation

@rubenvanassche
Copy link
Member

rubenvanassche commented Jul 5, 2023

I think you're test is incorrect, rewritten it:

it('can validate nested data collection date from json string', function () {
    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;
    }


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

        public bool $boolean;
    };

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

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

    // validation performed
    expect(fn() => DataWithDatetimeImmutable::validateAndCreate(json_decode($invalidJson, true)))
        ->toThrow(ValidationException::class);

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

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

    $jsonArray = json_decode('{"boolean":true,"collection":['.$invalidJson.','.$validJson.']}', true);

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

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

Still all looks valid to me, I'm gonna close this but you're always welcome to make this test break again with the case you had. Then we'll take a further look.

@wit3
Copy link
Author

wit3 commented Jul 5, 2023

@rubenvanassche Thanks for all support with this issue/pr

i've now test in my local with composer test and your updated test and two fail

    // validation performed
    expect(fn() => DataWithDatetimeImmutable::validateAndCreate(json_decode($invalidJson, true)))
        ->toThrow(ValidationException::class);

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

Exception "Illuminate\Validation\ValidationException" not thrown.

did you run it?

@rubenvanassche
Copy link
Member

screenshot 11

Jup, did you import the attributes, it first failed due to that reason in my test when I retried.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants