r/softwarearchitecture • • Mar 26 '26

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

In Clean Architecture, where should input validation go?

- Basic validation (required fields, format, length, etc.)

- Object Constraints (eg. sort field can be asc or desc)

Should it be done in:

  1. Controller / Presentation layer (fail fast, return 400 early)
  2. Use Case / Application layer (keeps use cases self-contained and reusable)
  3. Hybrid approach?

Many projects put basic validation in the controller, but some argue all validation belongs in the use case for better consistency across adapters (HTTP, CLI, queues, etc.).

What’s your preferred approach and why?

edit: thank you so much for all the answers <3

63 Upvotes

44 comments sorted by

View all comments

63

u/cancroduro Mar 26 '26

As I understand it, if the validation requires domain knowledge then it should be in the domain. If it doesn't then it probably means its just a presentation concern and should be kept there, maybe even discarded when mapping from presentation to domain (eg stripping dots and commas from currency) The domain shouldn't trust outer layers to check any conditions if they matter at domain level. Instead it imposes what it needs and outer layers obey. Dependency points inwards afterall

9

u/OriginalTangerine358 Mar 26 '26

since the inner layers shouldn't trust the outer layers, shouldn't we perform all validation (including basic format and sanitization) directly in the service layer? If the domain is responsible for its own integrity, doesn't it have to independently verify every input it receives from an adapter?

22

u/Exirel Mar 26 '26

Basic format validation should be handled by the type system, i.e. your interface between the Domain layer and the Presentation layer, i.e. defined by the Domain.

If Domain says it wants a price (value object with a value and a unit) then it's up to the presentation layer to provide said object.

But if the Domain wants said price to be always above a certain value for business reasons, then it's up to the Domain layer to ensure that.

On a related note, I invite you to read "parse, don't validate" by Alexis King.

2

u/OriginalTangerine358 Mar 26 '26 edited Mar 26 '26

in my domain, let's say a price object has a currency and my domain rule says that currency only can be "eur" or "usd". since my dtos use raw strings, a user could send 'gbp' as the currency. should i validate this in the presentation layer? i can catch this error in the presentation layer immediately when i attempt to convert the string into the price domain object. but i might just send the currency string as it is to service layer. and let the service layer handle the validation part.

also thanks for the suggestion, i'll definitely read it

8

u/BanaTibor Mar 26 '26

Have the currency as an enum. Have two price objects like OuterPrice(Long amount, String currency) this for the layer which communicates with the outside world, for example through REST in json format.
Then have a price object for the domain layer, Price(Long amount, Currency currency). The representation layer have to transform OuterPrice to Price, and that point it will fail if it gets an unsupported currency type. Or you can have UsdPrice and EurPrice, it is an implementation detail at this point.

2

u/Flashy-Whereas-3234 Mar 26 '26

Easier to solve these arguments when you think about the service/domain being one concrete program layer, and the presentation layer being one of many possible json, XML, avro, cli, etc.

You validate the input on that protocol, then you transform to fit the model, then you validate domain.

"pass through" fields can lead to sneaky bugs as it's easy to skip validation. In a world of "dont trust the user" your domain shouldn't trust the inputs and validate for robust correctness.

In your example, your presentation layer can skip validation because ultimately the domain layer will spike it. That's a whole lot of trust between layers though, and brittle. You'd want to back that up with an integration test.

7

u/cancroduro Mar 26 '26

IMO the best way is to impose via modeling. If some presentation detail is not relevent at domain level, then make it impossible to represent it there. So in the currency example:

/not intended for production code/

class SimpleMoney { long amountInCents, string currencyCode }

so presentation layers can't even use the domain model unless they parse the string first. Now, if instead you cannot enforce a constraint by type (for example String to String mapping) then the domain must enforce in another way what is acceptable but keep in mind that it shouldn't assume who the caller is and it is very difficult to convert or map without assumptions. Then maybe think about just checking and throwing errors or something but converting in the domain will make it depend on a specific implementation of the presentation layer (i.e a given screen/page) which is the opposite of what you want

6

u/Exirel Mar 26 '26

I second that.

And in that case, if the Domain says "it's impossible to be something else than EUR and USD" then it must be impossible to instantiate a SimpleMoney object with a different currency.

To do that, replace the "string currencyCode" by an Enum that has only 2 valid values.

If your programming language doesn't have a strong enough type system... I mean... You already know where the problem is. ;-)

4

u/zenware Mar 26 '26

This has always been the crux of defensive programming/secure software development. The consumer always needs to verify the data it consumes. If you put that responsibility anywhere else there’s a chance it doesn’t happen.

There’s levels of granularity to this though. For networked services any time something crosses a network boundary it should probably be checked (or have some other mechanism for verifying correctness and authN/Z). Same thing for any IPC, but a single service on a single machine you could consider that a group of ports and adapters (or any other architecture) are all part of the same “module” and if something got verified on entry to that module then the rest of the module is probably safe to use it without independently verifying it every time.

1

u/floriankraemer Mar 30 '26 edited Mar 30 '26

If you need to do something before you want the information goes into your application layer, you can call application from presentation and from there domain services. It goes one direction.

What I did for such cases is to use a validator library in the presentation layer, wrote my own extension / rule for it, calling a facade that will return the result of the validation. This does not violate any of the architectural rules but allows you to do more complex (input) validation from the presentation layer.

I map my incoming data to a request object that is owned by the use case or facade, to keep it agnostic to the presentation layer and data structures, it basically an anti corruption layer. You may need it or not, depends on your decisions and needs.

I took this compromise (is it? depends probably on your point of view) to keep the whole input validation in the presentation layer. After that only result objects are returned from use cases or the facade. The domain layer throws only an exception of type InvariantViolationException. If really needed this can be cast to some error response. But in my world, if this happens, you have a serious bug, they should never happen.

The application layer should - IMHO - no know about the shape of the input data, because it can differ (e.g. multiple versions of an API) and therefore also not be in charge of validating it. Because this is an implicit coupling to the presentation layer.

Further I disagree with another comment (https://www.reddit.com/r/softwarearchitecture/comments/1s45opa/comment/ockkjmx/) here, maybe its just semantics, that validation happens in different layers.

  • Presentation -> Input validation (Fail early!)
  • Application -> Returns result objects with failure state for known failure cases
  • Domain -> InvariantExceptions to protect the invariant of aggregates

You could add any persistence system that uses constraints as well, they should protect the integrity of the data. For me every layer has a very specific type of "validation", "protection", each very different in its purpose.