How can I make FluentValidation return errors in camel case??
I have tried created an ValidationInterceptor and configuring my program.cs to return json in camel case but nothing seems to work
using FluentValidation;
using FluentValidation.Results;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Text.Json;
using FluentValidation.AspNetCore;
namespace CollabParty.Application.Common.Validators
{
public class CamelCaseValidationInterceptor : IValidatorInterceptor
{
// Before MVC validation
public ValidationContext<T> BeforeMvcValidation<T>(ControllerContext controllerContext, ValidationContext<T> context)
{
return context; // No changes before validation
}
// After MVC validation: Convert the property names to camelCase
public ValidationResult AfterMvcValidation<T>(ControllerContext controllerContext, ValidationContext<T> context, ValidationResult result)
{
var camelCasedErrors = result.Errors.Select(error =>
new ValidationFailure(
JsonNamingPolicy.CamelCase.ConvertName(error.PropertyName), // Convert to camelCase
error.ErrorMessage,
error.AttemptedValue)
{
ErrorCode = error.ErrorCode,
CustomState = error.CustomState,
Severity = error.Severity
}).ToList();
return new ValidationResult(camelCasedErrors);
}
// These methods are not needed but can be left as no-ops
public IValidationContext BeforeAspNetValidation(ActionContext actionContext, IValidationContext commonContext)
{
return commonContext;
}
public ValidationResult AfterAspNetValidation(ActionContext actionContext, IValidationContext validationContext, ValidationResult result)
{
return result;
}
}
}
using FluentValidation;
using FluentValidation.Results;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Text.Json;
using FluentValidation.AspNetCore;
namespace CollabParty.Application.Common.Validators
{
public class CamelCaseValidationInterceptor : IValidatorInterceptor
{
// Before MVC validation
public ValidationContext<T> BeforeMvcValidation<T>(ControllerContext controllerContext, ValidationContext<T> context)
{
return context; // No changes before validation
}
// After MVC validation: Convert the property names to camelCase
public ValidationResult AfterMvcValidation<T>(ControllerContext controllerContext, ValidationContext<T> context, ValidationResult result)
{
var camelCasedErrors = result.Errors.Select(error =>
new ValidationFailure(
JsonNamingPolicy.CamelCase.ConvertName(error.PropertyName), // Convert to camelCase
error.ErrorMessage,
error.AttemptedValue)
{
ErrorCode = error.ErrorCode,
CustomState = error.CustomState,
Severity = error.Severity
}).ToList();
return new ValidationResult(camelCasedErrors);
}
// These methods are not needed but can be left as no-ops
public IValidationContext BeforeAspNetValidation(ActionContext actionContext, IValidationContext commonContext)
{
return commonContext;
}
public ValidationResult AfterAspNetValidation(ActionContext actionContext, IValidationContext validationContext, ValidationResult result)
{
return result;
}
}
}
1 Reply
Program.cs
// JSON and FluentValidation Configuration
builder.Services.AddControllers()
.AddJsonOptions(options => { options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; });
// Use the DependencyInjection method for adding EmailService
builder.Services.AddInfrastructureServices(builder.Configuration);
// FluentValidation Setup
builder.Services.AddFluentValidationAutoValidation()
.AddFluentValidationClientsideAdapters();
// Register Validators
builder.Services.AddValidatorsFromAssemblyContaining<LoginCredentialsRequestDtoValidator>();
builder.Services.AddValidatorsFromAssemblyContaining<RegisterCredentialsRequestDtoValidator>();
builder.Services.AddValidatorsFromAssemblyContaining<TokenDtoRequestValidator>();
builder.Services.AddValidatorsFromAssemblyContaining<CreatePartyRequestDtoValidator>();
builder.Services.AddValidatorsFromAssemblyContaining<ChangePasswordRequestDtoValidator>();
builder.Services.AddValidatorsFromAssemblyContaining<ForgotPasswordRequestDtoValidator>();
builder.Services.AddValidatorsFromAssemblyContaining<ResetPasswordRequestDtoValidator>();
// Register the CamelCaseValidationInterceptor manually
builder.Services.AddScoped<IValidatorInterceptor, CamelCaseValidationInterceptor>();
// JSON and FluentValidation Configuration
builder.Services.AddControllers()
.AddJsonOptions(options => { options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; });
// Use the DependencyInjection method for adding EmailService
builder.Services.AddInfrastructureServices(builder.Configuration);
// FluentValidation Setup
builder.Services.AddFluentValidationAutoValidation()
.AddFluentValidationClientsideAdapters();
// Register Validators
builder.Services.AddValidatorsFromAssemblyContaining<LoginCredentialsRequestDtoValidator>();
builder.Services.AddValidatorsFromAssemblyContaining<RegisterCredentialsRequestDtoValidator>();
builder.Services.AddValidatorsFromAssemblyContaining<TokenDtoRequestValidator>();
builder.Services.AddValidatorsFromAssemblyContaining<CreatePartyRequestDtoValidator>();
builder.Services.AddValidatorsFromAssemblyContaining<ChangePasswordRequestDtoValidator>();
builder.Services.AddValidatorsFromAssemblyContaining<ForgotPasswordRequestDtoValidator>();
builder.Services.AddValidatorsFromAssemblyContaining<ResetPasswordRequestDtoValidator>();
// Register the CamelCaseValidationInterceptor manually
builder.Services.AddScoped<IValidatorInterceptor, CamelCaseValidationInterceptor>();