Hi Team ,
.NET core API Fluent Validation Dynamically how set in model class and difference condition is it possible ?any example article or resources please share
Hi Team ,
.NET core API Fluent Validation Dynamically how set in model class and difference condition is it possible ?any example article or resources please share
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jaimin ShethiyaRank #177 · 13.7K reputationPosted Jun 26, 2024, 11:26 PM EDT
Hello Jobin,
Can you more elaborate your requirement so we are understand and give an idea.
Thanks
Jobin SRank #886 · 2.4K reputationPosted Jun 27, 2024, 12:45 PM EDT
Dynamically validation set in request model class options here in Fluent validation
Jaimin ShethiyaRank #177 · 13.7K reputationPosted Jun 27, 2024, 10:04 PM EDT
Hello Jobin,
You mean to say based on the request, automatically identified the fluent validation class and validate that request.
This is my understanding correct?
Jobin SRank #886 · 2.4K reputationPosted Jun 27, 2024, 10:38 PM EDT
yes jaimin ,
request validate in dynamically ? any options here fluent validation?
Prasad RaveendranRank #320 · 7.8K reputationPosted Jun 30, 2024, 3:08 PM EDT
Hello Jubin,
I would appreciate if you could provide more insights on how you are expecting fluent validation dynamically set on model classes.
Jaimin ShethiyaRank #177 · 13.7K reputationPosted Jun 28, 2024, 11:45 AM EDT
Hello Jobin,
You need to use a reflection and try to create an object and invoke that method.
I used this in my one of the organisation.
var type = Type.GetType("Your class name space"); object? instance = null; if (type != null) { instance = Activator.CreateInstance(type, _loggerFactory); } return instance; or instance.MethodName();Try this way,
Hope it will help you.
Thanks
Jaimin ShethiyaRank #177 · 13.7K reputationPosted Jun 29, 2024, 10:07 AM EDT
Hello Jobin,
Hope below code good for you.
var employee = new Employee { }; var result = Validate("FluentValidationDemo.Validator.EmployeeValidator", employee); Console.WriteLine($"{nameof(Employee)} validation"); foreach (var item in result.Errors) { Console.WriteLine(item.ErrorMessage); } Console.WriteLine(); Console.WriteLine($"{nameof(Student)} validation"); var student = new Student { }; result = Validate("FluentValidationDemo.Validator.StudentValidator", student); foreach (var item in result.Errors) { Console.WriteLine(item.ErrorMessage); } FluentValidation.Results.ValidationResult Validate(string typeName, object data) { var type = Type.GetType(typeName); object? instance = null; if (type != null) { instance = Activator.CreateInstance(type); } var methods = type.GetMethods(); var method = methods.FirstOrDefault(x => x.IsFinal && x.Name == "Validate"); var response = method.Invoke(instance, [data]); var result = response as FluentValidation.Results.ValidationResult; ArgumentNullException.ThrowIfNull(result); return result; }Output
Thanks