Watching this talk about Symfony Forms and validation, at 23:06, I got stuck with a doubt I couldn't find a satisfying answer to:
Is it a bad practice to execute a query (or a web request, or a side effect in general) inside a validator?
In my case, I have either Symfony Validation Constraints and some implementation of the Specification Pattern.
I am wondering how bad it is to perform queries to the database into these validation elements, given the fact that they could be used multiple times in the same controller (or in the same form, in the case of using Symfony components).
- there's a performance concern; especially in case the request into the validation is always the same and there's no real reason to send it multiple times.
- there's a failure concern; what if the response is delayed or, even worse, the request fails?
- should the validation add an incident/violation because it wasn't able to ensure that the data is actually valid (even in the case where it actually is) and report it together with the other incidents/violations ?
- should it accept it instead because of the impossibility to validate?
- should it try again?
- should it throw an error?
- there's a boundaries crossing and lack of separation of concerns; I would not expect that anything from the Domain Model (IMHO, including its invariants/validation-rules) knows about or has dependencies on anything from the Infrastructure (the persistence layer in this case).
Is it acceptable? Should it be avoided? Is it totally fine? I generally have a feeling of rejection in doing that (and that's why I am asking).