C
C#•2y ago
barcode

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
barcode
barcode•2y ago
this is what im trying to do
barcode
barcode•2y ago
Saber
Saber•2y ago
That kind of stuff really shouldn't exist in a validator
Mayor McCheese
Mayor McCheese•2y ago
I was typing this same statement 🙂
barcode
barcode•2y ago
ty, I saw mustAsync and thought it is being used for that https://docs.fluentvalidation.net/en/latest/async.html
Mayor McCheese
Mayor McCheese•2y ago
can you vs should you
barcode
barcode•2y ago
should I?
Mayor McCheese
Mayor McCheese•2y ago
probably not
barcode
barcode•2y ago
got it ty
Mayor McCheese
Mayor McCheese•2y ago
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.
barcode
barcode•2y ago
it just seems like a waste to check if city exists in validator by literally running a sql query to get it
Saber
Saber•2y ago
assuming this in the context of an api, it says a good reason to not do it
barcode
barcode•2y ago
and then in controller to get it again i'm not using automatic
Saber
Saber•2y ago
Any of those checks to validate if the entity exists, I handle in my code. Validators are the simple length, null, etc checks
Mayor McCheese
Mayor McCheese•2y ago
fluent validation doesn't maintain the automatic validation for asp.net any longer iirc
barcode
barcode•2y ago
ValidationResult result = await _createTenderDtoValidator.ValidateAsync(createTenderDto);
if (!result.IsValid)
return Validator.BadRequestErrorResponse(result.Errors);
ValidationResult result = await _createTenderDtoValidator.ValidateAsync(createTenderDto);
if (!result.IsValid)
return Validator.BadRequestErrorResponse(result.Errors);
done like this now another question is it ok to have static classes in apis or should they be singletons?
Mayor McCheese
Mayor McCheese•2y ago
yesn't
barcode
barcode•2y ago
rn I have this static class to generate error responses
barcode
barcode•2y ago
barcode
barcode•2y ago
i dont think it would make any sense for it not to be static sincei t doesnt have state
Mayor McCheese
Mayor McCheese•2y ago
I'm not opposed to it personally; but your class name doesn't really apply imho
barcode
barcode•2y ago
yeah ill rename it oops it did something different b4
Mayor McCheese
Mayor McCheese•2y ago
like you said there is no state; but I think fluent validation already has a response type doesn't it?
barcode
barcode•2y ago
nope in manual u have to return ur own i am guessing it has its own in automatic
Mayor McCheese
Mayor McCheese•2y ago
I mean really; I'd just create a new response type that took errors in the ctor what is the static class getting you?
barcode
barcode•2y ago
it just holds the function that turns fluent errors into a response I handle in my frontend
Mayor McCheese
Mayor McCheese•2y ago
why not have your response just able to handle that?
barcode
barcode•2y ago
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