api/event: remove legacy validate function; use domain map

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I9a68733cf16b09ef6381161452bda1e56a6a6964
This commit is contained in:
raf 2026-03-07 12:44:35 +03:00
commit 4189d14d65
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF
2 changed files with 17 additions and 67 deletions

View file

@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"io"
"slices"
"notashelf.dev/watchdog/internal/limits"
)
@ -40,40 +39,8 @@ func ParseEvent(body io.Reader) (*Event, error) {
return &event, nil
}
// Validate checks if the event is valid for the given domains
func (e *Event) Validate(allowedDomains []string) error {
if e.Domain == "" {
return fmt.Errorf("domain required")
}
// Check if domain is in allowed list
allowed := slices.Contains(allowedDomains, e.Domain)
if !allowed {
return fmt.Errorf("domain not allowed")
}
if e.Path == "" {
return fmt.Errorf("path required")
}
if len(e.Path) > limits.MaxPathLen {
return fmt.Errorf("path too long")
}
if len(e.Referrer) > limits.MaxRefLen {
return fmt.Errorf("referrer too long")
}
// Validate screen width is in reasonable range
if e.Width < 0 || e.Width > limits.MaxWidth {
return fmt.Errorf("invalid width")
}
return nil
}
// ValidateWithMap checks if the event is valid using a domain map (O(1) lookup)
func (e *Event) ValidateWithMap(allowedDomains map[string]bool) error {
// Validate checks if the event is valid using a domain map
func (e *Event) Validate(allowedDomains map[string]bool) error {
if e.Domain == "" {
return fmt.Errorf("domain required")
}