ValidateConversationID reports whether a container identifier matches its field grammar.
(id string, field string)
| 142 | |
| 143 | // ValidateConversationID reports whether a container identifier matches its field grammar. |
| 144 | func ValidateConversationID(id string, field string) error { |
| 145 | trimmed := strings.TrimSpace(id) |
| 146 | if trimmed == "" { |
| 147 | return fmt.Errorf("%w: %s is required", ErrMissingField, field) |
| 148 | } |
| 149 | switch field { |
| 150 | case validateThreadIDKey: |
| 151 | if !threadIDPattern.MatchString(trimmed) { |
| 152 | return fmt.Errorf("%w: thread_id=%q", ErrInvalidField, id) |
| 153 | } |
| 154 | case validateDirectIDKey: |
| 155 | if !directIDPattern.MatchString(trimmed) { |
| 156 | return fmt.Errorf("%w: direct_id=%q", ErrInvalidField, id) |
| 157 | } |
| 158 | default: |
| 159 | if strings.ContainsAny(trimmed, `/\`) || containsControlCharacter(trimmed) || len(trimmed) > 128 { |
| 160 | return fmt.Errorf("%w: %s=%q", ErrInvalidField, field, id) |
| 161 | } |
| 162 | } |
| 163 | return nil |
| 164 | } |
| 165 | |
| 166 | // ValidateWorkID reports whether a work id can safely cross the network boundary. |
| 167 | func ValidateWorkID(id string) error { |