Mad.Validation

Compose validation rules with C#.

ToValidation() extension

Import the namespace using Mad.Validation; and you can call .ToValidation() on all types of objects and variables. You can then start chaining validation rules like

    email.ToValidation()
        .Required()
        .MustContain("@");
To run the validation and to get a result, you call the .ToResult()


ValidationList

Most of the time you are validating multiple variables. For this you can use the ValidationList class. Example:

    new ValidatioList()
        .Add(account.Id.ToValidation().Required())
        .Add(account.Username.ToValidation().Required())
        .Add(account.Password.ToValidation().Required()).ToResult();


Null

By default null is not valid. If you want your validation rule to accept null then you have to add an acception like this firstName.ToValidation().AcceptNull(). You also have some extra accpetions for strings, .AcceptEmpty() and .AcceptEmptyOrWhitespace().


Validation Rules

Lists of the different vaidation rules.


All Types
EqualToValue must be equal to a value..EqualTo(someValue)
NotEqualToValue must not be equal to a value..NotEqualTo(someValue)
OneOfIs one of the items in a collection..OneOf(new [] { "val1", "val2", "val3" })
NotOneOfIs not one of the items in a collection..NotOneOf(new [] { "val1", "val2", "val3" })
ExpressionMustBeTrueCreate a custom lambda expression that must be true..ExpressionMustBeTrue(v => v.Length == 5)
ExpressionMustBeFalseCreate a custom lambda expression that must be false..ExpressionMustBeFalse(v => v.Length == 5)

IComparable Types numbers, dates & etc
MinThe minimum allowed value..Min(10)
MaxThe maximum allowed value..Max(100)
BetweenA value is equal or between two values..Between(10, 100)
GreaterThanA value greater than..GreaterThan(5)
LessThanA value less than..LessThan(8)

ICollection Types
MustBeEmptyCollection must be empty..MustBeEmpty()
CantBeEmptyCollection can't be empty..CantBeEmpty()
MinCountMinimum items in a collection..MinCount(10)
MaxCountMaximimum items in a collection..MaxCount(100)
CountBetweenThe number of items must be equal or between two values..CountBetween(10, 100)

Bool Types
MustBeTrueValue must be true..MustBeTrue()
MustBeFalseValue must be false..MustBeFalse()

String Types
RequiredString is required and can't be empty or whitespace..Required()
NotEmptyString cant be empty..NotEmpty()
NotEmptyOrWhitespaceString can't be empty or whitespace..NotEmptyOrWhitespace()
MinLengthMinimum length of a string..MinLength(10)
MaxLengthMaximum length of a string..MaxLength(100)
LengthBetweenLength must be equal or between two values..LengthBetween(10, 100)
MustContainString must contain a specific value..MustContain("someString")
StartsWithString must start with a specific value..StartsWith("someString")
EndsWithString must end with a specific value..EndsWith("someString")
MatchesRegexString must match a regex..MatchesRegex("^[a-zA-Z0-9_]*$")
DontMatchesRegexString must not match a regex..DontMatchesRegex("^[a-zA-Z0-9_]*$")

Number Types
RequiredNumber is required, meaning it can't be zero(0)..Required()