question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it possible to add rules dynamically?

See original GitHub issue

In 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,

  1. 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.

  2. 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:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
JeremySkinnercommented, Jul 5, 2017

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.

0reactions
JeremySkinnercommented, Jul 19, 2017

@costa100 You can also use the “InlineValidator”, which allows you to create rules without defining a specific class.

var validator = new InlineValidator<Person>();
/* or new InlineValidator<Person> {
  v => v.RuleFor(x => x.Name).NotNull();
 } 
*/

validator.RuleFor(x => x.Name).NotNull();
validator.Validate(new Person()); 

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JQuery Validation including rules dynamically
1 Answer 1 ... The only way to add, change, or remove the rules dynamically is by using the .rules() methods. .rules('add') to...
Read more >
Dynamic rules - MoodleDocs
Dynamic rules allow you to define and execute centralized and automated rules using an "if this then that" conditional approach to trigger ...
Read more >
Rules for dynamically populated groups membership
Dynamic group membership adds and removes group members automatically using membership rules based on member attributes. This article details ...
Read more >
Working with Dynamic Content Rules
A rule set includes all of the rules for a dynamic content region, the order of the rules, and the logic. You can...
Read more >
Execute rules dynamically
Is there a way to execute rules in appian dynamically ? Example execute ( <<rule Name>> , Map of parameters (<<key>> , <<Value...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found