Skip to content

Commit

Permalink
Rename StructError to FormError
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerwins committed Oct 31, 2024
1 parent 5e54e52 commit 148ac37
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 29 deletions.
16 changes: 8 additions & 8 deletions api/types/updatable_project_fields_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

func TestUpdatableProjectFields(t *testing.T) {
var structError *validation.StructError
var formErr *validation.FormError
t.Run("validation test", func(t *testing.T) {
newName := "changed-name"
newAuthWebhookURL := "http://localhost:3000"
Expand Down Expand Up @@ -68,7 +68,7 @@ func TestUpdatableProjectFields(t *testing.T) {
AuthWebhookMethods: &newAuthWebhookMethods,
ClientDeactivateThreshold: &newClientDeactivateThreshold,
}
assert.ErrorAs(t, fields.Validate(), &structError)
assert.ErrorAs(t, fields.Validate(), &formErr)
})

t.Run("project name format test", func(t *testing.T) {
Expand All @@ -82,36 +82,36 @@ func TestUpdatableProjectFields(t *testing.T) {
fields = &types.UpdatableProjectFields{
Name: &invalidName,
}
assert.ErrorAs(t, fields.Validate(), &structError)
assert.ErrorAs(t, fields.Validate(), &formErr)

reservedName := "new"
fields = &types.UpdatableProjectFields{
Name: &reservedName,
}
assert.ErrorAs(t, fields.Validate(), &structError)
assert.ErrorAs(t, fields.Validate(), &formErr)

reservedName = "default"
fields = &types.UpdatableProjectFields{
Name: &reservedName,
}
assert.ErrorAs(t, fields.Validate(), &structError)
assert.ErrorAs(t, fields.Validate(), &formErr)

invalidName = "1"
fields = &types.UpdatableProjectFields{
Name: &invalidName,
}
assert.ErrorAs(t, fields.Validate(), &structError)
assert.ErrorAs(t, fields.Validate(), &formErr)

invalidName = "over_30_chracaters_is_invalid_name"
fields = &types.UpdatableProjectFields{
Name: &invalidName,
}
assert.ErrorAs(t, fields.Validate(), &structError)
assert.ErrorAs(t, fields.Validate(), &formErr)

invalidName = "invalid/name"
fields = &types.UpdatableProjectFields{
Name: &invalidName,
}
assert.ErrorAs(t, fields.Validate(), &structError)
assert.ErrorAs(t, fields.Validate(), &formErr)
})
}
16 changes: 8 additions & 8 deletions api/types/user_fields_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

func TestSignupFields(t *testing.T) {
var structError *validation.StructError
var formErr *validation.FormError

t.Run("password validation test", func(t *testing.T) {
validUsername := "test"
Expand All @@ -42,48 +42,48 @@ func TestSignupFields(t *testing.T) {
Username: &validUsername,
Password: &invalidPassword,
}
assert.ErrorAs(t, fields.Validate(), &structError)
assert.ErrorAs(t, fields.Validate(), &formErr)

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

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

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

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

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

invalidPassword = "abcd1234!@abcd1234!@abcd1234!@1"
fields = &types.UserFields{
Username: &validUsername,
Password: &invalidPassword,
}
assert.ErrorAs(t, fields.Validate(), &structError)
assert.ErrorAs(t, fields.Validate(), &formErr)
})
}
12 changes: 6 additions & 6 deletions internal/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@ func (e Violation) Error() string {
return e.Err.Error()
}

// StructError is the error returned by the validation of struct.
type StructError struct {
// FormError represents the error of the form validation.
type FormError struct {
Violations []Violation
}

// Error returns the error message.
func (s StructError) Error() string {
func (s FormError) Error() string {
sb := strings.Builder{}

for _, v := range s.Violations {
Expand Down Expand Up @@ -223,16 +223,16 @@ func Validate(v string, tagOrRules []interface{}) error {
// ValidateStruct validates the struct
func ValidateStruct(s interface{}) error {
if err := defaultValidator.Struct(s); err != nil {
structError := &StructError{}
formErr := &FormError{}
for _, e := range err.(validator.ValidationErrors) {
structError.Violations = append(structError.Violations, Violation{
formErr.Violations = append(formErr.Violations, Violation{
Tag: e.Tag(),
Field: e.StructField(),
Err: e,
Description: e.Translate(trans),
})
}
return structError
return formErr
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions internal/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ func TestValidation(t *testing.T) {
user := User{Name: "invalid-key-$-wrong-string-value", Country: "korea"}

err := ValidateStruct(user)
structError := err.(*StructError)
assert.Len(t, structError.Violations, 2, "user should be invalid")
formErr := err.(*FormError)
assert.Len(t, formErr.Violations, 2, "user should be invalid")
})

t.Run("custom rule test", func(t *testing.T) {
Expand Down
10 changes: 5 additions & 5 deletions server/rpc/connecthelper/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ func richErrorToConnectError(err error) (*connect.Error, bool) {
return connectErr, true
}

// structErrorToConnectError returns connect.Error from the given struct error.
func structErrorToConnectError(err error) (*connect.Error, bool) {
var invalidFieldsError *validation.StructError
// formErrorToConnectError returns connect.Error from the given form error.
func formErrorToConnectError(err error) (*connect.Error, bool) {
var invalidFieldsError *validation.FormError
if !errors.As(err, &invalidFieldsError) {
return nil, false
}
Expand All @@ -244,7 +244,7 @@ func structErrorToConnectError(err error) (*connect.Error, bool) {
}

func badRequestFromError(err error) (*errdetails.BadRequest, bool) {
var invalidFieldsError *validation.StructError
var invalidFieldsError *validation.FormError
if !errors.As(err, &invalidFieldsError) {
return nil, false
}
Expand Down Expand Up @@ -278,7 +278,7 @@ func ToStatusError(err error) error {
return err
}

if err, ok := structErrorToConnectError(err); ok {
if err, ok := formErrorToConnectError(err); ok {
return err
}

Expand Down

0 comments on commit 148ac37

Please sign in to comment.