assert icon indicating copy to clipboard operation
assert copied to clipboard

API to improve describing property paths

Open arjanfrans opened this issue 3 years ago • 0 comments

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.

arjanfrans avatar Apr 06 '22 15:04 arjanfrans