php-validation-dsl icon indicating copy to clipboard operation
php-validation-dsl copied to clipboard

Error Message Handling

Open ragboyjr opened this issue 6 years ago • 1 comments

Found your library from twitter, and it's a very neat idea. I built something similar a few years back: https://github.com/krakphp/validation But yours takes a much more orthodox approach (which is very neat).

My initial implementation was just a set of validators that you could compose up, but that got old super fast from a DX perspective, and then also, error messages were a pain to deal with in any real app type of context. I ended up settling on just having a violation class which stores the violation code and data, and then had a different set of classes responsible for transforming those violations into a set of error messages.

In your app, every validator needs to have context about formatting errors which seems like a heavy burden on implementing individual validators.

What would you think about adopting an implementation where validation results only carry basic data about the failure, and a separate service class is responsible for normalizing the error messages either into single string, or a hashmap of strings.

ragboyjr avatar Apr 10 '19 11:04 ragboyjr

Looks an interesting idea, I would need to experiment with it. From one point of view, your proposal looks like a good separation of concerns between validation and error formatting. Going with the way you're suggesting would allow to define the validator once and choose later in which format to return it. On the other hand I think I remember spending some time thinking about it some time ago, and in the end I decided to go this way. So probably there was a reason...

marcosh avatar Apr 10 '19 17:04 marcosh