DomainValidation.NET 2.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package DomainValidation.NET --version 2.0.0
                    
NuGet\Install-Package DomainValidation.NET -Version 2.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="DomainValidation.NET" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DomainValidation.NET" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="DomainValidation.NET" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DomainValidation.NET --version 2.0.0
                    
#r "nuget: DomainValidation.NET, 2.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.
#addin nuget:?package=DomainValidation.NET&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=DomainValidation.NET&version=2.0.0
                    
Install as a Cake Tool

Nuget GitHub GitHub contributors GitHub Workflow Status (with event)

Domain validation with returning result object

This project is an approach to domain validation with a result object. The Result class is used to handle errors and results in your application in a consistent and reliable way. If there is no validation issue and if the model is valid, then result.Value will return the value which is valid model and the result.IsSuccess is true. However, if something is wrong with domain validation, then result.IsSuccess is false and result.Error will hold error code and description.

Example:

Here we have a Blog class with a domain validation logic:


public class Blog
{
    private Blog(string title)
    {

        Title = title;
    }

    public static Result<Blog> Create(string title)
    {
        var errors = new List<Error>();

        if (string.IsNullOrEmpty(title))
        {
            return Result.Failure<Blog>(new Error("Error.Title.Empty", "Title is mandatory"));
        }

        if (title.Length < 3)
        {
            errors.Add(new Error("Error.Title.MinLength", "The minimum title length is 3 character"));
        }

        if (title.Length > 40)
        {
            errors.Add(new Error("Error.Title.MaxLength", "The maximum title length is 40 character"));
        }

        if (errors.Any())
        {
            return Result.Failure<Blog>(errors.ToArray());
        }

        return new Blog(title);
    }

    public string Title { get; init; }
}

If we create an instance of Blog class with a valid title, then returned resule is successful with a new instance of Blog, but if it's not valid, then we will receive proper error message with detailed information about error.


if (blog.IsSuccess)
{
    Console.WriteLine(blog.Value.Title);
}

blog = Blog.Create("");

if (!blog.IsSuccess)
{
    foreach (var error in blog.Errors)
    {
        Console.WriteLine(error);
    }
}

blog = Blog.Create("B");

if (!blog.IsSuccess)
{
    foreach (var error in blog.Errors)
    {
        Console.WriteLine(error);
    }
}

Contribution

👍 You are encouraged to contribute to this project by forking it or giving it a star if you find it valuable 😃

Social Media

Email Stack Overflow Linkedin Twitter Follow

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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

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.7.0 185 2/16/2025
2.6.0 130 2/6/2025
2.5.0 110 2/6/2025
2.4.1 109 2/6/2025
2.4.0 108 2/4/2025
2.3.0 115 2/3/2025
2.2.0 116 2/3/2025
2.1.0 117 1/31/2025
2.0.0 110 1/26/2025
1.1.0 206 8/1/2023
1.0.0 173 8/1/2023