Perhaps in the app I have a feature allowing users to send feedback using a form with some validation logic:
Where would you put these validation logic, either in domain layer
as business logic or in presentation layer
as UI logic?
These logic are applied for all applications (android, iOS, web). Please note that we already had server side validation.
In general, it is best to perform input validation on both the client side and server side. Client-side input validation can help reduce server load and can prevent malicious users from submitting invalid data.
Business logic tier validation assumes existing data is valid. The business logic tier employs validation logic when it needs to create or change data, but it assumes that data already in the table is valid.
I think many developers do that in Presentation
layer, specifically in ViewModel/Presenter/Controller
(not in Activity/Fragment/View!
). My approach is to put that logic in Domain
layer. Why?
ValidationException
with explanation. ValidationException
will contain list of invalid parameters, type of validation they failed (minLength, maxLength, emailPatternMismatch etc..), what is expected (20 char at max etc..). ViewModel/Presenter/Controller
gets this ValidationException
and here we have Presentation logic. Now it decides what to render, how to render. Do we render error of all invalid inputs or only first invalid input? What text/color should be shown (based on data in ValidationException) ? Do we render error as popup/textView/tooltip? After all presentation decisions are made and new model is created, View
just! renders using that model.fun validate():ValidationOutcome
function. I don't think it is problem to put validation logic of Business Model in itself. All UseCases would call model.validate()
only. There is dependency between Model and ValidationOutcome.I guess this example of Uncle Bob quoted by @sufian and this article can be useful when making that decision.
Naoto points out that just as Clean Architecture splits the responsibility by layers, each layer has its own validation logic.
In each layer, the system should reject the input which breaks its layer's responsibility. So the meaning of validation is differrent depending on its context.
In Application layey, as validation, we must ensure that domain objects can receive the input. We should reject the input which the domain object can't be received.
For example, when some mandatory parameters are missing, it should be rejected because the domain object has no way to receive like that parameter.
I'm not an android nor ios developer but I have some experience in web dev. This question is asked constantly by some coworkers. For me, the answer is both.
For example, if you have the validation logic in the presentation layer, whenever a user sends a bad input, you must go to the server, validate and then return the errors. To avoid asking the server you could validate the presentation layer with html5 or javascript. If some input is bad, this is shown to the user and there is no communication with the server (so you avoid one request). But this validation can be skipped easily, so if a user changes something or do the request with a tool (like postman) this validation doesn't happen. So, you can not be sure the data you are receiving is ok. For that, you need the server validation too.
For me, this is the safer solution and you only use UI to avoid bad request to the server.
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With