C
C#•10mo ago
Pobiega

Backend Validations

Validate the DB? Can you explain the reasoning there?
47 Replies
CurseOfLife
CurseOfLife•10mo ago
data being inputed into the db.. indexes, required, lenght that sort of stuff@Pobiega db protecting itself from bad stuff reasoning 🙂 ex. u could validate if a required prop exists in the dto and let the db handle rest. or validate in the controller (some do service) and make it so nothing goes down ive seen different opinions so i ask here too
Pobiega
PobiegaOP•10mo ago
you certainly shouldnt need that, unless you are inserting into DB from outside EF
CurseOfLife
CurseOfLife•10mo ago
i mean this is a validation
No description
CurseOfLife
CurseOfLife•10mo ago
using ef to configure a model
Pobiega
PobiegaOP•10mo ago
that is not a validation thats an entity configuration
CurseOfLife
CurseOfLife•10mo ago
yes but u can configure it that it has max 40 if u try to enter 41 the db is gonna say no its a validation
Pobiega
PobiegaOP•10mo ago
its not thou
CurseOfLife
CurseOfLife•10mo ago
so the question is maybe what do we leave to the db and what take outside
Pobiega
PobiegaOP•10mo ago
thats a more interesting question
CurseOfLife
CurseOfLife•10mo ago
basically what i was asking but put in 1 line 😄
Pobiega
PobiegaOP•10mo ago
your database schema (thats what configuring your models does) prevents bad data, but only by causing DbUpdateExceptions, which is often unideal so do that for shit that "this shouldnt be possible"
CurseOfLife
CurseOfLife•10mo ago
i thought about it lil while i was outside this is the thought process that i had ok am i building the client and there isnt going to be any other client. so 1 of each (db, api, client) am i going to have multiple clients 1 api multiple api using the same db might come down to that where does it get placed even multiple db single api if im building my own that im going to use, or other ppl gonna connect to it.. i protect my stuff dont care what others do 🤔
Anton
Anton•10mo ago
this is a really great idea, and I've done it once there's no library for this, you have to do something custom
CurseOfLife
CurseOfLife•10mo ago
entity framework has annotations and fluent api to do it
Anton
Anton•10mo ago
on the db level not on the api level
CurseOfLife
CurseOfLife•10mo ago
db level u go to your dbms and do it 😄 code first so efcore
Anton
Anton•10mo ago
this is bad
CurseOfLife
CurseOfLife•10mo ago
thats not the problem, there are complex validations which are pretty much business rules
Anton
Anton•10mo ago
your frontend needs to have this info you want to propagate this all the way to the frontend so you don't duplicate your configuration in two places the max length should end up in the swagger schema
CurseOfLife
CurseOfLife•10mo ago
ex. we have 50 in stock tenant 1 can order 60 tenant 2 cant order above max thats a business rule that needs to be validated
Anton
Anton•10mo ago
it doesn't check it btw, it just truncates at least most dbs just truncate
CurseOfLife
CurseOfLife•10mo ago
🤔
Anton
Anton•10mo ago
I mean, this does need specific code
CurseOfLife
CurseOfLife•10mo ago
yy
Anton
Anton•10mo ago
but if it's something like the string length you want it to only be configured for the db and still reach the api schema
CurseOfLife
CurseOfLife•10mo ago
y i figured using ef core to setup the db fully and fluidvalidations to do business
Anton
Anton•10mo ago
so you either pull it from the ef core db model or duplicate it on the dtos
CurseOfLife
CurseOfLife•10mo ago
y thought of duplicate
Anton
Anton•10mo ago
that's what I managed to pull from the db model once
CurseOfLife
CurseOfLife•10mo ago
looking at the db as a separate entity that any api can access the db wont allow bs to be entered and the api needs to know whats allowed the api dude can duplicate the validations so the request doesnt waste time going to db the moment it finds a mistake throws an exception or sends a msg thinking out loud rn junior still 🙂
Anton
Anton•10mo ago
the idea is to: 1. have a mapping between db entity and dto properties to be able to know which column to pull the max length from (I just did it by name, ideally a mapper library should provide this metadata, but none of them do); 2. use some undocumented MVC thing to add a validation handler to those properties for that type; 3. make a schema filter that knows how to interpret this MVC validation handler as MaxLength; 4. inject db model into the MVC thing that sets up validation to do the property mapping and pull the max length information. that's how I did it at least with fluent validation it's going to be more involved, because you can't transparently inject dependencies into them it is kind of crazy that there's no library for this seems to me like a must have idea
CurseOfLife
CurseOfLife•10mo ago
@Antondont know if i misunderstood u can DI anything into the Validators constructor
Anton
Anton•10mo ago
I know
CurseOfLife
CurseOfLife•10mo ago
ah ok ok
Anton
Anton•10mo ago
but you can't say "all validators now have this" you have to do that manually for each one
CurseOfLife
CurseOfLife•10mo ago
understood an issue i see is i gotta write it into every single endpoint read something about filters need to research more, says not built in dont rly want to keep adding dependencies on libraries
Anton
Anton•10mo ago
the thing I did applies globally but it uses the data annotation validation system from MVC just piggybacks off of it
CurseOfLife
CurseOfLife•10mo ago
reading this rn
No description
Anton
Anton•10mo ago
you can do this, but usually you'd often want to do it at a certain point you control or have other context around before you do it I actually found it helpful to do the more complex validation manually, with a custom error context that I can push errors to
CurseOfLife
CurseOfLife•10mo ago
problem is currenty the project im on i joined this month the others working on it couple months
Anton
Anton•10mo ago
because then I can interleave validation with pulling context from the db
CurseOfLife
CurseOfLife•10mo ago
i got thrown the validation bone with no info 😄
Anton
Anton•10mo ago
and you do need info from the db to validate stuff properly
CurseOfLife
CurseOfLife•10mo ago
i got no info on requirements/business rules so im going blind rn just researching talking more generaly bec of it without examples figuring out where stuff would go
Anton
Anton•10mo ago
in my experience, there's two sorts of validation one is trivial like max length min max enum parses or not must not be null these should be automatic and then the other kind where you want to validate business rules (like the thing you did with the length based on other stuff in the db), ownership of entities and limit certain things based on permissions imo these should be done manually, when it's the best time for these when all the context is around, and you're about to do the changes
CurseOfLife
CurseOfLife•10mo ago
understand so controller if controller got all to pass, service if using that architecture where controller is smol, aggregate 🤔 i get the dto and on that place in my architecture i have all the stuff i need from the background at the highest point 🤔 may be mapped already
Anton
Anton•10mo ago
I have no idea what you just said

Did you find this page helpful?