CleanValidation.Core
1.0.0
dotnet add package CleanValidation.Core --version 1.0.0
NuGet\Install-Package CleanValidation.Core -Version 1.0.0
<PackageReference Include="CleanValidation.Core" Version="1.0.0" />
<PackageVersion Include="CleanValidation.Core" Version="1.0.0" />
<PackageReference Include="CleanValidation.Core" />
paket add CleanValidation.Core --version 1.0.0
#r "nuget: CleanValidation.Core, 1.0.0"
#:package CleanValidation.Core@1.0.0
#addin nuget:?package=CleanValidation.Core&version=1.0.0
#tool nuget:?package=CleanValidation.Core&version=1.0.0
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
andGuardThrow
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
andField
- 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 allIValidator<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 | Versions 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. |
-
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 |