Goodtocode.Validation 1.1.4

There is a newer version of this package available.
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
                    
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="Goodtocode.Validation" Version="1.1.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Goodtocode.Validation" Version="1.1.4" />
                    
Directory.Packages.props
<PackageReference Include="Goodtocode.Validation" />
                    
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 Goodtocode.Validation --version 1.1.4
                    
#r "nuget: Goodtocode.Validation, 1.1.4"
                    
#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 Goodtocode.Validation@1.1.4
                    
#: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=Goodtocode.Validation&version=1.1.4
                    
Install as a Cake Addin
#tool nuget:?package=Goodtocode.Validation&version=1.1.4
                    
Install as a Cake Tool

Goodtocode.Validation

NuGet CI/CD

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

  1. Create a validator by inheriting from Validator<T>.
  2. Use RuleFor(x => x.Property) to define rules with fluent methods like .NotEmpty(), .NotEqual(), .LessThanOrEqualTo(), .GreaterThanOrEqualTo(), .When(), etc.
  3. Call ValidateAndThrow(instance) or Validate(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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.12 106 1/23/2026
1.1.9 36 1/22/2026
1.1.4 40 1/22/2026