miniflux/internal/validator/feed.go

93 lines
2.6 KiB
Go
Raw Normal View History

// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
2021-01-04 22:49:28 +01:00
package validator // import "miniflux.app/v2/internal/validator"
2021-01-04 22:49:28 +01:00
import (
"miniflux.app/v2/internal/locale"
"miniflux.app/v2/internal/model"
"miniflux.app/v2/internal/storage"
2021-01-04 22:49:28 +01:00
)
// ValidateFeedCreation validates feed creation.
func ValidateFeedCreation(store *storage.Storage, userID int64, request *model.FeedCreationRequest) *locale.LocalizedError {
2021-01-04 22:49:28 +01:00
if request.FeedURL == "" || request.CategoryID <= 0 {
return locale.NewLocalizedError("error.feed_mandatory_fields")
2021-01-04 22:49:28 +01:00
}
if !IsValidURL(request.FeedURL) {
return locale.NewLocalizedError("error.invalid_feed_url")
2021-01-04 22:49:28 +01:00
}
if store.FeedURLExists(userID, request.FeedURL) {
return locale.NewLocalizedError("error.feed_already_exists")
2021-01-04 22:49:28 +01:00
}
if !store.CategoryIDExists(userID, request.CategoryID) {
return locale.NewLocalizedError("error.feed_category_not_found")
2021-01-04 22:49:28 +01:00
}
if !IsValidRegex(request.BlocklistRules) {
return locale.NewLocalizedError("error.feed_invalid_blocklist_rule")
}
if !IsValidRegex(request.KeeplistRules) {
return locale.NewLocalizedError("error.feed_invalid_keeplist_rule")
}
2021-01-04 22:49:28 +01:00
return nil
}
// ValidateFeedModification validates feed modification.
func ValidateFeedModification(store *storage.Storage, userID, feedID int64, request *model.FeedModificationRequest) *locale.LocalizedError {
2021-01-04 22:49:28 +01:00
if request.FeedURL != nil {
if *request.FeedURL == "" {
return locale.NewLocalizedError("error.feed_url_not_empty")
2021-01-04 22:49:28 +01:00
}
if !IsValidURL(*request.FeedURL) {
return locale.NewLocalizedError("error.invalid_feed_url")
2021-01-04 22:49:28 +01:00
}
if store.AnotherFeedURLExists(userID, feedID, *request.FeedURL) {
return locale.NewLocalizedError("error.feed_already_exists")
}
2021-01-04 22:49:28 +01:00
}
if request.SiteURL != nil {
if *request.SiteURL == "" {
return locale.NewLocalizedError("error.site_url_not_empty")
2021-01-04 22:49:28 +01:00
}
if !IsValidURL(*request.SiteURL) {
return locale.NewLocalizedError("error.invalid_site_url")
2021-01-04 22:49:28 +01:00
}
}
if request.Title != nil {
if *request.Title == "" {
return locale.NewLocalizedError("error.feed_title_not_empty")
2021-01-04 22:49:28 +01:00
}
}
if request.CategoryID != nil {
if !store.CategoryIDExists(userID, *request.CategoryID) {
return locale.NewLocalizedError("error.feed_category_not_found")
2021-01-04 22:49:28 +01:00
}
}
if request.BlocklistRules != nil {
if !IsValidRegex(*request.BlocklistRules) {
return locale.NewLocalizedError("error.feed_invalid_blocklist_rule")
}
}
if request.KeeplistRules != nil {
if !IsValidRegex(*request.KeeplistRules) {
return locale.NewLocalizedError("error.feed_invalid_keeplist_rule")
}
}
2021-01-04 22:49:28 +01:00
return nil
}