Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
primitives
Manage
Activity
Members
Labels
Automate
Agent sessions
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
elixxir
primitives
Commits
40a20755
Commit
40a20755
authored
Jun 13, 2023
by
benjamin
Browse files
Options
Downloads
Patches
Plain Diff
added nickname validity checking
parent
9a25e2d3
No related branches found
No related tags found
2 merge requests
!32
added nickname validity checking
,
!24
Release Updates for Channels
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
nicknames/isValid.go
+43
-0
43 additions, 0 deletions
nicknames/isValid.go
nicknames/isValid_test.go
+56
-0
56 additions, 0 deletions
nicknames/isValid_test.go
with
99 additions
and
0 deletions
nicknames/isValid.go
0 → 100644
+
43
−
0
View file @
40a20755
package
nicknames
import
(
"github.com/pkg/errors"
jww
"github.com/spf13/jwalterweatherman"
)
const
(
MinNicknameLength
=
3
MaxNicknameLength
=
24
)
var
ErrNicknameTooShort
=
errors
.
Errorf
(
"nicknames must be at least "
+
"%d characters in length"
,
MinNicknameLength
)
var
ErrNicknameTooLong
=
errors
.
Errorf
(
"nicknames must be %d "
+
"characters in length or less"
,
MaxNicknameLength
)
// IsValid checks if a nickname is valid.
//
// Rules:
// - A nickname must not be longer than 24 characters.
// - A nickname must not be shorter than 1 character.
// - A nickname may be blank, this will be treated by the system as
// no nickname
//
// TODO: Add character filtering.
func
IsValid
(
nick
string
)
error
{
if
nick
==
""
{
jww
.
INFO
.
Printf
(
"empty nickname passed, treating like no "
+
"nickname"
)
return
nil
}
runeNick
:=
[]
rune
(
nick
)
if
len
(
runeNick
)
<
MinNicknameLength
{
return
errors
.
WithStack
(
ErrNicknameTooShort
)
}
if
len
(
runeNick
)
>
MaxNicknameLength
{
return
errors
.
WithStack
(
ErrNicknameTooLong
)
}
return
nil
}
This diff is collapsed.
Click to expand it.
nicknames/isValid_test.go
0 → 100644
+
56
−
0
View file @
40a20755
package
nicknames
import
(
"github.com/pkg/errors"
"testing"
)
func
TestIsNicknameValid
(
t
*
testing
.
T
)
{
// test that behavior for an empty nickname is correct
if
err
:=
IsValid
(
""
);
err
!=
nil
{
t
.
Errorf
(
"Empty nickname should be valid, received: %+v"
,
err
)
}
nicknameSource
:=
"Sodium, atomic number 11, was first isolated by Humphry "
+
"Davy in 1807. A chemical component of salt, he named it Na in honor "
+
"of the saltiest region on earth, North America."
// test that behavior for too short nicknames is correct
for
i
:=
1
;
i
<
MinNicknameLength
;
i
++
{
nick
:=
nicknameSource
[
:
i
]
if
err
:=
IsValid
(
nick
);
err
!=
nil
&&
!
errors
.
Is
(
err
,
ErrNicknameTooShort
)
{
t
.
Errorf
(
"Wrong error returned from nicknames.IsValid() "
+
"with too short input of length %d: %+v"
,
i
,
err
)
}
else
if
err
==
nil
{
t
.
Errorf
(
"No error returned from nicknames.IsValid() "
+
"with too short input of length %d"
,
i
)
}
}
// test that behavior for too long nicknames is correct
for
i
:=
MaxNicknameLength
+
1
;
i
<
MaxNicknameLength
*
5
;
i
++
{
nick
:=
nicknameSource
[
:
i
]
if
err
:=
IsValid
(
nick
);
err
!=
nil
&&
!
errors
.
Is
(
err
,
ErrNicknameTooLong
)
{
t
.
Errorf
(
"Wrong error returned from nicknames.IsValid() "
+
"with too long input of length %d: %+v"
,
i
,
err
)
}
else
if
err
==
nil
{
t
.
Errorf
(
"No error returned from nicknames.IsValid() "
+
"with too long input of length %d"
,
i
)
}
}
// test that behavior for valid nicknames is correct
for
i
:=
MinNicknameLength
;
i
<=
MaxNicknameLength
;
i
++
{
nick
:=
nicknameSource
[
:
i
]
if
err
:=
IsValid
(
nick
);
err
!=
nil
{
t
.
Errorf
(
"Error returned from nicknames.IsValid() "
+
"with valid nickname of input of length %d: %+v"
,
i
,
err
)
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
sign in
to comment