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 (
)
var (
ErrEmptyID = errors.New("empty ID")
ErrEmptyID = errors.New("empty id")
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")
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
)
......@@ -26,31 +26,27 @@ func GenerateNewID() string {
return xid.New().String()
}
func ValidateID(id *string) error {
trimmedID := *id
trimmedID = strings.TrimSpace(trimmedID)
func ValidateID(id string) (string, error) {
trimmedID := strings.TrimSpace(id)
if trimmedID == "" {
return ErrEmptyID
return trimmedID, ErrEmptyID
}
if len(trimmedID) > MaxLengthID {
return ErrLongID
return trimmedID, ErrLongID
}
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 nil
return trimmedID, nil
}
func ValidateName(name *string) error {
trimmedName := *name
trimmedName = strings.TrimSpace(trimmedName)
func ValidateName(name string) (string, error) {
trimmedName := strings.TrimSpace(name)
if trimmedName == "" {
return ErrEmptyName
return trimmedName, ErrEmptyName
}
if len(trimmedName) > MaxLengthName {
return ErrLongName
return trimmedName, ErrLongName
}
*name = trimmedName
return nil
return trimmedName, nil
}
package id
import "testing"
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestValidateID(t *testing.T) {
tests := []struct {
name string
id string
result string
wantErr bool
}{
{
"Correct ID #1",
"test",
"test",
false,
},
{
"Correct ID #2",
"test_1",
"test_1",
false,
},
{
"Correct ID #3",
"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,
},
{
"Fail, ID starts with number",
"1test",
"1test",
true,
},
{
"Fail, ID to long",
"9QoulAlFbIcnQYLSudMistN1IczCWrXUTtN5EgNQJd516DN9UjXYJxieJ1RcsNcs1",
"9QoulAlFbIcnQYLSudMistN1IczCWrXUTtN5EgNQJd516DN9UjXYJxieJ1RcsNcs1",
true,
},
{
"Fail, ID with symbols",
"test!_)(&&",
"test!_)(&&",
true,
},
{
"Fail, ID is empty",
" ",
"",
true,
},
}
for _, tt := range tests {
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)
}
require.Equal(t, tt.result, res)
})
}
}
......@@ -52,34 +83,53 @@ func TestValidateName(t *testing.T) {
tests := []struct {
nameTest string
name string
result string
wantErr bool
}{
{
"Correct name #1",
"test",
"test",
false,
},
{
"Correct name #2",
"test_1",
"test_1",
false,
},
{
"Correct name #3",
"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,
},
{
"Fail, name to long",
"ChKRLdvWi0wYYPazuBXrIRtNFy96qGrhBDkuKQNd6N2DPV86IGdXVkeTjWj7qezKreIFUp9IUn03A8WJTTORHkgXAvkPuDVM8tVMcnHbR2hznooJ3gGUsXpn4uXo2QhsviHPyUKmE10GnkCOv9FgAMILNoFVHnIiSHI3cjWlGJglpS9YAMXFB1phOIRF5yol3jmPE7EeU1uZPUw9C2PChuksGsOuJQov07Zom0b13r6wOJv8PZVa4IKmjDDLGKlq1",
"ChKRLdvWi0wYYPazuBXrIRtNFy96qGrhBDkuKQNd6N2DPV86IGdXVkeTjWj7qezKreIFUp9IUn03A8WJTTORHkgXAvkPuDVM8tVMcnHbR2hznooJ3gGUsXpn4uXo2QhsviHPyUKmE10GnkCOv9FgAMILNoFVHnIiSHI3cjWlGJglpS9YAMXFB1phOIRF5yol3jmPE7EeU1uZPUw9C2PChuksGsOuJQov07Zom0b13r6wOJv8PZVa4IKmjDDLGKlq1",
true,
},
}
for _, tt := range tests {
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)
}
require.Equal(t, tt.result, res)
})
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment