package generatedtext import ( "bytes" "encoding/json" "fmt" "io" "strings" ) type Tomorrow struct { Summary string `json:"summary"` ForecastDiscussion []string `json:"forecast_discussion"` PrecipitationTiming string `json:"precipitation_timing,omitempty"` Confidence string `json:"confidence,omitempty"` } func ValidateTomorrow(data []byte) (Tomorrow, []byte, error) { decoder := json.NewDecoder(bytes.NewReader(data)) decoder.DisallowUnknownFields() var value Tomorrow if err := decoder.Decode(&value); err != nil { return Tomorrow{}, nil, fmt.Errorf("decode tomorrow generated text: %w", err) } var extra any if err := decoder.Decode(&extra); err != nil { if err != io.EOF { return Tomorrow{}, nil, fmt.Errorf("decode tomorrow generated text: %w", err) } } else { return Tomorrow{}, nil, fmt.Errorf("decode tomorrow generated text: multiple JSON values") } value.Summary = strings.TrimSpace(value.Summary) value.PrecipitationTiming = strings.TrimSpace(value.PrecipitationTiming) value.Confidence = strings.TrimSpace(value.Confidence) value.ForecastDiscussion = trimNonEmpty(value.ForecastDiscussion) if value.Summary == "" { return Tomorrow{}, nil, fmt.Errorf("tomorrow generated text summary is required") } if len(value.ForecastDiscussion) == 0 { return Tomorrow{}, nil, fmt.Errorf("tomorrow generated text forecast discussion is required") } normalized, err := json.Marshal(value) if err != nil { return Tomorrow{}, nil, fmt.Errorf("normalize tomorrow generated text: %w", err) } return value, normalized, nil } func trimNonEmpty(values []string) []string { out := make([]string, 0, len(values)) for _, value := range values { trimmed := strings.TrimSpace(value) if trimmed != "" { out = append(out, trimmed) } } return out }