44 "fmt"
55 "regexp"
66 "strings"
7+ "unicode/utf8"
78)
89
910// Validator provides common validation methods
@@ -22,9 +23,12 @@ func (v *Validator) ValidatePostContent(content interface{}, _ int) error {
2223 return nil
2324}
2425
25- // ValidateTextLength validates text doesn't exceed maximum length
26+ // ValidateTextLength validates text doesn't exceed maximum length.
27+ // The Threads API limits text posts to 500 characters (Unicode code points),
28+ // not 500 bytes. Using utf8.RuneCountInString ensures CJK and other
29+ // multi-byte characters are correctly counted as 1 character each.
2630func (v * Validator ) ValidateTextLength (text string , fieldName string ) error {
27- if len (text ) > MaxTextLength {
31+ if utf8 . RuneCountInString (text ) > MaxTextLength {
2832 return NewValidationError (400 ,
2933 fmt .Sprintf ("%s too long" , fieldName ),
3034 fmt .Sprintf ("%s is limited to %d characters" , fieldName , MaxTextLength ),
@@ -85,10 +89,10 @@ func (v *Validator) ValidateTextAttachment(textAttachment *TextAttachment) error
8589 "text_attachment.plaintext" )
8690 }
8791
88- if len (textAttachment .Plaintext ) > MaxTextAttachmentLength {
92+ if utf8 . RuneCountInString (textAttachment .Plaintext ) > MaxTextAttachmentLength {
8993 return NewValidationError (400 ,
9094 "Text attachment plaintext too long" ,
91- fmt .Sprintf ("Text attachment plaintext is limited to %d characters (currently %d)" , MaxTextAttachmentLength , len (textAttachment .Plaintext )),
95+ fmt .Sprintf ("Text attachment plaintext is limited to %d characters (currently %d)" , MaxTextAttachmentLength , utf8 . RuneCountInString (textAttachment .Plaintext )),
9296 "text_attachment.plaintext" )
9397 }
9498
0 commit comments