GHM.Validator 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package GHM.Validator --version 1.0.0                
NuGet\Install-Package GHM.Validator -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="GHM.Validator" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add GHM.Validator --version 1.0.0                
#r "nuget: GHM.Validator, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install GHM.Validator as a Cake Addin
#addin nuget:?package=GHM.Validator&version=1.0.0

// Install GHM.Validator as a Cake Tool
#tool nuget:?package=GHM.Validator&version=1.0.0                

GHM.Validator

GHM.Validator is a nuget package with the aim of validating data.

IServiceCollectionExtentions

To add scoped interface IValidator to implementation Validator, call extension method to your serviceCollection.

using  GHM.Validator.Extensions;

var builder = WebApplication.CreateBuilder(args);
var services = builder.Services;
service.AddGhmValidator();

Example

Validation object

Validation is a object with properties(Message, IsValid).

var validationSuccess = Validation.Success("Successful message");

validationSuccess.Message; // "Successful message"
validationSuccess.IsValid; // true

var validationError = Validation.Error("Error message");

validationError.Message; // "Error message"
validationError.IsValid; // false

Validate request data


public Validation[] ValidateCreateUserRequest(CreateUserRequest request)
{
    IValidator validator;

    return new Validation[]
    {
        validator.ValidateIfNotNull(request.Name,"Name must not be null"),
        validator.ValidateIfNotZero(request.Age,"Age must not be 0")
    };
}

Throw if request data is invalid

public bool ValidateCreateUserRequest(CreateUserRequest request)
{
    IValidator validator;
    validator.ThrowIfNull(request.Name,"Name must not be null");
    validator.ThrowIfZero(request.Age,"Age must not be 0");

    return true;
}

Returns

Base Interface

IValidator is inherited from IThrowerService and IValidationService.

public interface IValidator : IThrowerService, IValidationService { }

Validation Return

You can use it to return a validation result with method struct object.


public interface IValidationService
{
    Validation ValidateIfNotDefault<T>(T obj, string message);
    Validation ValidateIfNotNull(object? obj, string message);
    Validation ValidateIfNull(object? obj, string message);
    Validation ValidateIfEqual(object obj, object objToComapere, string message);
    Validation ValidateIfNotZero(int number, string message);
    Validation ValidateIfNotZero(decimal number, string message);
    Validation ValidateIfGreaterOrEqual(int number, int numberToCompare, string message);
    Validation ValidateIfGreater(int number, int numberToCompare, string message);
    Validation ValidateIfGreaterOrEqual(decimal number, decimal numberToCompare, string message);
    Validation ValidateIfGreater(decimal number, decimal numberToCompare, string message);
    Validation ValidateIfNotEmpty(string text, string message);
    Validation ValidateIfParseToLong(string text, string message);
    Validation ValidateIfNotEmpty<T>(IEnumerable<T> list, string message);
    Validation ValidateIfOlder(DateTime date, DateTime dateToCompare, string message);
    Validation ValidateIfOlderOrEqual(DateTime date, DateTime dateToCompare, string message);
}

Throw Exception

You can use it to throw exception with method struct object.

public interface IThrowerService
{
    bool ThrowIfDefault<T>(T obj, string message);
    bool ThrowIfNotNull(object? obj, string message);
    bool ThrowIfNull(object? obj, string message);
    bool ThrowIfNotEqual(object obj, object objToComapere, string message);
    bool ThrowIfZero(int number, string message);
    bool ThrowIfZero(decimal number, string message);
    bool ThrowIfGreaterOrEqual(int number, int numberToCompare, string message);
    bool ThrowIfGreater(int number, int numberToCompare, string message);
    bool ThrowIfGreaterOrEqual(decimal number, decimal numberToCompare, string message);
    bool ThrowIfGreater(decimal number, decimal numberToCompare, string message);
    bool ThrowIfEmpty(string text, string message);
    bool ThrowIfNotParseToLong(string text, string message);
    bool ThrowIfEmpty<T>(IEnumerable<T> list, string message);
    bool ThrowIfOlder(DateTime date, DateTime dateToCompare, string message);
    bool ThrowIfOlderOrEqual(DateTime date, DateTime dateToCompare, string message);
}

Star

if you enjoy, don't forget the ⭐ and install the package 😊.

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
5.0.0 106 8/7/2024
4.0.0 82 8/2/2024
3.5.0 110 6/1/2024
3.4.2 123 2/26/2024
3.4.1 125 2/24/2024
3.3.1 121 2/14/2024
3.2.1 117 2/6/2024
3.2.0 109 2/5/2024
3.1.0 109 2/2/2024
2.1.3 144 1/12/2024
2.1.2 115 1/12/2024
2.1.1 113 1/12/2024
2.1.0 110 1/12/2024
2.0.0 203 11/15/2023
1.0.0 137 10/7/2023