huma icon indicating copy to clipboard operation
huma copied to clipboard

Confusing documentation, possibly typo

Open vkhobor opened this issue 1 year ago • 2 comments
trafficstars

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 avatar Jul 27 '24 22:07 vkhobor

@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?

danielgtaylor avatar Jul 29 '24 17:07 danielgtaylor

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.

vkhobor avatar Jul 29 '24 17:07 vkhobor