huma
huma copied to clipboard
Case Sensitivity For JSON tags
Case sensitivity is enforced with huma via struct tags. In several other frameworks, case sensitivity is NOT enforced. Therefore, migration to huma may break some client integrations. Is it possible to introduce a tag to allow case insensitivity?
@jchen1122-sibros do you have an example? Do you want Huma to e.g. always produce lower case output? Part of this is controlled by the serialization format and the marshaler implementation, e.g. https://pkg.go.dev/encoding/json#Marshal rather than within Huma itself. You might be able to use a custom JSON marshaler to do what you want and register it with your huma.Config for the application/json format.
This is how the default formatter works BTW: https://github.com/danielgtaylor/huma/blob/main/defaults.go#L8-L21
The native golang json marshal/unmarshal is case insensitive . Huma json tag validation is case sensitive. It makes it is backward incompatible as we migrate to Huma. Would be a good idea to have a flag in Huma to make the validation case insensitive.
@danielgtaylor what do you think?
Aaaah, thank you @jc-fireball I think I wasn't understanding this before. So you are saying that since something like this works: https://go.dev/play/p/2qbvJKIkt9J you would expect Huma's validation to behave the same, ignoring the fact that FOO isn't the same as foo.
I think we can probably support this in a reasonable way to help people migrate over legacy systems.
@danielgtaylor correct. We are migrating services to Huma but this json field name issue blocks us. The change should be straightforward to lower all the cases when validation happens. If you can prioritize it, it will be much better. Thanks in advance.
@jc-fireball take a look at #629 and let me know what you think!
It looks good to me. Let's ship it !