-
Notifications
You must be signed in to change notification settings - Fork 5
/
form_submission.go
45 lines (38 loc) · 1020 Bytes
/
form_submission.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package mailbear
import (
"fmt"
"github.com/badoux/checkmail"
)
// FormSubmission represent a submitted form
type FormSubmission struct {
Name string `json:"name" form:"name"`
Email string `json:"email" form:"email"`
Subject string `json:"subject" form:"subject"`
Content string `json:"content" form:"content"`
FormID string `json:"-"`
}
// Validate validates all fields of a form submission.
func (f *FormSubmission) Validate() error {
if f.Name == "" {
// name is optional
}
if f.Email == "" {
return fmt.Errorf("field 'email'cannot be empty")
}
if f.Subject == "" {
return fmt.Errorf("field 'subject' cannot be empty")
}
if f.Content == "" {
return fmt.Errorf("field 'content' cannot be empty")
}
// Validate ID, although it should always be set manually
if f.FormID == "" {
return fmt.Errorf("id cannot be empty")
}
// Validate format of email
err := checkmail.ValidateFormat(f.Email)
if err != nil {
return fmt.Errorf("invalid email address: %v", err)
}
return nil
}