Is it possible to add rules dynamically?
See original GitHub issueIn the solution I am developing using Asp.Net Core (.Net Framework version), I need to add validation rules for given type dynamically, which means that–
For Example–
I have a model, let’s say Customer as below:
public class Customer
{
public string Name { get; set; }
public string City { get; set; }
public string Zipcode { get; set; }
}
Now,
-
I want to add validation rules for given type dynamically. Which means that- I want add validation rules reading from database. To do that- I have all the all the information related to type and its properties I have stored in database. I just want to iterate through it and want to apply rules dynamically.
-
For certain case, considering the above model, Name and City are required. For certain case, the City and Zipcode are required.
To do so, when actually model validation happens, I have some flag in my application. I will pass the flag to database while querying the database to retrieve the eligible properties to be validated. For some other flag, the eligible properties will be different for the same model.
Would you mind to share your insight on this for how would I validate the fields dynamically by adding the rules dynamically based on preference?
Thanks in advance!
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
FluentValidation really isn’t the right product for doing this. The entire point of FluentValidation is to define rules in a strongly-typed, intellisense-friendly way where the rules are defined at compile-time. It’s just not designed for building dynamic rules…you should really be using something else.
@costa100 You can also use the “InlineValidator”, which allows you to create rules without defining a specific class.
I’m going to close this issue as the original question was about dynamic rules, which is a completely separate thing. If you need help on using the InlineValidator please open a separate issue.