validates a tevent that was just created (not for validating out of the DB, or an uploaded TEvent) checks that TS is pretty current (or unset)
(current bool)
| 249 | // validates a tevent that was just created (not for validating out of the DB, or an uploaded TEvent) |
| 250 | // checks that TS is pretty current (or unset) |
| 251 | func (te *TEvent) Validate(current bool) error { |
| 252 | if te == nil { |
| 253 | return fmt.Errorf("TEvent cannot be nil") |
| 254 | } |
| 255 | if te.Event == "" { |
| 256 | return fmt.Errorf("TEvent.Event cannot be empty") |
| 257 | } |
| 258 | if !eventNameRe.MatchString(te.Event) { |
| 259 | return fmt.Errorf("TEvent.Event invalid: %q", te.Event) |
| 260 | } |
| 261 | if !ValidEventNames[te.Event] { |
| 262 | return fmt.Errorf("TEvent.Event not valid: %q", te.Event) |
| 263 | } |
| 264 | if te.Uuid == "" { |
| 265 | return fmt.Errorf("TEvent.Uuid cannot be empty") |
| 266 | } |
| 267 | _, err := uuid.Parse(te.Uuid) |
| 268 | if err != nil { |
| 269 | return fmt.Errorf("TEvent.Uuid invalid: %v", err) |
| 270 | } |
| 271 | if current { |
| 272 | if te.Ts != 0 { |
| 273 | now := time.Now().UnixMilli() |
| 274 | if te.Ts > now+60000 || te.Ts < now-60000 { |
| 275 | return fmt.Errorf("TEvent.Ts is not current: %d", te.Ts) |
| 276 | } |
| 277 | } |
| 278 | } else { |
| 279 | if te.Ts == 0 { |
| 280 | return fmt.Errorf("TEvent.Ts must be set") |
| 281 | } |
| 282 | if te.TsLocal == "" { |
| 283 | return fmt.Errorf("TEvent.TsLocal must be set") |
| 284 | } |
| 285 | t, err := time.Parse(time.RFC3339, te.TsLocal) |
| 286 | if err != nil { |
| 287 | return fmt.Errorf("TEvent.TsLocal parse error: %v", err) |
| 288 | } |
| 289 | now := time.Now() |
| 290 | if t.Before(now.Add(-30*24*time.Hour)) || t.After(now.Add(2*24*time.Hour)) { |
| 291 | return fmt.Errorf("tslocal out of valid range") |
| 292 | } |
| 293 | } |
| 294 | barr, err := json.Marshal(te.Props) |
| 295 | if err != nil { |
| 296 | return fmt.Errorf("TEvent.Props JSON error: %v", err) |
| 297 | } |
| 298 | if len(barr) > 20000 { |
| 299 | return fmt.Errorf("TEvent.Props too large: %d", len(barr)) |
| 300 | } |
| 301 | return nil |
| 302 | } |