DamianEdwards / MiniValidation

A minimalist validation library for .NET built atop the existing features in `System.ComponentModel.DataAnnotations` namespace
MIT License
321 stars 25 forks source link

Use JSON property names in validation errors #61

Open AnthonyMastrean opened 2 months ago

AnthonyMastrean commented 2 months ago

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

DamianEdwards commented 1 month ago

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?