Validating dynamic objects with their associated validators coming from different plugin assemblies
See original GitHub issueI 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:
- Created 5 years ago
- Comments:10 (5 by maintainers)
Top GitHub Comments
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’sCustomState
property. Usually used like this: