/Modern.FluentValidation.Extensions

A big set of custom validators for popular FluentValidation library for a daily usage

Primary LanguageC#Apache License 2.0Apache-2.0

Build & Tests

Modern.FluentValidation.Extensions

A big set of custom validators for popular FluentValidation library for a daily usage

Installation

Install a FluentValidation.Extensions nuget package

Features

A list of validators:

String validators

Contains

Validates that the string contains a specified substring.

RuleFor(x => x.StringValue).Contains("foo");

DoesNotContain

Validates that the string does not contain a specified substring.

RuleFor(x => x.StringValue).DoesNotContain("foo");

StartsWith

Validates that the string starts with a specified substring.

RuleFor(x => x.StringValue).StartsWith("foo");

EndsWith

Validates that the string ends with a specified substring.

RuleFor(x => x.StringValue).EndsWith("foo");

DoesNotStartWith

Validates that the string does not start with a specified substring.

RuleFor(x => x.StringValue).DoesNotStartWith("foo");

DoesNotEndWith

Validates that the string does not end with a specified substring.

RuleFor(x => x.StringValue).DoesNotEndWith("foo");

LengthBetween

Validates that the length of the string is between the specified minimum and maximum.

RuleFor(x => x.StringValue).LengthBetween(5, 10);

BeNumeric

Validates that the string consists only of numeric characters.

RuleFor(x => x.StringValue).BeNumeric();

BeLatinLetters

Validates that the string consists only of Latin letters.

RuleFor(x => x.StringValue).BeLatinLetters();

NoSpecialCharacters

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("_.");

Boolean validators

BeTrue

Validates that a value equals to true.

RuleFor(x => x.BooleanValue).BeTrue();

BeFalse

Validates that a value equals to false.

RuleFor(x => x.BooleanValue).BeFalse();

Collection validators

HasElements

Validates that a collection has elements.

RuleFor(x => x.CollectionProperty).HasElements();

HasNoElements

Validates that a collection has no elements.

RuleFor(x => x.CollectionProperty).HasNoElements();

HasCountOfElements

Validates that a collection has a given count of elements.

RuleFor(x => x.CollectionProperty).HasCountOfElements(5);

HasElementsMoreThan

Validates that a collection has more than N elements.

RuleFor(x => x.CollectionProperty).HasElementsMoreThan(5);

HasElementsMoreOrEqualThan

Validates that a collection has equal or more than N elements.

RuleFor(x => x.CollectionProperty).HasElementsMoreOrEqualThan(5);

HasElementsLessThan

Validates that a collection has less than N elements.

RuleFor(x => x.CollectionProperty).HasElementsLessThan(5);

HasElementsLessOrEqualThan

Validates that a collection has equal or less than N elements.

RuleFor(x => x.CollectionProperty).HasElementsLessOrEqualThan(5);

HasElementsBetweenExclusive

Validates that a collection has count of elements between N and M exclusively.

RuleFor(x => x.CollectionProperty).HasElementsBetweenExclusive(5, 10);

HasElementsBetweenInclusive

Validates that a collection has count of elements between N and M inclusively..

RuleFor(x => x.CollectionProperty).HasElementsBetweenInclusive(5, 10);

HasEvenCountOfElements

Validates that a collection has an even count of elements.

RuleFor(x => x.CollectionProperty).HasEvenCountOfElements();

HasOddCountOfElements

Validates that a collection has an odd count of elements.

RuleFor(x => x.CollectionProperty).HasOddCountOfElements();

IsUnique

Validates that a collection has all unique elements.

RuleFor(x => x.CollectionProperty).IsUnique();

HasDuplicates

Validates that a collection has at least one duplicate element.

RuleFor(x => x.CollectionProperty).HasDuplicates();

NoNullElements

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;

AllMatch

Validates that all elements of collection match the specified condition.

RuleFor(x => x.Persons).AllMatch(x => x.Age > 18);

AnyMatch

Validates that any element of collection matches the specified condition.

RuleFor(x => x.Persons).AnyMatch(x => x.Age > 18);

Contains element

Validates that a collection contains a specified value.

RuleFor(x => x.CollectionProperty).Contains(5);

DoesNotContain element

Validates that a collection has doesn't contain a specified value.

RuleFor(x => x.CollectionProperty).DoesNotContain(5);

AllElementsOfType

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));

DateTime validators

BeInFuture

Validates that the date is in the future.

RuleFor(x => x.DateValue).BeInFuture();

BeInPast

Validates that the date is in the past.

RuleFor(x => x.DateValue).BeInPast();

BeLeapYear

Validates that the date is within a leap year.

RuleFor(x => x.DateValue).BeLeapYear();

BeWeekday

Validates that the date falls on a weekday.

RuleFor(x => x.DateValue).BeWeekday();

BeWeekend

Validates that the date falls on a weekend.

RuleFor(x => x.DateValue).BeWeekend();

BeSpecificDayOfWeek

Validates that the date falls on a specified day of the week.

RuleFor(x => x.DateValue).BeSpecificDayOfWeek(DayOfWeek.Monday);

BeWithinRange

Validates that the date is within a specified range.

RuleFor(x => x.DateValue).BeWithinRange(startDate, endDate);

BeSpecificMonth

Validates that the date is in a specific month.

RuleFor(x => x.DateValue).BeSpecificMonth(month);

BeSpecificDay

Validates that the date is on a specific day of the month.

RuleFor(x => x.DateValue).BeSpecificDay(day);

BeExactTimeOfDay

Validates that the date has an exact time of day.

RuleFor(x => x.DateValue).BeExactTimeOfDay(new TimeSpan(hour, minute, second));

IsUtc

Validates that the date is in UTC.

RuleFor(x => x.DateValue).IsUtc();

IsLocalTime

Validates that the date is local time.

RuleFor(x => x.DateValue).IsLocalTime();

URL validators

IsAbsoluteUrl

Validates that the string is a valid absolute URL.

RuleFor(x => x.UrlValue).IsAbsoluteUrl();

IsRelativeUrl

Validates that the string is a valid relative URL.

RuleFor(x => x.UrlValue).IsRelativeUrl();

Acknowledgments

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!

Support My Work 🌟

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.

Buy me a coffee

Thank you for your support!