Skip to content

Commit 9d5df2e

Browse files
authored
test: improve validation request body helpers (#175)
1 parent dd80481 commit 9d5df2e

1 file changed

Lines changed: 31 additions & 9 deletions

File tree

bridge_integration_test.go

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2175,7 +2175,10 @@ func newMockServer(ctx context.Context, t *testing.T, files archiveFileMap, requ
21752175
}
21762176

21772177
// validateOpenAIChatCompletionRequest validates that an OpenAI chat completion request
2178-
// has all required fields. Returns an error if validation fails.
2178+
// has all required fields.
2179+
// According to OpenAI documentation https://platform.openai.com/docs/api-reference/chat/create,
2180+
// the "model" and "messages" fields are required.
2181+
// Returns an error if validation fails.
21792182
func validateOpenAIChatCompletionRequest(body []byte) error {
21802183
var req openai.ChatCompletionNewParams
21812184
if err := json.Unmarshal(body, &req); err != nil {
@@ -2198,24 +2201,43 @@ func validateOpenAIChatCompletionRequest(body []byte) error {
21982201
}
21992202

22002203
// validateOpenAIResponsesRequest validates that an OpenAI responses request
2201-
// has all required fields. Returns an error if validation fails.
2204+
// has all required fields.
2205+
// According to OpenAI documentation https://platform.openai.com/docs/api-reference/responses/create,
2206+
// no fields are strictly required. However, we check "model" and "input" fields
2207+
// as these should usually be set in request bodies.
2208+
// Returns an error if validation fails.
22022209
func validateOpenAIResponsesRequest(body []byte) error {
22032210
var reqBody map[string]any
22042211
if err := json.Unmarshal(body, &reqBody); err != nil {
2205-
return fmt.Errorf("request should be valid JSON: %w", err)
2212+
return fmt.Errorf("request should unmarshal into valid JSON: %w", err)
22062213
}
22072214

2208-
// Verify required fields for OpenAI responses
2209-
// Note: Using map here since there's no specific SDK type for responses endpoint
2210-
model, ok := reqBody["model"]
2211-
if !ok || model == "" {
2212-
return fmt.Errorf("model field is required but missing or empty")
2215+
// Collect all validation errors
2216+
var errs []string
2217+
2218+
// Validate model field exists
2219+
model, hasModel := reqBody["model"].(string)
2220+
if !hasModel || model == "" {
2221+
errs = append(errs, "model field is required but empty or missing")
2222+
}
2223+
2224+
// Validate input field exists
2225+
if _, hasInput := reqBody["input"]; !hasInput {
2226+
errs = append(errs, "input field is required but missing")
2227+
}
2228+
2229+
if len(errs) > 0 {
2230+
return fmt.Errorf("validation failed: %s", strings.Join(errs, "; "))
22132231
}
22142232
return nil
22152233
}
22162234

22172235
// validateAnthropicMessagesRequest validates that an Anthropic messages request
2218-
// has all required fields. Returns an error if validation fails.
2236+
// has all required fields.
2237+
// According to the Anthropic Go SDK https://github.com/anthropics/anthropic-sdk-go,
2238+
// the "model", "messages", and "max_tokens" fields are required, as indicated by
2239+
// the `required` struct tags in MessageNewParams.
2240+
// Returns an error if validation fails.
22192241
func validateAnthropicMessagesRequest(body []byte) error {
22202242
var req anthropic.MessageNewParams
22212243
if err := json.Unmarshal(body, &req); err != nil {

0 commit comments

Comments
 (0)