CleanValidation.Core 1.0.0

dotnet add package CleanValidation.Core --version 1.0.0
                    
NuGet\Install-Package CleanValidation.Core -Version 1.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="CleanValidation.Core" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CleanValidation.Core" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="CleanValidation.Core" />
                    
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 CleanValidation.Core --version 1.0.0
                    
#r "nuget: CleanValidation.Core, 1.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 CleanValidation.Core@1.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=CleanValidation.Core&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=CleanValidation.Core&version=1.0.0
                    
Install as a Cake Tool

CleanValidation.Core 🐚

The .NET library for fluent, expressive, and clean validation using the best of multiple patterns: Guard, Result<T>, and extensible validators with localization support built-in.


🚀 Getting Started

1. Add the NuGet package:

Install-Package CleanValidation.Core

âœĻ Key Features

  • ✅ Fluent validation syntax using Guard
  • ✅ Built-in Result<T> system: SuccessResult, ErrorResult, InvalidResult, ConflictResult, etc.
  • ✅ Separate Guard and GuardThrow for result-based or exception-based flows
  • ✅ Extensible IValidator<T> interface for custom validations
  • ✅ Localization support via cultureName
  • ✅ Fully composable, testable, and DI-ready

⚡ Quick Example

public record User(long Id, string Name, byte Age, long UserTypeId, string Description);

public class UserValidator(IUserTypeRepository userTypeRepository) : Validator<User>
{
    private readonly IUserTypeRepository _userTypeRepository = userTypeRepository;

    public override async Task<IResult> ValidateAsync(
        User value,
        string cultureName = "en-US",
        CancellationToken cancellationToken = default)
    {
        var result = await base.ValidateAsync(value, cultureName, cancellationToken);

        if (!result.Success)
            return result;

        var exists = await _userTypeRepository.ExistsAsync(value.UserTypeId, cancellationToken);

        if (!exists)
            return NotFoundResult.Create(new Error(message: "Invalid user type id", field: nameof(user.UserTypeId)));

        return Guard.Create()
            .AgainstNullOrWhiteSpace(value.Name)
            .AgainstNullOrWhiteSpace(value.Description)
            .GetResult();
    }
}

ðŸ”Ļ Core Concepts

Guard

Fluent and chainable validation that returns a Result<T>:

Guard.Create()
    .AgainstNull(dto)
    .AgainstNullOrWhiteSpace(dto.Name)
    .GetResult();

GuardThrow

Same as Guard, but throws exceptions instead:

GuardThrow.Create()
    .AgainstNull(entity)
    .AgainstNullOrWhiteSpace(entity.Name);

Result Types

All results implement IResult or IResult<T>:

  • SuccessResult<T>
  • ErrorResult<T>
  • InvalidResult<T>
  • ConflictResult<T>
  • NotFoundResult<T>
  • ProblemResult<T>

Each provides fluent creation with error details and optional field association.


🔍 Localization Support

All methods that return or create results accept a cultureName parameter, used internally with .resx resources to localize messages dynamically.

Guard.Create("pt-BR")
    .AgainstNullOrWhiteSpace(dto.Descricao)
    .GetResult();

⚙ Structure Overview

  • Error – Base error model with Message and Field
  • ErrorUtils – Factory methods for consistent error creation
  • CleanValidationException – Base exception with helper ThrowIfNull, ThrowIfNullOrWhiteSpace, etc.
  • Guard – Fluent builder that returns results
  • GuardThrow – Fluent builder that throws exceptions
  • IResult / IResult<T> – Interfaces for all result types
  • Validator<T> / IValidator<T> – Abstract class + interface for async or sync custom validation logic
  • ResultExtensions – Helpful extensions like ErrorsTo<T>()

🌐 Goals & Philosophy

  • Minimalism + Expressiveness
  • Strong typing, fail-fast, and readability
  • Plug-and-play usability
  • Clear separation between structural validation and business rules
  • Encouragement of clean architecture without forcing opinionated structures

✅ Next Steps

  • Check out CleanValidation.Extensions.Http for ASP.NET Core integration
  • Use CleanValidation.DependencyInjection to register all IValidator<T> via Scrutor

ðŸŠĪ Inspiration

This library combines the clarity of Guard clauses, the safety of Result<T>, and the convenience of fluent interfaces — all wrapped with localization and extensibility in mind.

CleanValidation: The .NET library for Clean Validations with clarity, consistency, and control. 🐚

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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.
  • net9.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on CleanValidation.Core:

Package Downloads
CleanValidation.DependencyInjection

Plug-and-play integration for CleanValidation with ASP.NET Dependency Injection

CleanValidation.Extensions.Http

Provides seamless mapping from IResult / IResult<T> (from CleanValidation.Core) into ASP.NET Core IActionResult types, enabling fluent and expressive return statements in your controller or endpoint layer.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 155 6/24/2025