Skip to content

Commit

Permalink
Combine validation in SignUp and ChangePassword
Browse files Browse the repository at this point in the history
  • Loading branch information
gusah009 committed Jul 20, 2024
1 parent cc3c18b commit 411fda4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 30 deletions.
24 changes: 4 additions & 20 deletions api/types/user_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,16 @@ import (
"github.com/yorkie-team/yorkie/internal/validation"
)

// SignupFields is a set of fields that use to sign up to yorkie server.
// // must be same with [ChangePasswordFields].
type SignupFields struct {
// UserFields is a set of fields that use to sign up or change password to yorkie server.
type UserFields struct {
// Username is the name of user.
Username *string `bson:"username" validate:"required,min=2,max=30,slug"`

// Password is the password of user.
Password *string `bson:"password" validate:"required,min=8,max=30,alpha_num_special"`
}

// Validate validates the SignupFields.
func (i *SignupFields) Validate() error {
return validation.ValidateStruct(i)
}

// ChangePasswordFields is a set of fields that use to change password to yorkie server.
// must be same with [SignupFields].
type ChangePasswordFields struct {
// Username is the name of user.
Username *string `bson:"username" validate:"required,min=2,max=30,slug"`

// NewPassword is the new password of user
NewPassword *string `bson:"new_password" validate:"required,min=8,max=30,alpha_num_special"`
}

// Validate validates the ChangePasswordFields.
func (i *ChangePasswordFields) Validate() error {
// Validate validates the UserFields.
func (i *UserFields) Validate() error {
return validation.ValidateStruct(i)
}
16 changes: 8 additions & 8 deletions api/types/user_fields_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,56 +31,56 @@ func TestSignupFields(t *testing.T) {
t.Run("password validation test", func(t *testing.T) {
validUsername := "test"
validPassword := "pass123!"
fields := &types.SignupFields{
fields := &types.UserFields{
Username: &validUsername,
Password: &validPassword,
}
assert.NoError(t, fields.Validate())

invalidPassword := "1234"
fields = &types.SignupFields{
fields = &types.UserFields{
Username: &validUsername,
Password: &invalidPassword,
}
assert.ErrorAs(t, fields.Validate(), &structError)

invalidPassword = "abcd"
fields = &types.SignupFields{
fields = &types.UserFields{
Username: &validUsername,
Password: &invalidPassword,
}
assert.ErrorAs(t, fields.Validate(), &structError)

invalidPassword = "!@#$"
fields = &types.SignupFields{
fields = &types.UserFields{
Username: &validUsername,
Password: &invalidPassword,
}
assert.ErrorAs(t, fields.Validate(), &structError)

invalidPassword = "abcd1234"
fields = &types.SignupFields{
fields = &types.UserFields{
Username: &validUsername,
Password: &invalidPassword,
}
assert.ErrorAs(t, fields.Validate(), &structError)

invalidPassword = "abcd!@#$"
fields = &types.SignupFields{
fields = &types.UserFields{
Username: &validUsername,
Password: &invalidPassword,
}
assert.ErrorAs(t, fields.Validate(), &structError)

invalidPassword = "1234!@#$"
fields = &types.SignupFields{
fields = &types.UserFields{
Username: &validUsername,
Password: &invalidPassword,
}
assert.ErrorAs(t, fields.Validate(), &structError)

invalidPassword = "abcd1234!@abcd1234!@abcd1234!@1"
fields = &types.SignupFields{
fields = &types.UserFields{
Username: &validUsername,
Password: &invalidPassword,
}
Expand Down
4 changes: 2 additions & 2 deletions server/rpc/admin_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (s *adminServer) SignUp(
ctx context.Context,
req *connect.Request[api.SignUpRequest],
) (*connect.Response[api.SignUpResponse], error) {
fields := &types.SignupFields{Username: &req.Msg.Username, Password: &req.Msg.Password}
fields := &types.UserFields{Username: &req.Msg.Username, Password: &req.Msg.Password}
if err := fields.Validate(); err != nil {
return nil, err
}
Expand Down Expand Up @@ -123,7 +123,7 @@ func (s *adminServer) ChangePassword(
ctx context.Context,
req *connect.Request[api.ChangePasswordRequest],
) (*connect.Response[api.ChangePasswordResponse], error) {
fields := &types.ChangePasswordFields{Username: &req.Msg.Username, NewPassword: &req.Msg.NewPassword}
fields := &types.UserFields{Username: &req.Msg.Username, Password: &req.Msg.NewPassword}
if err := fields.Validate(); err != nil {
return nil, err
}
Expand Down

0 comments on commit 411fda4

Please sign in to comment.