Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

TechNobre/PowerUtils.Results.Validations

Repository files navigation

PowerUtils.Results.Validations

⚠️ DEPRECATED

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.

Logo

Utils to help validation of the objects

License: MIT

Support to

  • .NET 6.0
  • .NET 5.0

Dependencies

  • PowerUtils.Results NuGet

How to use

Install NuGet package

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

Direct validations

var address = "";
IError error = address.IfNull();

Validations multiple properties

string firstName = null;
string lastName = null;

var errors = new List<IError>();
firstName.Validate(errors).IfNullOrWhiteSpace();
lastName.Validate(errors).IfNullOrWhiteSpace();

Multiple validations

string address = "";

var errors = address.Validate()
    .IfNullOrEmpty()
    .IfLongerThan(5);

Custom error

var validation = dateOfBirth
    .Validate()
    .IfLessThanUtcToday(
        property => Error.Forbidden(
            "DateOfBirth",
            "DateOfBirth.Invalid",
            "Invalid date"
        )
    );

Validations

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(); or ShouldBeISO2();
  • 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(); or ShouldBeGender();
    • IfNotGenderOrOther(); or ShouldBeGenderOrOther();
  • Network:
    • IfNotEmail(); or ShouldBeEmail();
  • Financial:
    • ShouldBeCVV();
    • ShouldBeCardNumber();
    • ShouldBeValidCardExpiryDate();

Conversions

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 ]

Contribution

If you have any questions, comments, or suggestions, please open an issue or create a pull request