mirror of
https://github.com/miniflux/v2.git
synced 2025-08-01 17:38:37 +00:00
Move internal packages to an internal folder
For reference: https://go.dev/doc/go1.4#internalpackages
This commit is contained in:
parent
c234903255
commit
168a870c02
433 changed files with 1121 additions and 1123 deletions
35
internal/validator/category.go
Normal file
35
internal/validator/category.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package validator // import "miniflux.app/v2/internal/validator"
|
||||
|
||||
import (
|
||||
"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 {
|
||||
if request.Title == "" {
|
||||
return NewValidationError("error.title_required")
|
||||
}
|
||||
|
||||
if store.CategoryTitleExists(userID, request.Title) {
|
||||
return NewValidationError("error.category_already_exists")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateCategoryModification validates category modification.
|
||||
func ValidateCategoryModification(store *storage.Storage, userID, categoryID int64, request *model.CategoryRequest) *ValidationError {
|
||||
if request.Title == "" {
|
||||
return NewValidationError("error.title_required")
|
||||
}
|
||||
|
||||
if store.AnotherCategoryExists(userID, categoryID, request.Title) {
|
||||
return NewValidationError("error.category_already_exists")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
39
internal/validator/entry.go
Normal file
39
internal/validator/entry.go
Normal file
|
@ -0,0 +1,39 @@
|
|||
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package validator // import "miniflux.app/v2/internal/validator"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"miniflux.app/v2/internal/model"
|
||||
)
|
||||
|
||||
// ValidateEntriesStatusUpdateRequest validates a status update for a list of entries.
|
||||
func ValidateEntriesStatusUpdateRequest(request *model.EntriesStatusUpdateRequest) error {
|
||||
if len(request.EntryIDs) == 0 {
|
||||
return fmt.Errorf(`The list of entries cannot be empty`)
|
||||
}
|
||||
|
||||
return ValidateEntryStatus(request.Status)
|
||||
}
|
||||
|
||||
// ValidateEntryStatus makes sure the entry status is valid.
|
||||
func ValidateEntryStatus(status string) error {
|
||||
switch status {
|
||||
case model.EntryStatusRead, model.EntryStatusUnread, model.EntryStatusRemoved:
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf(`Invalid entry status, valid status values are: "%s", "%s" and "%s"`, model.EntryStatusRead, model.EntryStatusUnread, model.EntryStatusRemoved)
|
||||
}
|
||||
|
||||
// ValidateEntryOrder makes sure the sorting order is valid.
|
||||
func ValidateEntryOrder(order string) error {
|
||||
switch order {
|
||||
case "id", "status", "changed_at", "published_at", "created_at", "category_title", "category_id", "title", "author":
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf(`Invalid entry order, valid order values are: "id", "status", "changed_at", "published_at", "created_at", "category_title", "category_id", "title", "author"`)
|
||||
}
|
59
internal/validator/entry_test.go
Normal file
59
internal/validator/entry_test.go
Normal file
|
@ -0,0 +1,59 @@
|
|||
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package validator // import "miniflux.app/v2/internal/validator"
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"miniflux.app/v2/internal/model"
|
||||
)
|
||||
|
||||
func TestValidateEntriesStatusUpdateRequest(t *testing.T) {
|
||||
err := ValidateEntriesStatusUpdateRequest(&model.EntriesStatusUpdateRequest{
|
||||
Status: model.EntryStatusRead,
|
||||
EntryIDs: []int64{int64(123), int64(456)},
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(`A valid request should not be rejected`)
|
||||
}
|
||||
|
||||
err = ValidateEntriesStatusUpdateRequest(&model.EntriesStatusUpdateRequest{
|
||||
Status: model.EntryStatusRead,
|
||||
})
|
||||
if err == nil {
|
||||
t.Error(`An empty list of entries is not valid`)
|
||||
}
|
||||
|
||||
err = ValidateEntriesStatusUpdateRequest(&model.EntriesStatusUpdateRequest{
|
||||
Status: "invalid",
|
||||
EntryIDs: []int64{int64(123)},
|
||||
})
|
||||
if err == nil {
|
||||
t.Error(`Only a valid status should be accepted`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateEntryStatus(t *testing.T) {
|
||||
for _, status := range []string{model.EntryStatusRead, model.EntryStatusUnread, model.EntryStatusRemoved} {
|
||||
if err := ValidateEntryStatus(status); err != nil {
|
||||
t.Error(`A valid status should not generate any error`)
|
||||
}
|
||||
}
|
||||
|
||||
if err := ValidateEntryStatus("invalid"); err == nil {
|
||||
t.Error(`An invalid status should generate a error`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateEntryOrder(t *testing.T) {
|
||||
for _, status := range []string{"id", "status", "changed_at", "published_at", "created_at", "category_title", "category_id"} {
|
||||
if err := ValidateEntryOrder(status); err != nil {
|
||||
t.Error(`A valid order should not generate any error`)
|
||||
}
|
||||
}
|
||||
|
||||
if err := ValidateEntryOrder("invalid"); err == nil {
|
||||
t.Error(`An invalid order should generate a error`)
|
||||
}
|
||||
}
|
87
internal/validator/feed.go
Normal file
87
internal/validator/feed.go
Normal file
|
@ -0,0 +1,87 @@
|
|||
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package validator // import "miniflux.app/v2/internal/validator"
|
||||
|
||||
import (
|
||||
"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 {
|
||||
if request.FeedURL == "" || request.CategoryID <= 0 {
|
||||
return NewValidationError("error.feed_mandatory_fields")
|
||||
}
|
||||
|
||||
if !IsValidURL(request.FeedURL) {
|
||||
return NewValidationError("error.invalid_feed_url")
|
||||
}
|
||||
|
||||
if store.FeedURLExists(userID, request.FeedURL) {
|
||||
return NewValidationError("error.feed_already_exists")
|
||||
}
|
||||
|
||||
if !store.CategoryIDExists(userID, request.CategoryID) {
|
||||
return NewValidationError("error.feed_category_not_found")
|
||||
}
|
||||
|
||||
if !IsValidRegex(request.BlocklistRules) {
|
||||
return NewValidationError("error.feed_invalid_blocklist_rule")
|
||||
}
|
||||
|
||||
if !IsValidRegex(request.KeeplistRules) {
|
||||
return NewValidationError("error.feed_invalid_keeplist_rule")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateFeedModification validates feed modification.
|
||||
func ValidateFeedModification(store *storage.Storage, userID int64, request *model.FeedModificationRequest) *ValidationError {
|
||||
if request.FeedURL != nil {
|
||||
if *request.FeedURL == "" {
|
||||
return NewValidationError("error.feed_url_not_empty")
|
||||
}
|
||||
|
||||
if !IsValidURL(*request.FeedURL) {
|
||||
return NewValidationError("error.invalid_feed_url")
|
||||
}
|
||||
}
|
||||
|
||||
if request.SiteURL != nil {
|
||||
if *request.SiteURL == "" {
|
||||
return NewValidationError("error.site_url_not_empty")
|
||||
}
|
||||
|
||||
if !IsValidURL(*request.SiteURL) {
|
||||
return NewValidationError("error.invalid_site_url")
|
||||
}
|
||||
}
|
||||
|
||||
if request.Title != nil {
|
||||
if *request.Title == "" {
|
||||
return NewValidationError("error.feed_title_not_empty")
|
||||
}
|
||||
}
|
||||
|
||||
if request.CategoryID != nil {
|
||||
if !store.CategoryIDExists(userID, *request.CategoryID) {
|
||||
return NewValidationError("error.feed_category_not_found")
|
||||
}
|
||||
}
|
||||
|
||||
if request.BlocklistRules != nil {
|
||||
if !IsValidRegex(*request.BlocklistRules) {
|
||||
return NewValidationError("error.feed_invalid_blocklist_rule")
|
||||
}
|
||||
}
|
||||
|
||||
if request.KeeplistRules != nil {
|
||||
if !IsValidRegex(*request.KeeplistRules) {
|
||||
return NewValidationError("error.feed_invalid_keeplist_rule")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
15
internal/validator/subscription.go
Normal file
15
internal/validator/subscription.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package validator // import "miniflux.app/v2/internal/validator"
|
||||
|
||||
import "miniflux.app/v2/internal/model"
|
||||
|
||||
// ValidateSubscriptionDiscovery validates subscription discovery requests.
|
||||
func ValidateSubscriptionDiscovery(request *model.SubscriptionDiscoveryRequest) *ValidationError {
|
||||
if !IsValidURL(request.URL) {
|
||||
return NewValidationError("error.invalid_site_url")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
184
internal/validator/user.go
Normal file
184
internal/validator/user.go
Normal file
|
@ -0,0 +1,184 @@
|
|||
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package validator // import "miniflux.app/v2/internal/validator"
|
||||
|
||||
import (
|
||||
"miniflux.app/v2/internal/locale"
|
||||
"miniflux.app/v2/internal/model"
|
||||
"miniflux.app/v2/internal/storage"
|
||||
)
|
||||
|
||||
// ValidateUserCreationWithPassword validates user creation with a password.
|
||||
func ValidateUserCreationWithPassword(store *storage.Storage, request *model.UserCreationRequest) *ValidationError {
|
||||
if request.Username == "" {
|
||||
return NewValidationError("error.user_mandatory_fields")
|
||||
}
|
||||
|
||||
if store.UserExists(request.Username) {
|
||||
return NewValidationError("error.user_already_exists")
|
||||
}
|
||||
|
||||
if err := validatePassword(request.Password); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateUserModification validates user modifications.
|
||||
func ValidateUserModification(store *storage.Storage, userID int64, changes *model.UserModificationRequest) *ValidationError {
|
||||
if changes.Username != nil {
|
||||
if *changes.Username == "" {
|
||||
return NewValidationError("error.user_mandatory_fields")
|
||||
} else if store.AnotherUserExists(userID, *changes.Username) {
|
||||
return NewValidationError("error.user_already_exists")
|
||||
}
|
||||
}
|
||||
|
||||
if changes.Password != nil {
|
||||
if err := validatePassword(*changes.Password); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if changes.Theme != nil {
|
||||
if err := validateTheme(*changes.Theme); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if changes.Language != nil {
|
||||
if err := validateLanguage(*changes.Language); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if changes.Timezone != nil {
|
||||
if err := validateTimezone(store, *changes.Timezone); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if changes.EntryDirection != nil {
|
||||
if err := validateEntryDirection(*changes.EntryDirection); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if changes.EntriesPerPage != nil {
|
||||
if err := validateEntriesPerPage(*changes.EntriesPerPage); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if changes.DisplayMode != nil {
|
||||
if err := validateDisplayMode(*changes.DisplayMode); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if changes.GestureNav != nil {
|
||||
if err := validateGestureNav(*changes.GestureNav); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if changes.DefaultReadingSpeed != nil {
|
||||
if err := validateReadingSpeed(*changes.DefaultReadingSpeed); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if changes.CJKReadingSpeed != nil {
|
||||
if err := validateReadingSpeed(*changes.CJKReadingSpeed); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if changes.DefaultHomePage != nil {
|
||||
if err := validateDefaultHomePage(*changes.DefaultHomePage); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateReadingSpeed(readingSpeed int) *ValidationError {
|
||||
if readingSpeed <= 0 {
|
||||
return NewValidationError("error.settings_reading_speed_is_positive")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validatePassword(password string) *ValidationError {
|
||||
if len(password) < 6 {
|
||||
return NewValidationError("error.password_min_length")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateTheme(theme string) *ValidationError {
|
||||
themes := model.Themes()
|
||||
if _, found := themes[theme]; !found {
|
||||
return NewValidationError("error.invalid_theme")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateLanguage(language string) *ValidationError {
|
||||
languages := locale.AvailableLanguages()
|
||||
if _, found := languages[language]; !found {
|
||||
return NewValidationError("error.invalid_language")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateTimezone(store *storage.Storage, timezone string) *ValidationError {
|
||||
timezones, err := store.Timezones()
|
||||
if err != nil {
|
||||
return NewValidationError(err.Error())
|
||||
}
|
||||
|
||||
if _, found := timezones[timezone]; !found {
|
||||
return NewValidationError("error.invalid_timezone")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateEntryDirection(direction string) *ValidationError {
|
||||
if direction != "asc" && direction != "desc" {
|
||||
return NewValidationError("error.invalid_entry_direction")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateEntriesPerPage(entriesPerPage int) *ValidationError {
|
||||
if entriesPerPage < 1 {
|
||||
return NewValidationError("error.entries_per_page_invalid")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateDisplayMode(displayMode string) *ValidationError {
|
||||
if displayMode != "fullscreen" && displayMode != "standalone" && displayMode != "minimal-ui" && displayMode != "browser" {
|
||||
return NewValidationError("error.invalid_display_mode")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateGestureNav(gestureNav string) *ValidationError {
|
||||
if gestureNav != "none" && gestureNav != "tap" && gestureNav != "swipe" {
|
||||
return NewValidationError("error.invalid_gesture_nav")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateDefaultHomePage(defaultHomePage string) *ValidationError {
|
||||
defaultHomePages := model.HomePages()
|
||||
if _, found := defaultHomePages[defaultHomePage]; !found {
|
||||
return NewValidationError("error.invalid_default_home_page")
|
||||
}
|
||||
return nil
|
||||
}
|
66
internal/validator/validator.go
Normal file
66
internal/validator/validator.go
Normal file
|
@ -0,0 +1,66 @@
|
|||
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
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 {
|
||||
return fmt.Errorf(`Offset value should be >= 0`)
|
||||
}
|
||||
|
||||
if limit < 0 {
|
||||
return fmt.Errorf(`Limit value should be >= 0`)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateDirection makes sure the sorting direction is valid.
|
||||
func ValidateDirection(direction string) error {
|
||||
switch direction {
|
||||
case "asc", "desc":
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf(`Invalid direction, valid direction values are: "asc" or "desc"`)
|
||||
}
|
||||
|
||||
// IsValidRegex verifies if the regex can be compiled.
|
||||
func IsValidRegex(expr string) bool {
|
||||
_, err := regexp.Compile(expr)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// IsValidURL verifies if the provided value is a valid absolute URL.
|
||||
func IsValidURL(absoluteURL string) bool {
|
||||
_, err := url.ParseRequestURI(absoluteURL)
|
||||
return err == nil
|
||||
}
|
61
internal/validator/validator_test.go
Normal file
61
internal/validator/validator_test.go
Normal file
|
@ -0,0 +1,61 @@
|
|||
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package validator // import "miniflux.app/v2/internal/validator"
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestIsValidURL(t *testing.T) {
|
||||
scenarios := map[string]bool{
|
||||
"https://www.example.org": true,
|
||||
"http://www.example.org/": true,
|
||||
"www.example.org": false,
|
||||
}
|
||||
|
||||
for link, expected := range scenarios {
|
||||
result := IsValidURL(link)
|
||||
if result != expected {
|
||||
t.Errorf(`Unexpected result, got %v instead of %v`, result, expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateRange(t *testing.T) {
|
||||
if err := ValidateRange(-1, 0); err == nil {
|
||||
t.Error(`An invalid offset should generate a error`)
|
||||
}
|
||||
|
||||
if err := ValidateRange(0, -1); err == nil {
|
||||
t.Error(`An invalid limit should generate a error`)
|
||||
}
|
||||
|
||||
if err := ValidateRange(42, 42); err != nil {
|
||||
t.Error(`A valid offset and limit should not generate any error`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateDirection(t *testing.T) {
|
||||
for _, status := range []string{"asc", "desc"} {
|
||||
if err := ValidateDirection(status); err != nil {
|
||||
t.Error(`A valid direction should not generate any error`)
|
||||
}
|
||||
}
|
||||
|
||||
if err := ValidateDirection("invalid"); err == nil {
|
||||
t.Error(`An invalid direction should generate a error`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsValidRegex(t *testing.T) {
|
||||
scenarios := map[string]bool{
|
||||
"(?i)miniflux": true,
|
||||
"[": false,
|
||||
}
|
||||
|
||||
for expr, expected := range scenarios {
|
||||
result := IsValidRegex(expr)
|
||||
if result != expected {
|
||||
t.Errorf(`Unexpected result, got %v instead of %v`, result, expected)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue