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
|
| EqualTo | Value must be equal to a value. | .EqualTo(someValue) |
| NotEqualTo | Value must not be equal to a value. | .NotEqualTo(someValue) |
| OneOf | Is one of the items in a collection. | .OneOf(new [] { "val1", "val2", "val3" }) |
| NotOneOf | Is not one of the items in a collection. | .NotOneOf(new [] { "val1", "val2", "val3" }) |
| ExpressionMustBeTrue | Create a custom lambda expression that must be true. | .ExpressionMustBeTrue(v => v.Length == 5) |
| ExpressionMustBeFalse | Create a custom lambda expression that must be false. | .ExpressionMustBeFalse(v => v.Length == 5) |
IComparable Types numbers, dates & etc
|
| Min | The minimum allowed value. | .Min(10) |
| Max | The maximum allowed value. | .Max(100) |
| Between | A value is equal or between two values. | .Between(10, 100) |
| GreaterThan | A value greater than. | .GreaterThan(5) |
| LessThan | A value less than. | .LessThan(8) |
ICollection Types
|
| MustBeEmpty | Collection must be empty. | .MustBeEmpty() |
| CantBeEmpty | Collection can't be empty. | .CantBeEmpty() |
| MinCount | Minimum items in a collection. | .MinCount(10) |
| MaxCount | Maximimum items in a collection. | .MaxCount(100) |
| CountBetween | The number of items must be equal or between two values. | .CountBetween(10, 100) |
Bool Types
|
| MustBeTrue | Value must be true. | .MustBeTrue() |
| MustBeFalse | Value must be false. | .MustBeFalse() |
String Types
|
| Required | String is required and can't be empty or whitespace. | .Required() |
| NotEmpty | String cant be empty. | .NotEmpty() |
| NotEmptyOrWhitespace | String can't be empty or whitespace. | .NotEmptyOrWhitespace() |
| MinLength | Minimum length of a string. | .MinLength(10) |
| MaxLength | Maximum length of a string. | .MaxLength(100) |
| LengthBetween | Length must be equal or between two values. | .LengthBetween(10, 100) |
| MustContain | String must contain a specific value. | .MustContain("someString") |
| StartsWith | String must start with a specific value. | .StartsWith("someString") |
| EndsWith | String must end with a specific value. | .EndsWith("someString") |
| MatchesRegex | String must match a regex. | .MatchesRegex("^[a-zA-Z0-9_]*$") |
| DontMatchesRegex | String must not match a regex. | .DontMatchesRegex("^[a-zA-Z0-9_]*$") |
Number Types
|
| Required | Number is required, meaning it can't be zero(0). | .Required() |