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.
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);
}
}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.