-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
194 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,53 @@ | ||
{ | ||
"id": 1, | ||
"id": "test_form", | ||
"secret": "SuperSecretsString", | ||
"recipients": ["[email protected]"], | ||
"recipients": [ | ||
"[email protected]" | ||
], | ||
"sender": "[email protected]", | ||
"domains": ["www.example.com", "example.com"], | ||
"domains": [ | ||
"www.example.com", | ||
"example.com" | ||
], | ||
"validation": { | ||
"hcaptcha": { | ||
"enabled": false, | ||
"secret_key": "0x1234567890" | ||
}, | ||
"recaptcha": { | ||
"enabled": false, | ||
"secret_key": "0x1234567890" | ||
}, | ||
"fields": [ | ||
{ | ||
"name": "name", | ||
"type": "text", | ||
"required": true | ||
}, | ||
{ | ||
"name": "email", | ||
"type": "email", | ||
"required": true | ||
}, | ||
{ | ||
"name": "age", | ||
"type": "number", | ||
"required": true | ||
}, | ||
{ | ||
"name": "message", | ||
"required": true | ||
} | ||
] | ||
}, | ||
"content": { | ||
"subject": "New message through the www.example.com contact form", | ||
"fields": ["name", "email", "message"], | ||
"required_fields": ["name", "email"] | ||
"fields": [ | ||
"name", | ||
"email", | ||
"age", | ||
"message" | ||
] | ||
}, | ||
"server": { | ||
"host": "mail.example.com", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package validation | ||
|
||
import ( | ||
"fmt" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/wneessen/js-mailer/form" | ||
"net/http" | ||
"regexp" | ||
) | ||
|
||
// Field validates the form field based on its configured type | ||
func Field(r *http.Request, f *form.ValidationField) error { | ||
l := log.WithFields(log.Fields{ | ||
"action": "validation.Field", | ||
"fieldName": f.Name, | ||
}) | ||
|
||
if f.Required && r.Form.Get(f.Name) == "" { | ||
l.Debugf("Form is missing required field: %s", f.Name) | ||
return fmt.Errorf("field is required, but missing") | ||
} | ||
|
||
switch f.Type { | ||
case "text": | ||
return nil | ||
case "email": | ||
mailRegExp, err := regexp.Compile("^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$") | ||
if err != nil { | ||
l.Errorf("Failed to compile email comparsion regexp: %s", err) | ||
return nil | ||
} | ||
if !mailRegExp.Match([]byte(r.Form.Get(f.Name))) { | ||
l.Debugf("Form field is expected to be of type email but does not match this requirementd: %s", f.Name) | ||
return fmt.Errorf("field is expected to be of type email, but does not match") | ||
} | ||
case "number": | ||
numRegExp, err := regexp.Compile("^[0-9]+$") | ||
if err != nil { | ||
l.Errorf("Failed to compile email comparsion regexp: %s", err) | ||
return nil | ||
} | ||
if !numRegExp.Match([]byte(r.Form.Get(f.Name))) { | ||
l.Debugf("Form field is expected to be of type number but does not match this requirementd: %s", f.Name) | ||
return fmt.Errorf("field is expected to be of type number, but does not match") | ||
} | ||
case "bool": | ||
boolRegExp, err := regexp.Compile("^(?i)(true|false|0|1)$") | ||
if err != nil { | ||
l.Errorf("Failed to compile boolean comparsion regexp: %s", err) | ||
return nil | ||
} | ||
if !boolRegExp.Match([]byte(r.Form.Get(f.Name))) { | ||
l.Debugf("Form field is expected to be of type boolean but does not match this requirementd: %s", f.Name) | ||
return fmt.Errorf("field is expected to be of type bool, but does not match") | ||
} | ||
default: | ||
return nil | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters