commit f41d6f248a9388286bbaece5f2a5be3170159abc Author: Aneurin Barker Snook Date: Fri Oct 6 11:58:24 2023 +0100 add workspace, validation diff --git a/email.go b/email.go new file mode 100644 index 0000000..2365d62 --- /dev/null +++ b/email.go @@ -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 +} diff --git a/email_test.go b/email_test.go new file mode 100644 index 0000000..95415c3 --- /dev/null +++ b/email_test.go @@ -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) + } + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..1a55cba --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/recipeer/go/validate + +go 1.21.1 diff --git a/uuid.go b/uuid.go new file mode 100644 index 0000000..d6915e6 --- /dev/null +++ b/uuid.go @@ -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 +} diff --git a/uuid_test.go b/uuid_test.go new file mode 100644 index 0000000..665b9dc --- /dev/null +++ b/uuid_test.go @@ -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) + } + } +}