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

TechNobre/PowerUtils.Validations

Repository files navigation

PowerUtils.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 5.0
  • .NET 6.0

Dependencies

  • PowerUtils.Validations.Primitives NuGet
  • PowerUtils.Globalization NuGet
  • PowerUtils.Text NuGet

How to use

Install NuGet package

This package is available through Nuget Packages: https://www.nuget.org/packages/PowerUtils.Validations

Nuget

Install-Package PowerUtils.Validations

.NET CLI

dotnet add package PowerUtils.Validations

ValidationsContract

var act = new ValidationsContract<int>(5)
    .RuleFor("Fake")
    .Min(10);

// True
var result = act.Invalid;
public class CountryValidation : ValidationsContract<Country>
{
    public CountryValidation(Country source) : base(source)
    {
        RuleFor(r => r.CountryCode)
            .CountryCodeISO2();
    }
}

ValidationNotifications

  • Properties:
    • HttpStatusCode StatusCode;
    • bool Valid;
    • bool Invalid;
    • IReadOnlyCollection<ValidationFailure> Notifications;
  • Methods:
    • IValidationNotifications AddNotification(string property, string errorCode);
    • IValidationNotifications AddBadNotification(string property, string errorCode);
    • IValidationNotifications AddBadNotification(ValidationFailure notification);
    • IValidationNotifications AddBadNotifications(IReadOnlyCollection<ValidationFailure> notifications);
    • IValidationNotifications AddBadNotifications(IList<ValidationFailure> notifications);
    • IValidationNotifications AddBadNotifications(ICollection<ValidationFailure> notifications);
    • IValidationNotifications AddBadNotifications(IEnumerable<ValidationFailure> notifications);
    • IValidationNotifications AddBadNotifications(IValidationsContract validations);
    • void SetForbiddenStatus(string property);
    • void SetNotFoundStatus(string property);
    • void SetConflictStatus(string property);
    • void SetNotificationStatus(HttpStatusCode statusCode, string property, string errorCode);
    • void Clear();

Rules

  • General:
    • .Gender();
    • .GenderWithOther();
    • .ForbiddenValue(options);
  • Objects:
    • .Required();
  • Collections:
    • .Min(min);
    • .Max(max);
    • .Range(min, max);
  • DateTimes:
    • .Date(minDate, maxDate);
    • .MinDateTimeUtcNow();
    • .MaxDateTimeUtcNow();
  • Globalization:
    • .CountryCodeISO2();
    • .Latitude();
    • .Longitude();
  • Guids:
    • .Required();
  • Numerics:
    • .MinZero();
    • .Min(min);
    • .Max(max);
    • .Range(min, max);
  • Pagination:
    • .OrderingDirectionIgnoreCase();
  • Strings:
    • .Required();
    • .Options(options);
    • .OptionsIgnoreCase(options);
    • .MaxLength(maxLength);
    • .MinLength(minLength);
    • .Length(minLength, maxLength);
    • .EmailAddress();
    • .ForbiddenValue(options);