diff --git a/api/types/updatable_project_fields_test.go b/api/types/updatable_project_fields_test.go index cba41401d..e8e61263b 100644 --- a/api/types/updatable_project_fields_test.go +++ b/api/types/updatable_project_fields_test.go @@ -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" @@ -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) { @@ -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) }) } diff --git a/api/types/user_fields_test.go b/api/types/user_fields_test.go index 15e30339a..e60aae49b 100644 --- a/api/types/user_fields_test.go +++ b/api/types/user_fields_test.go @@ -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" @@ -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) }) } diff --git a/internal/validation/validation.go b/internal/validation/validation.go index fbf4cbd01..3c802c7df 100644 --- a/internal/validation/validation.go +++ b/internal/validation/validation.go @@ -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 { @@ -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 diff --git a/internal/validation/validation_test.go b/internal/validation/validation_test.go index be513e21d..9fe6bcea4 100644 --- a/internal/validation/validation_test.go +++ b/internal/validation/validation_test.go @@ -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) { diff --git a/server/rpc/connecthelper/status.go b/server/rpc/connecthelper/status.go index bc9ce3ca8..1dbbabd94 100644 --- a/server/rpc/connecthelper/status.go +++ b/server/rpc/connecthelper/status.go @@ -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 } @@ -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 } @@ -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 }