Skip to content

2.5. String maximum and minimum Length validations

hugoj0s3 edited this page Apr 23, 2018 · 4 revisions

Adding StringMinimumLengthValidation and StringMaximumLengthValidation. In this example below the first name must have at least 3 characters and can not have more than 100 characters.

const string minumumFormatMessage = "{0} must have at least {1} characters";
// {0} will be replaced by nameof(Person.FirstName)
// {1} will be replaced by minumum lenght passed
const string maximumFormatMessage = "{0} must not have more than {1} characters";
// {0} will be replaced by nameof(Person.FirstName)
// {1} will be replaced by maximum lenght passed

private ICompositeValidation<Person> validations = new CompositeValidation<Person>()
  .MinimumLength(nameof(Person.FirstName), p => p.FirstName, 3, minumumFormatMessage)
  .MaximumLength(nameof(Person.FirstName), p => p.FirstName, 100, maximumFormatMessage);
// Minimum Length is 3 and Maximum Length is 100