Skip to content

iPazooki/DomainValidation

Repository files navigation

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

About

This project is an approach to domain validation with a result object

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages