DomainValidation.NET
3.0.0
dotnet add package DomainValidation.NET --version 3.0.0
NuGet\Install-Package DomainValidation.NET -Version 3.0.0
<PackageReference Include="DomainValidation.NET" Version="3.0.0" />
<PackageVersion Include="DomainValidation.NET" Version="3.0.0" />
<PackageReference Include="DomainValidation.NET" />
paket add DomainValidation.NET --version 3.0.0
#r "nuget: DomainValidation.NET, 3.0.0"
#:package DomainValidation.NET@3.0.0
#addin nuget:?package=DomainValidation.NET&version=3.0.0
#tool nuget:?package=DomainValidation.NET&version=3.0.0
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.IsSuccessistrueandresult.Valuecontains the valid model. - When validation fails,
result.IsSuccessisfalseandresult.Errorscontains one or moreErrorinstances 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
| Product | Versions 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. |
-
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.
Upgrade to .NET 10