How I make FluentValidation show nested properties as camelCase not PascalCase in .Net web api?

1.3k Views Asked by At

camelCase in Fluent validation in .Net 6 Web Api

Nested properties are PascalCase from Fluent Validation, how can i change it to be camelCase for nested objects/everything?

For the client I use React, I do not want to convert it client-side.

What i currently have is:

{
    "name": [
        "Navn på vin er påkrevd.",
        "Navn på vin må minst være 3 bokstaver."
    ],
    "type": [
        "Type er påkrevd."
    ],
    // What i want: userDetails.quantity
    "userDetails.Quantity": [
        "'user Details Quantity' must be between 0 and 2000. You entered -1."
    ]
}

validation response

What i want is userDetails.quantity not userDetails.Quantity

I have this in my Program.cs class

builder.Services.AddControllers()
    .AddFluentValidation(fv =>
    {
        fv.RegisterValidatorsFromAssemblyContaining<AddWineValidator>();
    })
    .AddNewtonsoftJson(options =>
    {
        options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    });
1

There are 1 best solutions below

1
mixo On

Well, I would suggest using fluentvalidation's PropertyNameResolver for the job. I think this issue on github directly answers your question.