DomainValidation.NET 3.0.0

dotnet add package DomainValidation.NET --version 3.0.0
                    
NuGet\Install-Package DomainValidation.NET -Version 3.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="3.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DomainValidation.NET" Version="3.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 3.0.0
                    
#r "nuget: DomainValidation.NET, 3.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.
#:package DomainValidation.NET@3.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DomainValidation.NET&version=3.0.0
                    
Install as a Cake Addin
#tool nuget:?package=DomainValidation.NET&version=3.0.0
                    
Install as a Cake Tool

Nuget GitHub GitHub contributors GitHub Workflow Status (with event)

Domain validation with a result object

This library provides a simple, consistent approach to domain validation using a Result object. The Result class encapsulates both the outcome of an operation and any associated validation errors.

  • When validation passes and the model is valid, result.IsSuccess is true and result.Value contains the valid model.
  • When validation fails, result.IsSuccess is false and result.Errors contains one or more Error instances describing what went wrong.

Example

Consider a Blog class with basic 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<Blog>.Failure("Title is mandatory");
        }

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

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

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

        return new Blog(title);
    }

    public string Title { get; init; }
}

Creating an instance of Blog with a valid title returns a successful Result<Blog> containing the new instance. If the title is invalid, the result indicates failure and contains the corresponding error details.

Result<Blog> blog = Blog.Create("My awesome blog");

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 very welcome to contribute by opening issues, submitting pull requests, or simply giving the project a ⭐ on GitHub if you find it useful.

Social media

Email Stack Overflow Linkedin Twitter Follow

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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.
  • net10.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
3.0.0 300 12/25/2025
2.7.0 381 2/16/2025
2.6.0 267 2/6/2025
2.5.0 246 2/6/2025
2.4.1 255 2/6/2025
2.4.0 222 2/4/2025
2.3.0 257 2/3/2025
2.2.0 231 2/3/2025
2.1.0 269 1/31/2025
2.0.0 241 1/26/2025
1.1.0 317 8/1/2023
1.0.0 278 8/1/2023

Upgrade to .NET 10