add workspace, validation
This commit is contained in:
@@ -0,0 +1,18 @@
|
|||||||
|
package validate
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"regexp"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Based on https://stackoverflow.com/a/201378
|
||||||
|
var emailRegexp = regexp.MustCompile("^(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\\])$")
|
||||||
|
|
||||||
|
// Email validates an email address.
|
||||||
|
func Email(value string) error {
|
||||||
|
if !emailRegexp.MatchString(value) {
|
||||||
|
return errors.New("Invalid email address")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package validate
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestInvalidEmail(t *testing.T) {
|
||||||
|
valid := []string{
|
||||||
|
"testexample.com",
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, email := range valid {
|
||||||
|
if err := Email(email); err == nil {
|
||||||
|
t.Errorf("%s is not a valid email", email)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestValidEmail(t *testing.T) {
|
||||||
|
valid := []string{
|
||||||
|
"test@example.com",
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, email := range valid {
|
||||||
|
if err := Email(email); err != nil {
|
||||||
|
t.Errorf("%s is a valid email", email)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package validate
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"regexp"
|
||||||
|
)
|
||||||
|
|
||||||
|
var uuidRegexp = regexp.MustCompile("^[a-f0-9]{8}(-[a-f0-9]{4}){3}-[a-f0-9]{12}$")
|
||||||
|
|
||||||
|
// UUID validates a UUID string.
|
||||||
|
// The UUID must be formatted with separators.
|
||||||
|
func UUID(value string) error {
|
||||||
|
if !uuidRegexp.MatchString(value) {
|
||||||
|
return errors.New("Invalid UUID")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package validate
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestInvalidUUID(t *testing.T) {
|
||||||
|
invalid := []string{
|
||||||
|
"Not a UUID",
|
||||||
|
"00000000-00-0000-0000-00000000000000",
|
||||||
|
"00000000000000000000000000000000",
|
||||||
|
"01234567-89ab-cdef-ghij-klmnopqrstuv",
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, uuid := range invalid {
|
||||||
|
if err := UUID(uuid); err == nil {
|
||||||
|
t.Errorf("%s is not a valid UUID", uuid)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestValidUUID(t *testing.T) {
|
||||||
|
valid := []string{
|
||||||
|
"00000000-0000-0000-0000-000000000000",
|
||||||
|
"01234567-89ab-cdef-0123-456789abcdef",
|
||||||
|
"abcdef01-2345-6789-abcd-ef0123456789",
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, uuid := range valid {
|
||||||
|
if err := UUID(uuid); err != nil {
|
||||||
|
t.Errorf("%s is a valid UUID", uuid)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user