CleanResult.FluentValidation
1.3.0
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package CleanResult.FluentValidation --version 1.3.0
NuGet\Install-Package CleanResult.FluentValidation -Version 1.3.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="CleanResult.FluentValidation" Version="1.3.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CleanResult.FluentValidation" Version="1.3.0" />
<PackageReference Include="CleanResult.FluentValidation" />
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 CleanResult.FluentValidation --version 1.3.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: CleanResult.FluentValidation, 1.3.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 CleanResult.FluentValidation@1.3.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=CleanResult.FluentValidation&version=1.3.0
#tool nuget:?package=CleanResult.FluentValidation&version=1.3.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
CleanResult.FluentValidation
Integration between CleanResult and FluentValidation for seamless validation error handling.
Installation
dotnet add package CleanResult.FluentValidation
Features
- Convert
ValidationResulttoResultorResult<T> - Direct validator integration with Result types
- Async validation support
- Automatic conversion of validation errors to RFC 9457 Problem Details format
Usage
Basic ValidationResult Conversion
using CleanResult.FluentValidation;
using FluentValidation;
public class CreateUserCommand
{
public string Name { get; set; }
public string Email { get; set; }
}
public class CreateUserValidator : AbstractValidator<CreateUserCommand>
{
public CreateUserValidator()
{
RuleFor(x => x.Name).NotEmpty();
RuleFor(x => x.Email).EmailAddress();
}
}
// Convert ValidationResult to Result
var validator = new CreateUserValidator();
var command = new CreateUserCommand { Name = "", Email = "invalid" };
var validationResult = validator.Validate(command);
Result result = validationResult.ToResult();
if (result.IsError())
{
// Returns 400 Bad Request with validation errors in RFC 9457 format
return result;
}
Direct Validator Integration
// Validate and return Result directly
Result result = validator.ValidateToResult(command);
// Validate and return the validated instance on success
Result<CreateUserCommand> resultWithValue = validator.ValidateToResultWithValue(command);
if (resultWithValue.IsOk())
{
var validatedCommand = resultWithValue.Value;
// Use the validated command
}
Async Validation
// Async validation
Result result = await validator.ValidateToResultAsync(command);
// Async validation with value
Result<CreateUserCommand> resultWithValue =
await validator.ValidateToResultWithValueAsync(command);
ASP.NET Core Integration
Use directly in controllers:
[HttpPost]
public async Task<Result<User>> CreateUser(
[FromBody] CreateUserCommand command,
[FromServices] IValidator<CreateUserCommand> validator)
{
var validationResult = await validator.ValidateToResultWithValueAsync(command);
if (validationResult.IsError())
return validationResult;
var user = await _userService.CreateUser(validationResult.Value);
return Result.Ok(user);
}
Customizing Error Messages
Result result = validationResult.ToResult(
title: "User Creation Failed",
detail: "The provided user data is invalid",
instance: "/users/create"
);
Error Format
Validation errors are automatically converted to RFC 9457 Problem Details format:
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "Validation Failed",
"status": 400,
"errors": {
"Name": ["'Name' must not be empty."],
"Email": ["'Email' is not a valid email address."]
}
}
API Reference
ValidationResultExtensions
ToResult()- Convert ValidationResult to ResultToResult<T>(T value)- Convert ValidationResult to Result<T> with value
ValidatorExtensions
ValidateToResult<T>(instance)- Validate and return ResultValidateToResultAsync<T>(instance, cancellationToken)- Async validate and return ResultValidateToResultWithValue<T>(instance)- Validate and return Result<T> with validated instanceValidateToResultWithValueAsync<T>(instance, cancellationToken)- Async version of above
All methods support optional parameters for customizing error details:
title- Error title (default: "Validation Failed")detail- Optional error detail messageinstanceUri/instance- Optional URI identifying the error instance
License
MIT
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- CleanResult (>= 1.3.0)
- FluentValidation (>= 11.9.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.