Переглянути джерело

refactor(validator): reuse ValidateDirection in user modification

Frédéric Guillot 3 місяців тому
батько
коміт
570227c263
2 змінених файлів з 4 додано та 11 видалено
  1. 2 9
      internal/validator/user.go
  2. 2 2
      internal/validator/user_test.go

+ 2 - 9
internal/validator/user.go

@@ -69,8 +69,8 @@ func ValidateUserModification(store *storage.Storage, userID int64, changes *mod
 	}
 
 	if changes.EntryDirection != nil {
-		if err := validateEntryDirection(*changes.EntryDirection); err != nil {
-			return err
+		if err := ValidateDirection(*changes.EntryDirection); err != nil {
+			return locale.NewLocalizedError("error.invalid_entry_direction")
 		}
 	}
 
@@ -211,13 +211,6 @@ func validateTimezone(timezoneValue string) *locale.LocalizedError {
 	return nil
 }
 
-func validateEntryDirection(direction string) *locale.LocalizedError {
-	if direction != "asc" && direction != "desc" {
-		return locale.NewLocalizedError("error.invalid_entry_direction")
-	}
-	return nil
-}
-
 func validateEntriesPerPage(entriesPerPage int) *locale.LocalizedError {
 	if entriesPerPage < 1 {
 		return locale.NewLocalizedError("error.entries_per_page_invalid")

+ 2 - 2
internal/validator/user_test.go

@@ -99,12 +99,12 @@ func TestValidateTimezone(t *testing.T) {
 
 func TestValidateEntryDirection(t *testing.T) {
 	for _, direction := range []string{"asc", "desc"} {
-		if err := validateEntryDirection(direction); err != nil {
+		if err := ValidateDirection(direction); err != nil {
 			t.Errorf("expected valid direction %q to pass, got %v", direction, err)
 		}
 	}
 
-	if err := validateEntryDirection("sideways"); err == nil {
+	if err := ValidateDirection("sideways"); err == nil {
 		t.Error("expected invalid direction to fail")
 	}
 }