Validity.DataAnnotation 2.1.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package Validity.DataAnnotation --version 2.1.3                
NuGet\Install-Package Validity.DataAnnotation -Version 2.1.3                
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="Validity.DataAnnotation" Version="2.1.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Validity.DataAnnotation --version 2.1.3                
#r "nuget: Validity.DataAnnotation, 2.1.3"                
#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 Validity.DataAnnotation as a Cake Addin
#addin nuget:?package=Validity.DataAnnotation&version=2.1.3

// Install Validity.DataAnnotation as a Cake Tool
#tool nuget:?package=Validity.DataAnnotation&version=2.1.3                

NuGet Package

Table of content

Installation

dotnet add package Resulver

Usage

 public Result<int> Sum(int a, int b)
 {
     var sum = a + b;

     return new Result<int>(sum);
 }

 public void Writer()
 {
     var result = Sum(3, 5);

     Console.WriteLine(result.Content);
 } 

Result Message

public Result<User> AddUser(User user)
{
    // implementation

    return new Result<User>(user, message: "User Created");
}

public void Writer()
{
    var user = new User();

    var result = AddUser(user);

    Console.WriteLine(result.Message);
}

Result Errors

public class UserNotFoundError() : ResultError(message: "User not found");
public class UserIdIsNotValidError() : ResultError(message:   "User ID is not valid");

public Result RemoveUser(int userId)
{
    //Implementation



    return new UserNotFoundError();

    //or for multiple errors
    return new Result(new UserNotFoundError(), new UserIdIsNotValidError());
}

public void Writer()
{
    var result = RemoveUser(1);

    if (result.IsFailure)
    {
        foreach (var error in result.Errors)
        {
            Console.WriteLine(error.Message);
        }
    }
}

1. Install package

dotnet add package AutoValidator.DataAnnotation

2. Create Class

public class MyClass
{
     [Phone]
     public string PhoneNumber { get; set; }
     public NestedClass Nested { get; set; }
}

public class NestedClass
{
     [MaxLength(5)]
     public string Title { get; set; }
}

3. Add validator to service container

in Program.cs

 builder.Services.AddObjectValidator();

4. Inject validator to yout controller

[ApiController]
[Route("api/[controller]")]
public class MyController : ControllerBase
{
    readonly IObjectValidator _validator;

    public MyController(IObjectValidator validator)
    {
        _validator = validator;
    }
}

5. Use validator

[HttpPost]
public IActionResult AddUser(MyClass myClass)
{
    var validationErrors = _validator.Validate(myClass);

    if (validationErrors.Count != 0)
    {
        //DO SOMETHING
    }
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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
2.2.3 90 10/3/2024
2.2.2 133 8/20/2024
2.2.1 114 8/20/2024
2.1.12 122 8/17/2024
2.1.11 109 8/17/2024
2.1.3 115 8/20/2024
2.1.2 104 8/20/2024
2.1.1 100 7/14/2024
2.1.0 98 7/13/2024
2.0.1 103 7/7/2024
2.0.0 94 7/7/2024
1.3.0 92 7/3/2024
1.2.0 109 6/28/2024
1.0.0 98 6/12/2024