Is it necessary to setup validation constraints at the api level and database level?

I'm setting up a minimal api and just started to use FluentValidation. Should I also setup validation constraints at the database level as well?
3 Replies
Sossenbinder
Sossenbinder2mo ago
The database layer should ideally deal with persistence details, I think validating something should primarily take place in your api layer as well as your domain code in my opinion In the base case everything which gets in and out of your db already went through validation, and your db can take care of mapping your domain objects into their persistence representation, including all the database details So tldr, it depends, but in the way I commonly understand the layers people use, I think the persistence layer has other responsibilities
nathanAjacobs
nathanAjacobsOP2mo ago
Ty for the response. Yeah I was thinking, as long as the data only gets into the database through the validation code on the application layer then it shouldn't be necessary at the db level. I was more curious what the "standard" way was
Tvde1
Tvde12mo ago
Unique constraints on database level are still useful for concurrency issues or if a bug in the business logic exists
Want results from more Discord servers?
Add your server