mirror of
https://github.com/miniflux/v2.git
synced 2025-08-01 17:38:37 +00:00
Refactor HTTP Client and LocalizedError packages
This commit is contained in:
parent
120aabfbce
commit
14e25ab9fe
104 changed files with 1277 additions and 10672 deletions
|
@ -4,31 +4,32 @@
|
|||
package validator // import "miniflux.app/v2/internal/validator"
|
||||
|
||||
import (
|
||||
"miniflux.app/v2/internal/locale"
|
||||
"miniflux.app/v2/internal/model"
|
||||
"miniflux.app/v2/internal/storage"
|
||||
)
|
||||
|
||||
// ValidateCategoryCreation validates category creation.
|
||||
func ValidateCategoryCreation(store *storage.Storage, userID int64, request *model.CategoryRequest) *ValidationError {
|
||||
func ValidateCategoryCreation(store *storage.Storage, userID int64, request *model.CategoryRequest) *locale.LocalizedError {
|
||||
if request.Title == "" {
|
||||
return NewValidationError("error.title_required")
|
||||
return locale.NewLocalizedError("error.title_required")
|
||||
}
|
||||
|
||||
if store.CategoryTitleExists(userID, request.Title) {
|
||||
return NewValidationError("error.category_already_exists")
|
||||
return locale.NewLocalizedError("error.category_already_exists")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateCategoryModification validates category modification.
|
||||
func ValidateCategoryModification(store *storage.Storage, userID, categoryID int64, request *model.CategoryRequest) *ValidationError {
|
||||
func ValidateCategoryModification(store *storage.Storage, userID, categoryID int64, request *model.CategoryRequest) *locale.LocalizedError {
|
||||
if request.Title == "" {
|
||||
return NewValidationError("error.title_required")
|
||||
return locale.NewLocalizedError("error.title_required")
|
||||
}
|
||||
|
||||
if store.AnotherCategoryExists(userID, categoryID, request.Title) {
|
||||
return NewValidationError("error.category_already_exists")
|
||||
return locale.NewLocalizedError("error.category_already_exists")
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
@ -4,82 +4,83 @@
|
|||
package validator // import "miniflux.app/v2/internal/validator"
|
||||
|
||||
import (
|
||||
"miniflux.app/v2/internal/locale"
|
||||
"miniflux.app/v2/internal/model"
|
||||
"miniflux.app/v2/internal/storage"
|
||||
)
|
||||
|
||||
// ValidateFeedCreation validates feed creation.
|
||||
func ValidateFeedCreation(store *storage.Storage, userID int64, request *model.FeedCreationRequest) *ValidationError {
|
||||
func ValidateFeedCreation(store *storage.Storage, userID int64, request *model.FeedCreationRequest) *locale.LocalizedError {
|
||||
if request.FeedURL == "" || request.CategoryID <= 0 {
|
||||
return NewValidationError("error.feed_mandatory_fields")
|
||||
return locale.NewLocalizedError("error.feed_mandatory_fields")
|
||||
}
|
||||
|
||||
if !IsValidURL(request.FeedURL) {
|
||||
return NewValidationError("error.invalid_feed_url")
|
||||
return locale.NewLocalizedError("error.invalid_feed_url")
|
||||
}
|
||||
|
||||
if store.FeedURLExists(userID, request.FeedURL) {
|
||||
return NewValidationError("error.feed_already_exists")
|
||||
return locale.NewLocalizedError("error.feed_already_exists")
|
||||
}
|
||||
|
||||
if !store.CategoryIDExists(userID, request.CategoryID) {
|
||||
return NewValidationError("error.feed_category_not_found")
|
||||
return locale.NewLocalizedError("error.feed_category_not_found")
|
||||
}
|
||||
|
||||
if !IsValidRegex(request.BlocklistRules) {
|
||||
return NewValidationError("error.feed_invalid_blocklist_rule")
|
||||
return locale.NewLocalizedError("error.feed_invalid_blocklist_rule")
|
||||
}
|
||||
|
||||
if !IsValidRegex(request.KeeplistRules) {
|
||||
return NewValidationError("error.feed_invalid_keeplist_rule")
|
||||
return locale.NewLocalizedError("error.feed_invalid_keeplist_rule")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateFeedModification validates feed modification.
|
||||
func ValidateFeedModification(store *storage.Storage, userID int64, request *model.FeedModificationRequest) *ValidationError {
|
||||
func ValidateFeedModification(store *storage.Storage, userID int64, request *model.FeedModificationRequest) *locale.LocalizedError {
|
||||
if request.FeedURL != nil {
|
||||
if *request.FeedURL == "" {
|
||||
return NewValidationError("error.feed_url_not_empty")
|
||||
return locale.NewLocalizedError("error.feed_url_not_empty")
|
||||
}
|
||||
|
||||
if !IsValidURL(*request.FeedURL) {
|
||||
return NewValidationError("error.invalid_feed_url")
|
||||
return locale.NewLocalizedError("error.invalid_feed_url")
|
||||
}
|
||||
}
|
||||
|
||||
if request.SiteURL != nil {
|
||||
if *request.SiteURL == "" {
|
||||
return NewValidationError("error.site_url_not_empty")
|
||||
return locale.NewLocalizedError("error.site_url_not_empty")
|
||||
}
|
||||
|
||||
if !IsValidURL(*request.SiteURL) {
|
||||
return NewValidationError("error.invalid_site_url")
|
||||
return locale.NewLocalizedError("error.invalid_site_url")
|
||||
}
|
||||
}
|
||||
|
||||
if request.Title != nil {
|
||||
if *request.Title == "" {
|
||||
return NewValidationError("error.feed_title_not_empty")
|
||||
return locale.NewLocalizedError("error.feed_title_not_empty")
|
||||
}
|
||||
}
|
||||
|
||||
if request.CategoryID != nil {
|
||||
if !store.CategoryIDExists(userID, *request.CategoryID) {
|
||||
return NewValidationError("error.feed_category_not_found")
|
||||
return locale.NewLocalizedError("error.feed_category_not_found")
|
||||
}
|
||||
}
|
||||
|
||||
if request.BlocklistRules != nil {
|
||||
if !IsValidRegex(*request.BlocklistRules) {
|
||||
return NewValidationError("error.feed_invalid_blocklist_rule")
|
||||
return locale.NewLocalizedError("error.feed_invalid_blocklist_rule")
|
||||
}
|
||||
}
|
||||
|
||||
if request.KeeplistRules != nil {
|
||||
if !IsValidRegex(*request.KeeplistRules) {
|
||||
return NewValidationError("error.feed_invalid_keeplist_rule")
|
||||
return locale.NewLocalizedError("error.feed_invalid_keeplist_rule")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -3,12 +3,15 @@
|
|||
|
||||
package validator // import "miniflux.app/v2/internal/validator"
|
||||
|
||||
import "miniflux.app/v2/internal/model"
|
||||
import (
|
||||
"miniflux.app/v2/internal/locale"
|
||||
"miniflux.app/v2/internal/model"
|
||||
)
|
||||
|
||||
// ValidateSubscriptionDiscovery validates subscription discovery requests.
|
||||
func ValidateSubscriptionDiscovery(request *model.SubscriptionDiscoveryRequest) *ValidationError {
|
||||
func ValidateSubscriptionDiscovery(request *model.SubscriptionDiscoveryRequest) *locale.LocalizedError {
|
||||
if !IsValidURL(request.URL) {
|
||||
return NewValidationError("error.invalid_site_url")
|
||||
return locale.NewLocalizedError("error.invalid_site_url")
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
@ -10,13 +10,13 @@ import (
|
|||
)
|
||||
|
||||
// ValidateUserCreationWithPassword validates user creation with a password.
|
||||
func ValidateUserCreationWithPassword(store *storage.Storage, request *model.UserCreationRequest) *ValidationError {
|
||||
func ValidateUserCreationWithPassword(store *storage.Storage, request *model.UserCreationRequest) *locale.LocalizedError {
|
||||
if request.Username == "" {
|
||||
return NewValidationError("error.user_mandatory_fields")
|
||||
return locale.NewLocalizedError("error.user_mandatory_fields")
|
||||
}
|
||||
|
||||
if store.UserExists(request.Username) {
|
||||
return NewValidationError("error.user_already_exists")
|
||||
return locale.NewLocalizedError("error.user_already_exists")
|
||||
}
|
||||
|
||||
if err := validatePassword(request.Password); err != nil {
|
||||
|
@ -27,12 +27,12 @@ func ValidateUserCreationWithPassword(store *storage.Storage, request *model.Use
|
|||
}
|
||||
|
||||
// ValidateUserModification validates user modifications.
|
||||
func ValidateUserModification(store *storage.Storage, userID int64, changes *model.UserModificationRequest) *ValidationError {
|
||||
func ValidateUserModification(store *storage.Storage, userID int64, changes *model.UserModificationRequest) *locale.LocalizedError {
|
||||
if changes.Username != nil {
|
||||
if *changes.Username == "" {
|
||||
return NewValidationError("error.user_mandatory_fields")
|
||||
return locale.NewLocalizedError("error.user_mandatory_fields")
|
||||
} else if store.AnotherUserExists(userID, *changes.Username) {
|
||||
return NewValidationError("error.user_already_exists")
|
||||
return locale.NewLocalizedError("error.user_already_exists")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -105,80 +105,80 @@ func ValidateUserModification(store *storage.Storage, userID int64, changes *mod
|
|||
return nil
|
||||
}
|
||||
|
||||
func validateReadingSpeed(readingSpeed int) *ValidationError {
|
||||
func validateReadingSpeed(readingSpeed int) *locale.LocalizedError {
|
||||
if readingSpeed <= 0 {
|
||||
return NewValidationError("error.settings_reading_speed_is_positive")
|
||||
return locale.NewLocalizedError("error.settings_reading_speed_is_positive")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validatePassword(password string) *ValidationError {
|
||||
func validatePassword(password string) *locale.LocalizedError {
|
||||
if len(password) < 6 {
|
||||
return NewValidationError("error.password_min_length")
|
||||
return locale.NewLocalizedError("error.password_min_length")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateTheme(theme string) *ValidationError {
|
||||
func validateTheme(theme string) *locale.LocalizedError {
|
||||
themes := model.Themes()
|
||||
if _, found := themes[theme]; !found {
|
||||
return NewValidationError("error.invalid_theme")
|
||||
return locale.NewLocalizedError("error.invalid_theme")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateLanguage(language string) *ValidationError {
|
||||
func validateLanguage(language string) *locale.LocalizedError {
|
||||
languages := locale.AvailableLanguages()
|
||||
if _, found := languages[language]; !found {
|
||||
return NewValidationError("error.invalid_language")
|
||||
return locale.NewLocalizedError("error.invalid_language")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateTimezone(store *storage.Storage, timezone string) *ValidationError {
|
||||
func validateTimezone(store *storage.Storage, timezone string) *locale.LocalizedError {
|
||||
timezones, err := store.Timezones()
|
||||
if err != nil {
|
||||
return NewValidationError(err.Error())
|
||||
return locale.NewLocalizedError(err.Error())
|
||||
}
|
||||
|
||||
if _, found := timezones[timezone]; !found {
|
||||
return NewValidationError("error.invalid_timezone")
|
||||
return locale.NewLocalizedError("error.invalid_timezone")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateEntryDirection(direction string) *ValidationError {
|
||||
func validateEntryDirection(direction string) *locale.LocalizedError {
|
||||
if direction != "asc" && direction != "desc" {
|
||||
return NewValidationError("error.invalid_entry_direction")
|
||||
return locale.NewLocalizedError("error.invalid_entry_direction")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateEntriesPerPage(entriesPerPage int) *ValidationError {
|
||||
func validateEntriesPerPage(entriesPerPage int) *locale.LocalizedError {
|
||||
if entriesPerPage < 1 {
|
||||
return NewValidationError("error.entries_per_page_invalid")
|
||||
return locale.NewLocalizedError("error.entries_per_page_invalid")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateDisplayMode(displayMode string) *ValidationError {
|
||||
func validateDisplayMode(displayMode string) *locale.LocalizedError {
|
||||
if displayMode != "fullscreen" && displayMode != "standalone" && displayMode != "minimal-ui" && displayMode != "browser" {
|
||||
return NewValidationError("error.invalid_display_mode")
|
||||
return locale.NewLocalizedError("error.invalid_display_mode")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateGestureNav(gestureNav string) *ValidationError {
|
||||
func validateGestureNav(gestureNav string) *locale.LocalizedError {
|
||||
if gestureNav != "none" && gestureNav != "tap" && gestureNav != "swipe" {
|
||||
return NewValidationError("error.invalid_gesture_nav")
|
||||
return locale.NewLocalizedError("error.invalid_gesture_nav")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateDefaultHomePage(defaultHomePage string) *ValidationError {
|
||||
func validateDefaultHomePage(defaultHomePage string) *locale.LocalizedError {
|
||||
defaultHomePages := model.HomePages()
|
||||
if _, found := defaultHomePages[defaultHomePage]; !found {
|
||||
return NewValidationError("error.invalid_default_home_page")
|
||||
return locale.NewLocalizedError("error.invalid_default_home_page")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -4,32 +4,11 @@
|
|||
package validator // import "miniflux.app/v2/internal/validator"
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"regexp"
|
||||
|
||||
"miniflux.app/v2/internal/locale"
|
||||
)
|
||||
|
||||
// ValidationError represents a validation error.
|
||||
type ValidationError struct {
|
||||
TranslationKey string
|
||||
}
|
||||
|
||||
// NewValidationError initializes a validation error.
|
||||
func NewValidationError(translationKey string) *ValidationError {
|
||||
return &ValidationError{TranslationKey: translationKey}
|
||||
}
|
||||
|
||||
func (v *ValidationError) String() string {
|
||||
return locale.NewPrinter("en_US").Printf(v.TranslationKey)
|
||||
}
|
||||
|
||||
func (v *ValidationError) Error() error {
|
||||
return errors.New(v.String())
|
||||
}
|
||||
|
||||
// ValidateRange makes sure the offset/limit values are valid.
|
||||
func ValidateRange(offset, limit int) error {
|
||||
if offset < 0 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue