-
Notifications
You must be signed in to change notification settings - Fork 7
2. Configuration creation
FluentValidation.Validators.UnitTestExtension allows you to build test configuration for FlunetValidation rules. You can prepare test configuration in two ways:
- by using BaseVerifiersSetComposer
- creation array of
IValidatorVerifier
I recommend to use first apporach beacuse it is more comfortable than second one.
BaseVerifiersSetComposer is class that provide generic methods for configuration construction. You cannot create instance of this class directly. Instead of constructir you should use static method Build(). This method should be used each time when you need to create new configuration.
After that you can use one of following methods for adding validation verifiers:
-
AddPropertyValidatorVerifier<TPropertyValidator>()-TPropertyValidatorshould implementIPropertyValidator -
AddPropertyValidatorVerifier<TLengthValidator>(int min, int max)-TLengthValidatorshould implementILengthValidator -
AddPropertyValidatorVerifier<TRegularExpressionValidator>(string expression)-TRegularExpressionValidatorshould implementIRegularExpressionValidator AddPropertyValidatorVerifier<TRegularExpressionValidator>(string expression, RegexOptions options)AddPropertyValidatorVerifier<TRegularExpressionValidator>(Regex regex)-
AddEqualValidatorVerifier<TEqualValidator, T, TProperty>(object valueToCompare, Comparison? comparison, MemberInfo memberToCompare)- verifiesEqualValidatororNotEqualValidator -
AddComparisonValidatorVerifier<TComparisonValidator, T, TProperty>(object valueToCompare, Comparison? comparison, MemberInfo memberToCompare)- verifiesLessThanValidatororLessThanOrEqualValidatororGreaterThanValidatororGreaterThanOrEqualValidator AddBetweenValidatorVerifier<TBetweenValidator>(IComparable from, IComparable to)- `AddPrecisionScaleValidatorVerifier<TPrecisionScaleValidator, T>(int scale, int precision, bool ignoreTrailingZeros) - TPrecisionScaleValidator should inherit from PrecisionScaleValidator
AddEnumValidatorVerifier<TEnumValidator, T, TProperty>()AddChildValidatorVerifier<T, TProperty>()AddExactLengthValidatorVerifier<T>(int length)AddMinimumLengthValidatorVerifier<T>(int min)AddMaximumLengthValidatorVerifier<T>(int max)-
AddVerifier(IValidatorVerifier ruleVerifier)- allows usage of your own implementer Verifier -
AddPlaceholderVerifier()- insterts dummy verifier that does not check anything
As type you must insert one of validators provided by FluentValidation or your validator that you have implemented by yourself. Togheter with that you are able to instert also parameters when provided validator type needs it.
And finally you must execute Create() method. It transfomrs configuration into IValidatorVerifier array.
Such created configuration can be used in unit tests.
BaseVerifiersSetComposer.Build()
.AddPropertyValidatorVerifier<NotNullValidator<Person, string>>()
.AddPropertyValidatorVerifier<NotEmptyValidator<Person, string>>()
.AddPropertyValidatorVerifier<LengthValidator<Person>>(0, 20)
.Create());Other option is to create configuration manually by providing array of objects that implements IValidatorVerifier interface.
List of already implemented verifiers can be found under following link: Validator verifiers.
new IValidatorVerifier[]
{
new TypeValidatorVerifier<NotNullValidator<Person, string>>(),
new TypeValidatorVerifier<NotEmptyValidator<Person, string>>(),
new LengthValidatorVerifier<LengthValidator<Person>>(0, 20)
});