How to get validation rules from FluentValidator
Asked Answered
J

1

9

Given a validator extending AbstractValidator and implementing IValidator, I would like to get it's rules. There seems to be no way to do this?

Jade answered 11/3, 2013 at 10:25 Comment(2)
What do you mean? Are you trying to disassemble CLR?Zirkle
No, I just want to call a method to get a validator's rules. I will post the answer.Jade
J
4

Use the GetEnumerator method from the IValidator interface.

By enumerating through the collection you can inspect the validators applied to each property.

However, it got quite complex when dealing with DelegatingValidators so instead I applied a custom state with a .WithState() onto my validators which I could easily understand.

Jade answered 12/3, 2013 at 11:25 Comment(3)
Could you provide an example?Tridactyl
Could you provide an example?Paranoia
If your IValidator is a subclass of AbstractValidator, it implements the IEnumerable interface so you can iterate over it: var validationRules = MyValidator as IEnumerable<IValidationRule>;Sluiter

© 2022 - 2024 — McMap. All rights reserved.