A big set of custom validators for popular FluentValidation library for a daily usage
Install a FluentValidation.Extensions nuget package
A list of validators:
- String validators
- Boolean validators
- Collection validators
- HasElements
- HasNoElements
- HasCountOfElements
- HasElementsMoreThan
- HasElementsMoreOrEqualThan
- HasElementsLessThan
- HasElementsLessOrEqualThan
- HasElementsBetweenExclusive
- HasElementsBetweenInclusive
- HasEvenCountOfElements
- HasOddCountOfElements
- IsUnique
- HasDuplicates
- NoNullElements
- AllMatch
- AnyMatch
- Contains
- DoesNotContain
- AllElementsOfType
- DateTime validators
- URL validators
Validates that the string contains a specified substring.
RuleFor(x => x.StringValue).Contains("foo");
Validates that the string does not contain a specified substring.
RuleFor(x => x.StringValue).DoesNotContain("foo");
Validates that the string starts with a specified substring.
RuleFor(x => x.StringValue).StartsWith("foo");
Validates that the string ends with a specified substring.
RuleFor(x => x.StringValue).EndsWith("foo");
Validates that the string does not start with a specified substring.
RuleFor(x => x.StringValue).DoesNotStartWith("foo");
Validates that the string does not end with a specified substring.
RuleFor(x => x.StringValue).DoesNotEndWith("foo");
Validates that the length of the string is between the specified minimum and maximum.
RuleFor(x => x.StringValue).LengthBetween(5, 10);
Validates that the string consists only of numeric characters.
RuleFor(x => x.StringValue).BeNumeric();
Validates that the string consists only of Latin letters.
RuleFor(x => x.StringValue).BeLatinLetters();
Validates that the string does not contain any special characters.
RuleFor(x => x.StringValue).NoSpecialCharacters();
If some special characters are allowed they can be passed inside a string parameter without delimeters:
RuleFor(x => x.StringValue).NoSpecialCharacters("_.");
Validates that a value equals to true.
RuleFor(x => x.BooleanValue).BeTrue();
Validates that a value equals to false.
RuleFor(x => x.BooleanValue).BeFalse();
Validates that a collection has elements.
RuleFor(x => x.CollectionProperty).HasElements();
Validates that a collection has no elements.
RuleFor(x => x.CollectionProperty).HasNoElements();
Validates that a collection has a given count of elements.
RuleFor(x => x.CollectionProperty).HasCountOfElements(5);
Validates that a collection has more than N elements.
RuleFor(x => x.CollectionProperty).HasElementsMoreThan(5);
Validates that a collection has equal or more than N elements.
RuleFor(x => x.CollectionProperty).HasElementsMoreOrEqualThan(5);
Validates that a collection has less than N elements.
RuleFor(x => x.CollectionProperty).HasElementsLessThan(5);
Validates that a collection has equal or less than N elements.
RuleFor(x => x.CollectionProperty).HasElementsLessOrEqualThan(5);
Validates that a collection has count of elements between N and M exclusively.
RuleFor(x => x.CollectionProperty).HasElementsBetweenExclusive(5, 10);
Validates that a collection has count of elements between N and M inclusively..
RuleFor(x => x.CollectionProperty).HasElementsBetweenInclusive(5, 10);
Validates that a collection has an even count of elements.
RuleFor(x => x.CollectionProperty).HasEvenCountOfElements();
Validates that a collection has an odd count of elements.
RuleFor(x => x.CollectionProperty).HasOddCountOfElements();
Validates that a collection has all unique elements.
RuleFor(x => x.CollectionProperty).IsUnique();
Validates that a collection has at least one duplicate element.
RuleFor(x => x.CollectionProperty).HasDuplicates();
Validates that a collection doesn't have null elements.
NOTE: this method treats default values of objects as null elements, like 0 for numbers and false for boolean
RuleFor(x => x.CollectionProperty).NoNullElements(5;
Validates that all elements of collection match the specified condition.
RuleFor(x => x.Persons).AllMatch(x => x.Age > 18);
Validates that any element of collection matches the specified condition.
RuleFor(x => x.Persons).AnyMatch(x => x.Age > 18);
Validates that a collection contains a specified value.
RuleFor(x => x.CollectionProperty).Contains(5);
Validates that a collection has doesn't contain a specified value.
RuleFor(x => x.CollectionProperty).DoesNotContain(5);
Validates that a collection has all objects of the specified type.
public class TestClass
{
public List<object> ObjectItems { get; set; } = new();
}
RuleFor(x => x.ObjectItems).AllElementsOfType(typeof(string));
Validates that the date is in the future.
RuleFor(x => x.DateValue).BeInFuture();
Validates that the date is in the past.
RuleFor(x => x.DateValue).BeInPast();
Validates that the date is within a leap year.
RuleFor(x => x.DateValue).BeLeapYear();
Validates that the date falls on a weekday.
RuleFor(x => x.DateValue).BeWeekday();
Validates that the date falls on a weekend.
RuleFor(x => x.DateValue).BeWeekend();
Validates that the date falls on a specified day of the week.
RuleFor(x => x.DateValue).BeSpecificDayOfWeek(DayOfWeek.Monday);
Validates that the date is within a specified range.
RuleFor(x => x.DateValue).BeWithinRange(startDate, endDate);
Validates that the date is in a specific month.
RuleFor(x => x.DateValue).BeSpecificMonth(month);
Validates that the date is on a specific day of the month.
RuleFor(x => x.DateValue).BeSpecificDay(day);
Validates that the date has an exact time of day.
RuleFor(x => x.DateValue).BeExactTimeOfDay(new TimeSpan(hour, minute, second));
Validates that the date is in UTC.
RuleFor(x => x.DateValue).IsUtc();
Validates that the date is local time.
RuleFor(x => x.DateValue).IsLocalTime();
Validates that the string is a valid absolute URL.
RuleFor(x => x.UrlValue).IsAbsoluteUrl();
Validates that the string is a valid relative URL.
RuleFor(x => x.UrlValue).IsRelativeUrl();
A big shoutout to the original FluentValidation library and its author for creating such a fantastic library. This package is built upon the foundation laid by the original library, and I am very grateful for the inspiration and the work put into it. Thank you!
If you find this package helpful, consider supporting my work by buying me a coffee ☕!
Your support is greatly appreciated and helps me continue developing and maintaining this project.
You can also give me a ⭐ on github to make my package more relevant to others.
Thank you for your support!