C
C#3y ago
honk

Validating an object not received during a request(FluentValidation) [Answered]

Hi, I'm trying to achieve this: I receive Id and someRequest model from an endpoint(separate ID because I'm trying to comply to REST standards and don't want to cram [FromRoute] into the request model]. I also have a different someCommand model,the takes aforementioned id and model into the constructor and creates an object that will be queried against database)This model has validation using AbstractValidation<T> class of FluentValidation. I'm wondering, would these validators even work, considering I'm not receiving this model from the request?
4 Replies
Pobiega
Pobiega3y ago
You can manually run the validation just fine
Customer customer = new Customer();
CustomerValidator validator = new CustomerValidator();

ValidationResult results = validator.Validate(customer);

if(! results.IsValid)
{
foreach(var failure in results.Errors)
{
Console.WriteLine("Property " + failure.PropertyName + " failed validation. Error was: " + failure.ErrorMessage);
}
}
Customer customer = new Customer();
CustomerValidator validator = new CustomerValidator();

ValidationResult results = validator.Validate(customer);

if(! results.IsValid)
{
foreach(var failure in results.Errors)
{
Console.WriteLine("Property " + failure.PropertyName + " failed validation. Error was: " + failure.ErrorMessage);
}
}
also, FluentValidation recommends against using the "automatic validation" style: https://docs.fluentvalidation.net/en/latest/aspnet.html so yeah, you can use manual validation when/whereever you want
honk
honkOP3y ago
Thanks! Didn't even know that bit about automatic validation!
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Accord
Accord3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server