I have a class Action which has a collection of more Action objects. Something like this:
public class Action
{
ICollection<Action> SubActions;
}
This basically forms a tree structure (I make sure there are no cycles). I used Fluent Validation to write a validator for this class. Here is my Validator attempt:
public class ActionValidator : AbstractValidator<Action>
{
public ActionValidator()
{
RuleFor(x => x.SubActions).SetCollectionValidator(new ActionValidator());
}
}
Unity blows up when I try to resolve anything which depends on ActionValidator. More specifically, LINQPad crashes when it tries to resolve a service which depends on ActionValidator, presumably from a stack overflow.
There are other members in my Action class that I'm validating, but I've just put the important part for brevity. If I comment out the rule I've listed here, it works fine (except it's not validating subactions anymore).
I get the problem with my approach. I'm recursively constructing validators until something dies. But I'm just not sure how I'm to tell Fluent Validation to validate sub-objects this way.
To run the validator, instantiate the validator object and call the Validate method, passing in the object to validate. Customer customer = new Customer(); CustomerValidator validator = new CustomerValidator(); ValidationResult result = validator. Validate(customer);
There is no way to debug Fluent Validator code with Visual Studio tools. You need to comment the specific part of code (RuleFor) that you want to test. Keep doing it until all rules are tested.
Fluent Validation is a validation library for . NET, used for building strongly typed validation rules for business objects. Fluent validation is one way of setting up dedicated validator objects, that you would use when you want to treat validation logic as separate from business logic.
FluentValidation is a server-side library and does not provide any client-side validation directly. However, it can provide metadata which can be applied to the generated HTML elements for use with a client-side framework such as jQuery Validate in the same way that ASP. NET's default validation attributes work.
Change the rule that validates the same type to:
Rulefor(x => x.SubActions).SetCollectionValidator(this);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With