DEV Community

Discussion on: Data constraints: database layer or app logic?

Collapse
 
shalvah profile image
Shalvah • Edited

Thanks for sharing, Dian. Regarding point 1, I would absolutely hate to tell the user "Error: Couldn't save data" because I don't know the exact error. If the constraint was checked by the app, though, I would know the exact constraint that failed, and I can advise the user accordingly ("That username is taken").
I'm thinking it's best to enforce constraints in both places.

Collapse
 
dmfay profile image
Dian Fay

Yeah, you need validation on the request before it makes it to the database. What the user enters isn't always what the database stores anyway. It'd be nice if data layers could raise precise errors in a general enough format to make communicating them to the user easy in any scenario but that'd require a lot of stuff to line up just so.