beberlei / assert

Thin assertion library for use in libraries and business-model
Other
2.41k stars 186 forks source link

API to improve describing property paths #324

Open arjanfrans opened 2 years ago

arjanfrans commented 2 years ago

I am using validation in my models and want to be specific about the validation errors. I want to know from which class and property and error came. I am aware that it is possible by setting the 'defaultPropertyPath' to something like 'Address.street', but this will cause a lot of duplication. For each 'that' call you'd have to specify the property.

What I am looking for is an API which looks more like this:

Assert::lazy('Address')
            ->that($street, 'street')->notEmpty()
            ->that($number, 'number')->notEmpty()
            ->verifyNow();
Assert::that('Address', 'street')->notEmpty();

For the "root" it might also be a nice idea to add a 'getRootPath' function that can be overriden. So that someone can pass in $this or $something::class and then format it inside this function, for example: reading $this::class and stripping it down to the basename.

I already have an implementation for this. Of course, to be backwards compatible it would mean that the that function can not be used; so maybe a new function called thatProperty.