huma
huma copied to clipboard
Confusing documentation, possibly typo
https://github.com/danielgtaylor/huma/blob/4b6a508912c987cd725a700a4994404dc1f18c8a/docs/docs/features/request-validation.md?plain=1#L61
The section above mentioned that:
Pointers have no effect on optional/required. ... Start with all fields required.
Isn't this the correct phrasing?
1. To a `boolean`, `integer`, `number`, `string`: it is required unless it has `omitempty`.
@vkhobor no I believe this is correct. If a field is a pointer to e.g. a string and is nil then it will marshal in JSON as null (so is nullable). If it has omitempty applied then it will not marshal into the JSON at all, which is equivalent to undefined rather than null, so the type is not nullable when omitempty is present. Does that make sense? Is there a better way to write this?
Is this only about marshaling?
Maybe my confusion comes from the fact that this page is titled 'request validation'.
There is a code block above in a section.
type MyStruct struct {
// The following are all required.
Required1 string `json:"required1"`
Required2 *string `json:"required2"`
Required3 string `json:"required3,omitempty" required:"true"`
// The following are all optional.
Optional1 string `json:"optional1,omitempty"`
Optional2 *string `json:"optional2,omitempty"`
Optional3 string `json:"optional3" required:"false"` }
And to me the conflicting info is that there is a pointer to a string, and it is in the required section.