Validate document content length by extracted text, not JSON size
Add Node.TextLength() to walk the ProseMirror tree and sum actual user text. Introduce ProseMirrorDocumentMaxTextLength validator that enforces a 50k character limit on extracted text, paired with a 500k byte safety cap on the raw JSON string. Extend e2e tests for content length. Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
@@ -14,7 +14,10 @@
|
||||
|
||||
package validator
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestProseMirrorDocumentContent(t *testing.T) {
|
||||
t.Parallel()
|
||||
@@ -51,3 +54,45 @@ func TestProseMirrorDocumentContent(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func proseMirrorDoc(text string) string {
|
||||
return `{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"` + text + `"}]}]}`
|
||||
}
|
||||
|
||||
func TestProseMirrorDocumentMaxTextLength(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const maxLen = 10
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
value any
|
||||
wantError bool
|
||||
wantCode ErrorCode
|
||||
}{
|
||||
{"nil value", nil, false, ""},
|
||||
{"nil *string", (*string)(nil), false, ""},
|
||||
{"empty string", "", false, ""},
|
||||
{"whitespace only", " \n\t ", false, ""},
|
||||
{"under limit", proseMirrorDoc("hello"), false, ""},
|
||||
{"at limit", proseMirrorDoc(strings.Repeat("a", maxLen)), false, ""},
|
||||
{"over limit", proseMirrorDoc(strings.Repeat("a", maxLen+1)), true, ErrorCodeTooLong},
|
||||
{"invalid json skipped", "not json", false, ""},
|
||||
{"non-string", 42, true, ErrorCodeInvalidFormat},
|
||||
}
|
||||
|
||||
fn := ProseMirrorDocumentMaxTextLength(maxLen)
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
err := fn(tt.value)
|
||||
if (err != nil) != tt.wantError {
|
||||
t.Errorf("ProseMirrorDocumentMaxTextLength() error = %v, wantError %v", err, tt.wantError)
|
||||
}
|
||||
if err != nil && tt.wantCode != "" && err.Code != tt.wantCode {
|
||||
t.Errorf("expected code %s, got %s", tt.wantCode, err.Code)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user