2023-06-19 17:42:47 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2021-01-04 16:49:28 -05:00
|
|
|
|
2023-08-10 22:46:45 -04:00
|
|
|
package validator // import "miniflux.app/v2/internal/validator"
|
2021-01-04 16:49:28 -05:00
|
|
|
|
|
|
|
import (
|
2023-08-10 22:46:45 -04:00
|
|
|
"miniflux.app/v2/internal/model"
|
|
|
|
"miniflux.app/v2/internal/storage"
|
2021-01-04 16:49:28 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
|
2021-02-07 21:38:45 -05:00
|
|
|
if !IsValidURL(request.FeedURL) {
|
2021-01-04 16:49:28 -05:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2021-02-07 21:38:45 -05:00
|
|
|
if !IsValidRegex(request.BlocklistRules) {
|
|
|
|
return NewValidationError("error.feed_invalid_blocklist_rule")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !IsValidRegex(request.KeeplistRules) {
|
|
|
|
return NewValidationError("error.feed_invalid_keeplist_rule")
|
|
|
|
}
|
|
|
|
|
2021-01-04 16:49:28 -05:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2021-02-07 21:38:45 -05:00
|
|
|
if !IsValidURL(*request.FeedURL) {
|
2021-01-04 16:49:28 -05:00
|
|
|
return NewValidationError("error.invalid_feed_url")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if request.SiteURL != nil {
|
|
|
|
if *request.SiteURL == "" {
|
|
|
|
return NewValidationError("error.site_url_not_empty")
|
|
|
|
}
|
|
|
|
|
2021-02-07 21:38:45 -05:00
|
|
|
if !IsValidURL(*request.SiteURL) {
|
2021-01-04 16:49:28 -05:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-07 21:38:45 -05:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 16:49:28 -05:00
|
|
|
return nil
|
|
|
|
}
|