package strictjson import "testing" // TestDecodeOptional distinguishes omission from explicit null and duplicate fields. func TestDecodeOptional(t *testing.T) { cases := []struct { name string body string valid bool }{ {"omitted", `{"text":"ok"}`, true}, {"provided", `{"text":"ok","attachment_ids":["a"]}`, true}, {"null", `{"text":"ok","attachment_ids":null}`, false}, {"unknown", `{"text":"ok","sender_id":"a"}`, false}, {"duplicate", `{"text":"ok","text":"other"}`, false}, {"missing required", `{"attachment_ids":[]}`, false}, {"wrong type", `{"text":"ok","attachment_ids":12}`, false}, {"invalid utf8", string([]byte{255}), false}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { var target struct { Text string `json:"text"` IDs []string `json:"attachment_ids"` } err := DecodeOptional([]byte(tc.body), &target, []string{"text"}, []string{"attachment_ids"}) if (err == nil) != tc.valid { t.Fatalf("validation: %v", err) } }) } }