MiniValidation icon indicating copy to clipboard operation
MiniValidation copied to clipboard

Use JSON property names in validation errors

Open AnthonyMastrean opened this issue 1 year ago • 1 comments

Is there any way to convert error keys from their C# property names to their JSON property names, as in MVC model validation?

https://learn.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-8.0#use-json-property-names-in-validation-errors

AnthonyMastrean avatar Apr 15 '24 18:04 AnthonyMastrean

Not currently. MVC has a very rich "model metadata" system that it uses to track the input, state, and output of data through the request lifecycle, but MiniValidation is just concerned with the actual validation using System.ComponentModel.DataAnnotations attributes. That said, it would be possible to add support for some kind of error key transformer to MiniValidation to enable transforming the error keys, e.g. MiniValidator.TryValidate(widget, ErrorKeyTransformer.CamelCase, out var errors). Perhaps it could support extracting JsonSerializerOptions from the passed IServiceProvider and using its PropertyNamingPolicy too?

DamianEdwards avatar May 03 '24 16:05 DamianEdwards