Goodtocode.Validation
1.1.4
See the version list below for details.
dotnet add package Goodtocode.Validation --version 1.1.4
NuGet\Install-Package Goodtocode.Validation -Version 1.1.4
<PackageReference Include="Goodtocode.Validation" Version="1.1.4" />
<PackageVersion Include="Goodtocode.Validation" Version="1.1.4" />
<PackageReference Include="Goodtocode.Validation" />
paket add Goodtocode.Validation --version 1.1.4
#r "nuget: Goodtocode.Validation, 1.1.4"
#:package Goodtocode.Validation@1.1.4
#addin nuget:?package=Goodtocode.Validation&version=1.1.4
#tool nuget:?package=Goodtocode.Validation&version=1.1.4
Goodtocode.Validation
Goodtocode.Validation is a lightweight, dependency-free validation library for .NET, designed for use in CQRS, Clean Architecture, and modern .NET applications. It provides a fluent API for defining validation rules for commands, queries, DTOs, and more.
Features
- Fluent, expressive rule definitions
- Supports conditional and cross-property validation
- Throws custom exceptions for invalid data
- No external dependencies
Core Concepts
- Validator<T>: Base class for defining validation rules for a type.
- RuleBuilder: Fluent builder for chaining rules on properties.
- ValidationResult: Contains validation errors and validity state.
- CustomValidationException: Thrown when validation fails (optional).
Example: Paginated Query Validator (Clean Architecture Use Case)
public class GetEntitiesQueryValidator : Validator<GetEntitiesQuery>
{
public GetEntitiesQueryValidator()
{
RuleFor(v => v.StartDate).NotEmpty()
.When(v => v.EndDate != null)
.LessThanOrEqualTo(v => v.EndDate);
RuleFor(v => v.EndDate)
.NotEmpty()
.When(v => v.StartDate != null)
.GreaterThanOrEqualTo(v => v.StartDate);
RuleFor(x => x.PageNumber).NotEqual(0);
RuleFor(x => x.PageSize).NotEqual(0);
}
}
How to Use
- Create a validator by inheriting from
Validator<T>. - Use
RuleFor(x => x.Property)to define rules with fluent methods like.NotEmpty(),.NotEqual(),.LessThanOrEqualTo(),.GreaterThanOrEqualTo(),.When(), etc. - Call
ValidateAndThrow(instance)orValidate(instance)to check validity.
RuleBuilder, Validation, and Exception Handling
RuleBuilder
The RuleBuilder class provides a fluent API for defining validation rules on your object's properties. You can chain methods such as .NotEmpty(), .NotEqual(), .LessThanOrEqualTo(), .GreaterThanOrEqualTo(), and .When() to express complex validation logic in a readable way. Each rule is registered with the validator and will be checked when validation is performed.
Validate
The Validate method (and its async variant) executes all defined rules for a given object instance. It returns a ValidationResult containing a list of ValidationFailure errors and an IsValid flag. If you use ValidateAndThrow, a CustomValidationException is thrown if any rule fails.
Handling Validation in WebApi
When using ValidateAndThrow, any validation failures will result in a CustomValidationException being thrown. In ASP.NET Core WebApi projects, it is recommended to catch this exception in your global exception handling middleware. You can then translate the exception into a 400 Bad Request response, returning the validation errors to the client in a structured format. This ensures that clients receive clear feedback on why their request was invalid, and keeps your controller actions clean and focused on business logic.
Example: Handling Validation Exceptions in Middleware
app.Use(async (context, next) =>
{
try
{
await next();
}
catch (CustomValidationException ex)
{
context.Response.StatusCode = StatusCodes.Status400BadRequest;
context.Response.ContentType = "application/json";
var result = JsonSerializer.Serialize(new { errors = ex.Errors });
await context.Response.WriteAsync(result);
}
});
Installation
Install via NuGet:
dotnet add package Goodtocode.Validation
License
MIT
Contact
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. 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. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.11)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.