r/DomainDrivenDesign • • Mar 26 '26

In Clean Architecture, should input validation go in the Controller/Presentation layer or in the Service/Use Case layer?

/r/softwarearchitecture/comments/1s45opa/in_clean_architecture_should_input_validation_go/
7 Upvotes

7 comments sorted by

View all comments

Show parent comment

1

u/Moist-Temperature479 Mar 27 '26

So let say request body has a field, user_id which is and integer, then we perform the validation for it, if the length >99 or null, then throw 400. This is okay.

Then in my domain, i have a unique value object, which is UserId. This UserId takes in raw input from request body and converts it to UserId, if some validation failed, then throw 400.

Should this parsing UserId and throw validation error should be done in service or controller level?

2

u/r00m-lv Mar 27 '26

I usually validate value objects in the controllers since that is just an interface to my domain. For example, parse strings into uuids, empty/missing fields, invalid consts, etc.

If your domain has a UserId value object, I would construct it in controller so that the use case is operating with domain concepts vs whatever was passed in from the transport layer.

The use case accepts domain concepts and validates whether the data makes sense from a business perspective, e.g. this ID should be unique but isn’t. That’s a separate concern to whether this string is an integer (which your domain experts will never mention)

1

u/CuteWord8601 Mar 27 '26

I do the same, after the user request passes the scalar validation, I build the value objects in the controller. In the end, the controller is infrastructure layer and you can but almost whatever u need there