Closed danielgtaylor closed 3 weeks ago
The pull request introduces significant updates to the documentation and implementation of request validation within the API framework. Key changes include expanded explanations of Go struct tags for JSON Schema generation, detailed handling of optional and required fields, and new sections addressing nullable fields, defaults, and strict versus loose validation. Additionally, the codebase sees improvements in error handling and parameter processing, particularly in the huma.go
file. Enhanced test cases in huma_test.go
ensure robust validation and error management across various scenarios.
File | Change Summary |
---|---|
docs/docs/features/request-validation.md | Expanded documentation on request validation, including sections on defaults, read/write-only fields, strict vs. loose validation, and advanced validation. |
huma.go | Refined error handling in findDefaults , every , and transformAndWrite functions; improved parameter processing in Register . |
huma_test.go | Added logging for panic messages, expanded test cases for middleware and request handling, and improved OpenAPI documentation generation. |
schema.go | Enhanced convertType function for better handling of type conversion for slices, especially with pointers and interfaces. |
Objective | Addressed | Explanation |
---|---|---|
Improved handling of default values for input fields (#597) | ✅ | |
Enhanced middleware functionality for cookie handling (#597) | ✅ | |
Restored input values that were incorrectly overridden (#597) | ❌ | No specific restoration of overridden input values. |
Expanded test coverage for middleware and request parameters (#597) | ✅ |
In the land of code where rabbits play,
We've spruced up docs in a grand way!
With tags and fields, both strict and loose,
Our validation's now a mighty moose!
So hop along, dear coder friends,
With clearer paths, our joy transcends! 🐇✨
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?
Attention: Patch coverage is 72.88136%
with 16 lines
in your changes missing coverage. Please review.
Project coverage is 92.70%. Comparing base (
d67ab01
) to head (9f1ee23
). Report is 4 commits behind head on main.
Files with missing lines | Patch % | Lines |
---|---|---|
huma.go | 66.66% | 10 Missing and 4 partials :warning: |
schema.go | 88.23% | 1 Missing and 1 partial :warning: |
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
@danielgtaylor
Do you think omitting a field and explcity assigning a null value should have different ouputs?
eg.
Body: `{"items": [{"id": 1, verified: null}]}`,
and
Body: `{"items": [{"id": 1}]}`,
?
Do you think omitting a field and explcity assigning a null value should have different ouputs?
@barata0 since Go's JSON marshaler and the CBOR marshaler we use don't differentiate between those I don't think it it makes sense at the parsing layer. Sending null
for something with a default will be just like explicitly saying you want the default. I think this is just a limitation of Go we have to deal with, though I'm open to ideas!
Edit: you can always not add the default
tag and use custom unmarshaling on your struct (and provide a custom schema) and then you can handle it however you like, for example https://github.com/danielgtaylor/huma/blob/main/examples/omit/main.go#L41.
I agree. Cool. I don´t think I can technically review your PR because I'm new in GO and u r using a lot of reflection (I didn't get there yet) But it looks ok to me!
This PR is an attempt to fix a long-standing issue where defaults would override explicitly sent zero values from a client if the zero value has semantic meaning for an operation handler.
It implements a new feature that allows the use of scalar pointers with defaults to match the existing behavior of the standard library JSON and other marshaling packages like https://github.com/fxamacker/cbor, which is to use a scalar pointer to determine whether a value has been set by a client or not. You can now do something like this:
The behavior is:
true
true
false
false
null
/undefined
true
This implementation does not require any additional marshal/unmarshal steps or impact the performance of the running service. It does require some additional reflection & conversions on service startup, but that's a one-time cost and the additional code complexity is not too bad.
Just to note, you still cannot provide defaults for pointers to structs. It would be possible to support this but requires different unmarshaling code in
jsonTagValue
so I'm not going to implement it unless there is a good reason to do so.FYI @Grumpfy, @robsonpeixoto, @barata0 - what do you think?
Fixes #597, #627, #628
Summary by CodeRabbit
Documentation
Bug Fixes
Tests