Skip to content
Snippets Groups Projects
Commit c7a05319 authored by ko_oler's avatar ko_oler
Browse files

правки по ПР

parent d7fbac07
No related branches found
No related tags found
No related merge requests found
...@@ -14,11 +14,11 @@ const ( ...@@ -14,11 +14,11 @@ const (
) )
var ( var (
ErrEmptyID = errors.New("empty ID") ErrEmptyID = errors.New("empty id")
ErrNotValidID = errors.New("id contains invalid characters") ErrNotValidID = errors.New("id contains invalid characters")
ErrLongID = errors.New("id too long (max 64 symbols)") ErrLongID = errors.Errorf("id too long (max %d symbols)", MaxLengthID)
ErrEmptyName = errors.New("empty name") ErrEmptyName = errors.New("empty name")
ErrLongName = errors.New("name too long (max 256 symbols)") ErrLongName = errors.Errorf("name too long (max %d symbols)", MaxLengthName)
isValidID = regexp.MustCompile(`^[A-Za-z][A-Za-z0-9-_]*$`).MatchString isValidID = regexp.MustCompile(`^[A-Za-z][A-Za-z0-9-_]*$`).MatchString
) )
...@@ -26,31 +26,27 @@ func GenerateNewID() string { ...@@ -26,31 +26,27 @@ func GenerateNewID() string {
return xid.New().String() return xid.New().String()
} }
func ValidateID(id *string) error { func ValidateID(id string) (string, error) {
trimmedID := *id trimmedID := strings.TrimSpace(id)
trimmedID = strings.TrimSpace(trimmedID)
if trimmedID == "" { if trimmedID == "" {
return ErrEmptyID return trimmedID, ErrEmptyID
} }
if len(trimmedID) > MaxLengthID { if len(trimmedID) > MaxLengthID {
return ErrLongID return trimmedID, ErrLongID
} }
if !isValidID(trimmedID) { if !isValidID(trimmedID) {
return errors.WithDetail(ErrNotValidID, "must begin with latin letters and contain latin letters, numbers or '-', '_'") return trimmedID, errors.WithDetail(ErrNotValidID, "must begin with latin letters and contain latin letters, numbers or '-', '_'")
} }
*id = trimmedID return trimmedID, nil
return nil
} }
func ValidateName(name *string) error { func ValidateName(name string) (string, error) {
trimmedName := *name trimmedName := strings.TrimSpace(name)
trimmedName = strings.TrimSpace(trimmedName)
if trimmedName == "" { if trimmedName == "" {
return ErrEmptyName return trimmedName, ErrEmptyName
} }
if len(trimmedName) > MaxLengthName { if len(trimmedName) > MaxLengthName {
return ErrLongName return trimmedName, ErrLongName
} }
*name = trimmedName return trimmedName, nil
return nil
} }
package id package id
import "testing" import (
"testing"
"github.com/stretchr/testify/require"
)
func TestValidateID(t *testing.T) { func TestValidateID(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
id string id string
result string
wantErr bool wantErr bool
}{ }{
{ {
"Correct ID #1", "Correct ID #1",
"test", "test",
"test",
false, false,
}, },
{ {
"Correct ID #2", "Correct ID #2",
"test_1", "test_1",
"test_1",
false, false,
}, },
{ {
"Correct ID #3", "Correct ID #3",
"test_1-2", "test_1-2",
"test_1-2",
false,
},
{
"Trimmed ID #1",
" test_1-2 ",
"test_1-2",
false,
},
{
"Trimmed ID #2",
"test_1-2 ",
"test_1-2",
false, false,
}, },
{ {
"Fail, ID starts with number", "Fail, ID starts with number",
"1test", "1test",
"1test",
true, true,
}, },
{ {
"Fail, ID to long", "Fail, ID to long",
"9QoulAlFbIcnQYLSudMistN1IczCWrXUTtN5EgNQJd516DN9UjXYJxieJ1RcsNcs1", "9QoulAlFbIcnQYLSudMistN1IczCWrXUTtN5EgNQJd516DN9UjXYJxieJ1RcsNcs1",
"9QoulAlFbIcnQYLSudMistN1IczCWrXUTtN5EgNQJd516DN9UjXYJxieJ1RcsNcs1",
true, true,
}, },
{ {
"Fail, ID with symbols", "Fail, ID with symbols",
"test!_)(&&", "test!_)(&&",
"test!_)(&&",
true,
},
{
"Fail, ID is empty",
" ",
"",
true, true,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if err := ValidateID(&tt.id); (err != nil) != tt.wantErr { res, err := ValidateID(tt.id)
if (err != nil) != tt.wantErr {
t.Errorf("ValidateID() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("ValidateID() error = %v, wantErr %v", err, tt.wantErr)
} }
require.Equal(t, tt.result, res)
}) })
} }
} }
...@@ -52,34 +83,53 @@ func TestValidateName(t *testing.T) { ...@@ -52,34 +83,53 @@ func TestValidateName(t *testing.T) {
tests := []struct { tests := []struct {
nameTest string nameTest string
name string name string
result string
wantErr bool wantErr bool
}{ }{
{ {
"Correct name #1", "Correct name #1",
"test", "test",
"test",
false, false,
}, },
{ {
"Correct name #2", "Correct name #2",
"test_1", "test_1",
"test_1",
false, false,
}, },
{ {
"Correct name #3", "Correct name #3",
"test_1-2", "test_1-2",
"test_1-2",
false,
},
{
"Trimmed name #1",
" test_1-3 ",
"test_1-3",
false,
},
{
"Trimmed name #2",
"test_1234 ",
"test_1234",
false, false,
}, },
{ {
"Fail, name to long", "Fail, name to long",
"ChKRLdvWi0wYYPazuBXrIRtNFy96qGrhBDkuKQNd6N2DPV86IGdXVkeTjWj7qezKreIFUp9IUn03A8WJTTORHkgXAvkPuDVM8tVMcnHbR2hznooJ3gGUsXpn4uXo2QhsviHPyUKmE10GnkCOv9FgAMILNoFVHnIiSHI3cjWlGJglpS9YAMXFB1phOIRF5yol3jmPE7EeU1uZPUw9C2PChuksGsOuJQov07Zom0b13r6wOJv8PZVa4IKmjDDLGKlq1", "ChKRLdvWi0wYYPazuBXrIRtNFy96qGrhBDkuKQNd6N2DPV86IGdXVkeTjWj7qezKreIFUp9IUn03A8WJTTORHkgXAvkPuDVM8tVMcnHbR2hznooJ3gGUsXpn4uXo2QhsviHPyUKmE10GnkCOv9FgAMILNoFVHnIiSHI3cjWlGJglpS9YAMXFB1phOIRF5yol3jmPE7EeU1uZPUw9C2PChuksGsOuJQov07Zom0b13r6wOJv8PZVa4IKmjDDLGKlq1",
"ChKRLdvWi0wYYPazuBXrIRtNFy96qGrhBDkuKQNd6N2DPV86IGdXVkeTjWj7qezKreIFUp9IUn03A8WJTTORHkgXAvkPuDVM8tVMcnHbR2hznooJ3gGUsXpn4uXo2QhsviHPyUKmE10GnkCOv9FgAMILNoFVHnIiSHI3cjWlGJglpS9YAMXFB1phOIRF5yol3jmPE7EeU1uZPUw9C2PChuksGsOuJQov07Zom0b13r6wOJv8PZVa4IKmjDDLGKlq1",
true, true,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if err := ValidateName(&tt.name); (err != nil) != tt.wantErr { res, err := ValidateName(tt.name)
if (err != nil) != tt.wantErr {
t.Errorf("ValidateName() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("ValidateName() error = %v, wantErr %v", err, tt.wantErr)
} }
require.Equal(t, tt.result, res)
}) })
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment