This package has been discontinued because it never evolved, and the code present in this package does not justify its continuation. It is preferable to implement this code directly in the project if necessary or if you prefer, you can use a good projects like FluentValidation and Flunt.
Utils to help validation of the objects
- .NET 6.0
- .NET 5.0
- PowerUtils.Results NuGet
This package is available through Nuget Packages: https://www.nuget.org/packages/PowerUtils.Results.Validations
Nuget
Install-Package PowerUtils.Results.Validations
.NET CLI
dotnet add package PowerUtils.Results.Validations
var address = "";
IError error = address.IfNull();
string firstName = null;
string lastName = null;
var errors = new List<IError>();
firstName.Validate(errors).IfNullOrWhiteSpace();
lastName.Validate(errors).IfNullOrWhiteSpace();
string address = "";
var errors = address.Validate()
.IfNullOrEmpty()
.IfLongerThan(5);
var validation = dateOfBirth
.Validate()
.IfLessThanUtcToday(
property => Error.Forbidden(
"DateOfBirth",
"DateOfBirth.Invalid",
"Invalid date"
)
);
When a rule starts of
If***
, the error is returned when the condition matches
When a rule starts from
Should***
, the error is returned when the condition doesn't match
E.g:
var email1 = "email";
// Returns an error
var error1 = email1.IfNotEmail();
// Returns an error
var error2 = email1.ShouldBeEmail();
var email2 = "email@fake.fk";
// Returns null
var error3 = email2.IfNotEmail();
// Returns null
var error4 = email2.ShouldBeEmail();
- Collections:
IfEmpty();
IfNullOrEmpty();
IfCountGreaterThan();
IfCountLessThan();
IfCountOutOfRange();
- Globalization:
IfLatitudeOutOfRange();
IfLongitudeOutOfRange();
IfNotISO2();
orShouldBeISO2();
- Guids:
IfEmpty();
IfEquals();
IfDifferent();
- Numerics:
IfZero();
IfGreaterThan();
IfLessThan();
IfEquals();
IfDifferent();
IfOutOfRange();
- Strings:
IfEmpty();
IfNullOrEmpty();
IfNullOrWhiteSpace();
IfLongerThan();
IfShorterThan();
IfLengthEquals();
IfLengthDifferent();
IfLengthOutOfRange();
IfEquals();
IfDifferent();
- DateTimes/DateOnly/TimeOnly:
IfGreaterThan();
IfLessThan();
IfOutOfRange();
IfEquals();
IfDifferent();
IfGreaterThanUtcNow();
IfLessThanUtcNow();
IfGreaterThanUtcToday();
IfLessThanUtcToday();
- Objects:
IfNull();
IfEquals();
IfDifferent();
- Streams:
IfNull();
IfEmpty();
IfNullOrEmpty();
- Human:
IfNotGender();
orShouldBeGender();
IfNotGenderOrOther();
orShouldBeGenderOrOther();
- Network:
IfNotEmail();
orShouldBeEmail();
- Financial:
ShouldBeCVV();
ShouldBeCardNumber();
ShouldBeValidCardExpiryDate();
var dateTime = "2022-12-31 12:15:46";
var errors = dateTime
.Validate()
.IfNull() // Validate if the string is null
.ToDateTime() // Convert to DateTime
.IfLessThanUtcNow(); // Validate as a DateTime
In case you want to use the conversion extensions with a sugar syntax and still enjoy the resulted value, you can use the overload with
out result
var value = "12:15:46";
DateTime dateTime;
var errors = value
.Validate()
.ToDateTime(out dateTime) // Convert to DateTime
.IfLessThanUtcNow(); // Validate as a DateTime
- DateTimes:
ToDateTime();
ToDateTimeNullable();
ToDate();
(Only available for.NET 6.0
or greater)ToDateNullable();
(Only available for.NET 6.0
or greater)ToTime();
(Only available for.NET 6.0
or greater)ToTimeNullable();
(Only available for.NET 6.0
or greater)
- Numerics:
ToNumber<TValue>();
[ short | ushort | int | uint | long | ulong | float | double | decimal ]
If you have any questions, comments, or suggestions, please open an issue or create a pull request