validateAttributes validates keys and values of an attributes map, handling nil input
(attributes map[string]any)
| 4075 | |
| 4076 | // validateAttributes validates keys and values of an attributes map, handling nil input |
| 4077 | func (c *connection) validateAttributes(attributes map[string]any) (map[string]any, error) { |
| 4078 | if attributes == nil { |
| 4079 | return make(map[string]any), nil |
| 4080 | } |
| 4081 | |
| 4082 | if len(attributes) > 50 { |
| 4083 | return nil, fmt.Errorf("too many attributes: maximum 50 allowed, got %d", len(attributes)) |
| 4084 | } |
| 4085 | |
| 4086 | for key, value := range attributes { |
| 4087 | // Validate key format |
| 4088 | if !isValidAttributeKey(key) { |
| 4089 | return nil, fmt.Errorf("invalid attribute key '%s': must contain only alphanumeric characters and underscores", key) |
| 4090 | } |
| 4091 | |
| 4092 | // Validate value length |
| 4093 | if str, ok := value.(string); ok && len(str) > 256 { |
| 4094 | return nil, fmt.Errorf("attribute value for key '%s' too long: maximum 256 characters, got %d", key, len(str)) |
| 4095 | } |
| 4096 | } |
| 4097 | return attributes, nil |
| 4098 | } |
| 4099 | |
| 4100 | // isValidAttributeKey checks if an attribute key contains only alphanumeric characters and underscores |
| 4101 | func isValidAttributeKey(key string) bool { |