Fluent validation
Hello, I am using fluent validation and I'm trying to validate that the user supplied city is valid, to validate it I am getting it from the dbcontext and checking if it exists, I also want to assign it to a variable inside the object but I am unable to get the object that is being validates, does anyone know how to do this?
28 Replies
this is what im trying to do
That kind of stuff really shouldn't exist in a validator
I was typing this same statement 🙂
ty, I saw mustAsync and thought it is being used for that
https://docs.fluentvalidation.net/en/latest/async.html
can you vs should you
should I?
probably not
got it
ty
reasons are many, but you have to start to make all sorts of guarantees about what dependencies are available in your validator.
that doesn't mean you shouldn't validate; but you may have a set of other business rules outside of model validators.
it just seems like a waste to check if city exists in validator by literally running a sql query to get it
assuming this in the context of an api, it says a good reason to not do it
and then in controller to get it again
i'm not using automatic
Any of those checks to validate if the entity exists, I handle in my code. Validators are the simple length, null, etc checks
fluent validation doesn't maintain the automatic validation for asp.net any longer iirc
done like this now
another question
is it ok to have static classes in apis or should they be singletons?
yesn't
rn I have this static class to generate error responses
i dont think it would make any sense for it not to be static sincei t doesnt have state
I'm not opposed to it personally; but your class name doesn't really apply imho
yeah ill rename it oops
it did something different b4
like you said there is no state; but I think fluent validation already has a response type doesn't it?
nope in manual u have to return ur own
i am guessing it has its own
in automatic
I mean really; I'd just create a new response type that took errors in the ctor
what is the static class getting you?
it just holds the function
that turns fluent errors into a response I handle in my frontend
why not have your response just able to handle that?
oh I see fluent literally has the same structure as me
with different names
oops
i didnt like the data annotation ones
so I made a class to turn it into another format
and when i switched to fluent i just kept the func and refactored it