summaryrefslogtreecommitdiff
path: root/internal/user/input.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/user/input.go')
-rw-r--r--internal/user/input.go65
1 files changed, 65 insertions, 0 deletions
diff --git a/internal/user/input.go b/internal/user/input.go
new file mode 100644
index 0000000..38db678
--- /dev/null
+++ b/internal/user/input.go
@@ -0,0 +1,65 @@
+package user
+
+import (
+ "errors"
+ "fmt"
+ "regexp"
+)
+
+type RegisterInput struct {
+ Username string
+ Email string
+ Phone string
+ Password string
+}
+
+type CreateInput struct {
+ Username string
+ Email string
+ Phone string
+ PwHash string
+}
+
+var ErrInvalidInput = errors.New("invalid input")
+var ErrUserExists = errors.New("user already exists")
+var ErrUserNotFound = errors.New("user not found")
+
+var usrnameRegExp = regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9_-]*$`)
+var mailRegExp = regexp.MustCompile(`^[^@\s]+@[^@\s]+\.[^@\s]+$`)
+var phoneRegExp = regexp.MustCompile(`^\+?[0-9][0-9\s-]*$`)
+
+func (in *RegisterInput) validate() error {
+ if len(in.Password) < 8 {
+ return fmt.Errorf("%w: password too short - min is 8", ErrInvalidInput)
+ }
+
+ if len(in.Password) > 72 {
+ return fmt.Errorf("%w: password too long - max is 72", ErrInvalidInput)
+ }
+
+ if in.Username == "" {
+ return fmt.Errorf("%w: username is mandatory", ErrInvalidInput)
+ }
+
+ if !usrnameRegExp.MatchString(in.Username) {
+ return fmt.Errorf("%w: bad username - must start with a letter", ErrInvalidInput)
+ }
+
+ if len(in.Username) < 3 {
+ return fmt.Errorf("%w: username too short - min is 3 chars", ErrInvalidInput)
+ }
+
+ if in.Email == "" {
+ return fmt.Errorf("%w: email is mandatory", ErrInvalidInput)
+ }
+
+ if !mailRegExp.MatchString(in.Email) {
+ return fmt.Errorf("%w: bad email format", ErrInvalidInput)
+ }
+
+ if in.Phone != "" && !phoneRegExp.MatchString(in.Phone) {
+ return fmt.Errorf("%w: bad phone format", ErrInvalidInput)
+ }
+
+ return nil
+}