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.

Validating dynamic objects with their associated validators coming from different plugin assemblies

See original GitHub issue

I am comparatively new to FluentValidation and looking to solve the puzzle i am in. I have a complex object named, RuleApp, which has one of the collection property of abstract type SchedulerTask. Below i have given RuleApp representation by ignoring other details to keep it simple and relevant to my question.

public class RuleApp : Entity
{
    public List<SchedulerTask> Schedulers { get; set; }

    protected override IValidator GetValidator()
    {
        return new RuleAppValidator();
    }   
}

public class RuleAppValidator : AbstractValidator<RuleApp>
{
    protected RuleAppValidator()
    {
        //RuleForEach(x => x.Schedulers)
        // how to make this validator familiar with concrete type of `SchedulerTask` dynamically
    }
}

public abstract class SchedulerTask : Entity
{
}

public abstract class SchedulerTaskValidator<T> : AbstractValidator<T>
    where T : SchedulerTask
{
}

Now i have a separate plugin assembly which is loaded dynamically including concrete SchedulerTask named, FileInFileOut along with its associated validator named, FileInFileOutValidator.

public class FileInFileOut : SchedulerTask
{
    protected override IValidator GetValidator()
    {
        return new FileInFileOutValidator();
    }   
}

public class FileInFileOutValidator : SchedulerTaskValidator<FileInFileOut>
{
    public FileInFileOutValidator()
    {
        // some rules here for example
    }
}

My question is how to include the dynamically loaded FileInFileOutValidator so that when RuleApp is validated all the child SchedulerTask is validated accordingly.

Finally, i know we get the ValidationResult as an output to validate, but is there any way to get the actual instance along with it on which errors have occurred?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
JeremySkinnercommented, Dec 17, 2018
public MyValidator() {
    RuleFor(x => x.Foo).Whatever();
   //...other rules...

  foreach (var rule in this) {
    if (rule is PropertyRule ruleChain) {
      foreach (var validator in ruleChain.Validators) {
        validator.Options.CustomStateProvider = ctx => ctx.Instance;
      }
    }
  }
}
1reaction
JeremySkinnercommented, Dec 16, 2018

The ValidationResult is designed to be a simple easily serializable representation of the validation failures and intentionally doesn’t contain a reference back the original instance. That being said you should be able do this by using the WithState method in the rule chain. This allows you to associate any arbitrary state with the validation result which can then be accessed from on each failure’s CustomState property. Usually used like this:

// The lambda's parameter is the item being va
RuleFor(x => x.Name).NotNull().WithState(x => x); 
var result = validator.Validate(...);
foreach(var error in result.Errors) {
   // access via error.CustomState
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Validation of nested dynamic reactive form - angular
I have a dynamic reactive form where a new form group is being added when you click on a button. How do I...
Read more >
Thoughts on validation in ASP.NET MVC applications
1 says that model objects don't merely hold validation rules (e.g., as attributes on their properties), but model objects also actually ...
Read more >
16 steps to write flexible business validation in C# using ...
config file. First step create the customer object and set the property. In the second step we tell to the validation static class...
Read more >
How to use FluentValidation in ASP.NET Core Apps (.Net 6)
Learn how to implement FluentValidation in your ASP.NET Core (.NET 6) applications to build strongly-typed validation rules, the easy way.
Read more >
Dynamic type validation in TypeScript
Combining both entities, the result is a validated type object. Dynamic type validation allows a type to generate a validator from its ......
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