From dd6ade211a107519af8b5ea7295d76158a29f0f4 Mon Sep 17 00:00:00 2001
From: ko_oler <kooler89@gmail.com>
Date: Mon, 19 Jun 2023 20:59:38 +0300
Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?=
 =?UTF-8?q?=D0=BD=D0=B0=20=D0=BE=D0=B1=D1=89=D0=B0=D1=8F=20=D1=84=D1=83?=
 =?UTF-8?q?=D0=BD=D0=BA=D1=86=D0=B8=D1=8F=20=D0=B2=D0=B0=D0=BB=D0=B8=D0=B4?=
 =?UTF-8?q?=D0=B0=D1=86=D0=B8=D0=B8=20=D0=B8=20=D0=BF=D0=BE=D0=BB=D1=83?=
 =?UTF-8?q?=D1=87=D0=B5=D0=BD=D0=B8=D1=8F=20ID/=D0=BD=D0=B0=D0=B7=D0=B2?=
 =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D1=8F=20=D1=81=D1=83=D1=89=D0=BD=D0=BE=D1=81?=
 =?UTF-8?q?=D1=82=D0=B5=D0=B9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 pkg/id/id.go      | 33 ++++++++++++++++-
 pkg/id/id_test.go | 90 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 122 insertions(+), 1 deletion(-)
 create mode 100644 pkg/id/id_test.go

diff --git a/pkg/id/id.go b/pkg/id/id.go
index cfb8b639..090fda2c 100644
--- a/pkg/id/id.go
+++ b/pkg/id/id.go
@@ -1,7 +1,38 @@
 package id
 
-import "github.com/rs/xid"
+import (
+	"regexp"
+
+	"git.perx.ru/perxis/perxis-go/pkg/errors"
+	"github.com/rs/xid"
+)
+
+const (
+	IDMaxLength   = 64
+	NameMaxLength = 256
+)
+
+var isValidID = regexp.MustCompile(`^[A-Za-z][A-Za-z0-9-_]*$`).MatchString
 
 func GenerateNewID() string {
 	return xid.New().String()
 }
+
+func ValidateID(id string) error {
+	if len(id) > IDMaxLength {
+		return errors.New("invalid id: too long (max 64 symbols)")
+	}
+	if !isValidID(id) {
+		return errors.New("invalid id: must begin with latin letters and contain latin letters, numbers or '-', '_'")
+	}
+
+	return nil
+}
+
+func ValidateName(name string) error {
+	if len(name) > NameMaxLength {
+		return errors.New("invalid name: too long (max 256 symbols)")
+	}
+
+	return nil
+}
diff --git a/pkg/id/id_test.go b/pkg/id/id_test.go
new file mode 100644
index 00000000..640b67da
--- /dev/null
+++ b/pkg/id/id_test.go
@@ -0,0 +1,90 @@
+package id
+
+import "testing"
+
+func TestValidateID(t *testing.T) {
+	tests := []struct {
+		name    string
+		id      string
+		wantErr bool
+	}{
+		{
+			"Correct ID #1",
+			"test",
+			false,
+		},
+		{
+			"Correct ID #2",
+			"test_1",
+			false,
+		},
+		{
+			"Correct ID #3",
+			"test_1-2",
+			false,
+		},
+		{
+			"Fail, ID starts with number",
+			"1test",
+			true,
+		},
+		{
+			"Fail, ID starts with whitespace",
+			" test",
+			true,
+		},
+		{
+			"Fail, ID to long",
+			"9QoulAlFbIcnQYLSudMistN1IczCWrXUTtN5EgNQJd516DN9UjXYJxieJ1RcsNcs1",
+			true,
+		},
+		{
+			"Fail, ID with symbols",
+			"test!_)(&&",
+			true,
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			if err := ValidateID(tt.id); (err != nil) != tt.wantErr {
+				t.Errorf("ValidateID() error = %v, wantErr %v", err, tt.wantErr)
+			}
+		})
+	}
+}
+
+func TestValidateName(t *testing.T) {
+	tests := []struct {
+		nameTest string
+		name     string
+		wantErr  bool
+	}{
+		{
+			"Correct name #1",
+			"test",
+			false,
+		},
+		{
+			"Correct name #2",
+			"test_1",
+			false,
+		},
+		{
+			"Correct name #3",
+			"test_1-2",
+			false,
+		},
+		{
+			"Fail, name to long",
+			"ChKRLdvWi0wYYPazuBXrIRtNFy96qGrhBDkuKQNd6N2DPV86IGdXVkeTjWj7qezKreIFUp9IUn03A8WJTTORHkgXAvkPuDVM8tVMcnHbR2hznooJ3gGUsXpn4uXo2QhsviHPyUKmE10GnkCOv9FgAMILNoFVHnIiSHI3cjWlGJglpS9YAMXFB1phOIRF5yol3jmPE7EeU1uZPUw9C2PChuksGsOuJQov07Zom0b13r6wOJv8PZVa4IKmjDDLGKlq1",
+			true,
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			if err := ValidateName(tt.name); (err != nil) != tt.wantErr {
+				t.Errorf("ValidateName() error = %v, wantErr %v", err, tt.wantErr)
+			}
+		})
+	}
+}
-- 
GitLab