Southclaws / supervillain

Converts Go structs to Zod schemas
MIT License
76 stars 6 forks source link

Treat byte slices as strings #13

Closed josiah-tt closed 3 months ago

josiah-tt commented 3 months ago

This is a special case noted in the json.Marshal() documentation (https://pkg.go.dev/encoding/json#Marshal):

Array and slice values encode as JSON arrays, except that []byte encodes as a base64-encoded string, and a nil slice encodes as the null JSON value.

josiah-tt commented 3 months ago

Here's a demonstration of json.Marshal() behaviour, which is consistent with z.string().nullable(): https://go.dev/play/p/yALffxm7AS8

&main.T{Data:[]uint8{0x68, 0x69}}
{"Data":"aGk="}

&main.T{Data:[]uint8{}}
{"Data":""}

&main.T{Data:[]uint8(nil)}
{"Data":null}
Southclaws commented 3 months ago

Nice catch, thanks!