Skip to content

3. Validator verifiers

Michal Jankowski edited this page Jul 10, 2021 · 15 revisions

Validator verifiers are classes that are being uses to check configuration of validators. You can use one of following validator verifiers:

  • BetweenValidatorVerifier
  • ChildValidatorVerifier
  • ComparisonValidatorVerifier
  • EnumValidatorVerifier
  • EqualValidatorVerifier
  • LengthValidatorVerifier -- ExactLengthValidatorVerifier -- MaximumLengthValidatorVerifier -- MinimumLengthValidatorVerifier
  • PlaceholderVerifier
  • RegularExpressionValidatorVerifier
  • ScalePrecisionValidatorVerifier
  • TypeValidatorVerifier

BetweenValidatorVerifier

It can be used with all validators that implements IBetweenValidator interface.

It means that in unit test following values will be checked:

  • type of validator
  • value of from
  • value of to

In most cases you will check following validators:

  • Exclusive Between Validator
  • Inclusive Between Validator

Example:

// Rule
RuleFor(person => person.HeightInMeters).InclusiveBetween(0.0, 2.5);

// Direct usage
new BetweenValidatorVerifier<InclusiveBetweenValidator<Person, double>>(0.0, 2.5)

// Veryfication in unit test
personValidator.ShouldHaveRules(x => x.HeightInMeters,
  BaseVerifiersSetComposer.Build()
    .AddBetweenValidatorVerifier<InclusiveBetweenValidator<Person, double>>(0.0, 2.5)
    .Create());

ChildValidatorVerifier

Check whether type of ValidatorType form ChildValidator is correct.

ComparisonValidatorVerifier

This validator checks two things:

  • type of validator with one value to compare
  • correctness of specified comparison value

It can be used with all validators that implements IComparisonValidator interface.

In most cases you will check following validators:

  • Less Than Validator
  • Less Than Or Equal Validator
  • Greater Than Validator
  • Greater Than Or Equal Validator

Example:

// Rule
RuleFor(person => person.HeightInCentimeters).LessThanOrEqualTo(250);

// Direct usage
new ComparisonValidatorVerifier<LessThanOrEqualValidator<Person, int>, Person, int>>(250)

// Veryfication in unit test
personValidator.ShouldHaveRules(x => x.HeightInCentimeters,
  BaseVerifiersSetComposer.Build()
    .AddPropertyValidatorVerifier<LessThanOrEqualValidator<Person, int>, Person, int>>(250)
    .Create());

EnumValidatorVerifier

It verifies correctness of EnumValidator configuration. It means that in unit test following values will be checked:

  • type of validator
  • type of enum

It can be used with all validators that inherit EnumValidator class.

Example:

// Rule
RuleFor(person => person.Weight).SetValidator(new ScalePrecisionValidator(4, 2));

// Direct usage
new EnumValidatorVerifier<<Person, DayOfWeek>, Person, DayOfWeek>()

// Veryfication in unit test
personValidator.ShouldHaveRules(x => x.FavouriteDay,
  BaseVerifiersSetComposer.Build()
    .AddEnumValidatorVerifier<<Person, DayOfWeek>, Person, DayOfWeek>()
    .Create());

// Or

personValidator.ShouldHaveRules(x => x.FavouriteDay,
  BaseVerifiersSetComposer.Build()
    .AddEnumValidatorVerifier<Person, DayOfWeek>()
    .Create());

EqualValidatorVerifier

This validator checks two things:

  • type of validator with one value to compare
  • correctness of specified comparison value

In most cases you will check following validators:

  • NotEqual Validator
  • Equal Validator

Example:

// Rule
RuleFor(person => person.HeightInCentimeters).Equal(250);

// Direct usage
new EqualValidatorVerifier<EqualValidator<Person, int>, Person, int>>(250)

// Veryfication in unit test
personValidator.ShouldHaveRules(x => x.HeightInCentimeters,
  BaseVerifiersSetComposer.Build()
    .AddEqualValidatorVerifier<EqualValidator<Person, int>, Person, int>
    .Create());

LengthValidatorVerifier

It verifies correctness of LenghtValidator configuration. It means that in unit test following values will be checked:

  • type of validator
  • value of minimum length
  • value of maximum length

It can be used with all validators that implements ILengthValidator interface.

In most cases you will check following validators:

  • Length Validator

Example:

// Rule
RuleFor(person => person.FirstName).Length(0, 20);

// Direct usage
new LengthValidatorVerifier<LengthValidator<Person>>(0, 20);

// Veryfication in unit test
personValidator.ShouldHaveRules(x => x.FirstName,
  BaseVerifiersSetComposer.Build()
    .AddPropertyValidatorVerifier<LengthValidator<Person>>(0, 20)
    .Create());

You can use also more detailed verifiers:

  • ExactLengthValidatorVerifier
  • MinimumLengthValidatorVerifier
  • MaximumLengthValidatorVerifier

PlaceholderVerifier

This is quite different verifier. It does not check anything. It is used as dummy verifier. This framework checks by default number of added validators to property. You need to use PlaseholderVerifier each time when used validator is not supported by this framework. In most cases you will use this when you will be using your own custom validators. Please remember that you will need to test those custom validators separately or add extension to this framework.

Example:

// Rule
RuleFor(person => person.FirstName).NotNull().CustomValidator().Length(0, 20);

// Direct usage
new CustomVerifier();

// Veryfication in unit test
personValidator.ShouldHaveRules(x => x.FirstName,
  BaseVerifiersSetComposer.Build()
    .AddPropertyValidatorVerifier<NotNullValidator<Person, string>>()
    .AddPlaceholderVerifier()
    .AddPropertyValidatorVerifier<LengthValidator<Person>>(0, 20)
    .Create());

RegularExpressionValidatorVerifier

It verifies correctness of RegularExpressionValidator configuration. It means that in unit test following values will be checked:

  • type of validator
  • expression

It can be used with all validators that implements IRegularExpressionValidator interface.

In most cases you will check following validators:

  • Regular Expression Validator

Example:

// Rule
RuleFor(person => person.Email).SetValidator(new RegularExpressionValidator(this.regexString));

// Direct usage
new RegularExpressionValidatorVerifier<RegularExpressionValidator<Person>>("^[_a-z0-9-]+(.[a-z0-9-]+)@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})$");

// Veryfication in unit test
personValidator.ShouldHaveRules(x => x.Email,
  BaseVerifiersSetComposer.Build()
    .AddPropertyValidatorVerifier<RegularExpressionValidator<Person>>("^[_a-z0-9-]+(.[a-z0-9-]+)@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})$")
    .Create());

ScalePrecisionValidatorVerifier

It verifies correctness of ScalePrecisionValidator configuration. It means that in unit test following values will be checked:

  • type of validator
  • scale
  • precision
  • IgnoreTrailingZeros configuration

It can be used with all validators that inherit ScalePrecisionValidator class.

Example:

// Rule
RuleFor(person => person.Weight).SetValidator(new ScalePrecisionValidator(4, 2));

// Direct usage
new ScalePrecisionValidatorVerifier<ScalePrecisionValidator>(2, 4);

// Veryfication in unit test
personValidator.ShouldHaveRules(x => x.Weight,
  BaseVerifiersSetComposer.Build()
    .AddScalePrecisionValidatorVerifier<<ScalePrecisionValidator<Person>, Person>(2, 4)
    .Create());

// Or

personValidator.ShouldHaveRules(x => x.Weight,
  BaseVerifiersSetComposer.Build()
    .AddScalePrecisionValidatorVerifier<ScalePrecisionValidator<Person>, Person>(2, 4)
    .Create());

TypeValidatorVerifier

This is one of the simplest verifiers. It checks only one thing - correctness of validator type. It can be uses will all validators that implement interface IPropertyValidator. In most cases you will check following validators:

  • Credit Card validator
  • Email Validator
  • Empty Validator
  • NotEmpty Validator
  • Null Validator
  • NotNull Validator

Example:

// Rule
RuleFor(person => person.FirstName).NotNull()

// Direct usage
new TypeValidatorVerifier<NotNullValidator<Person, string>>()

// Usage in unit test
personValidator.ShouldHaveRules(x => x.FirstName,
  BaseVerifiersSetComposer.Build()
    .AddPropertyValidatorVerifier<NotNullValidator<Person, string>>()
    .Create());