© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•15mo ago•
1 reply
Cydo

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;
        }
    }
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

❔ (MVC web app) How can I handle exceptions/errors in this case?
C#CC# / help
3y ago
✅ How to convert controller route name to camel case automatically?
C#CC# / help
11mo ago
❔ FluentValidation DTO
C#CC# / help
3y ago
❔ FluentValidation help
C#CC# / help
4y ago