Refactor feed validator
This commit is contained in:
parent
b35fece3d5
commit
806b9545a9
32 changed files with 588 additions and 521 deletions
68
validator/feed.go
Normal file
68
validator/feed.go
Normal file
|
@ -0,0 +1,68 @@
|
|||
// Copyright 2021 Frédéric Guillot. All rights reserved.
|
||||
// Use of this source code is governed by the Apache 2.0
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package validator // import "miniflux.app/validator"
|
||||
|
||||
import (
|
||||
"miniflux.app/model"
|
||||
"miniflux.app/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")
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
16
validator/subscription.go
Normal file
16
validator/subscription.go
Normal file
|
@ -0,0 +1,16 @@
|
|||
// Copyright 2021 Frédéric Guillot. All rights reserved.
|
||||
// Use of this source code is governed by the Apache 2.0
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package validator // import "miniflux.app/validator"
|
||||
|
||||
import "miniflux.app/model"
|
||||
|
||||
// ValidateSubscriptionDiscovery validates subscription discovery requests.
|
||||
func ValidateSubscriptionDiscovery(request *model.SubscriptionDiscoveryRequest) *ValidationError {
|
||||
if !isValidURL(request.URL) {
|
||||
return NewValidationError("error.invalid_site_url")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -6,6 +6,7 @@ package validator // import "miniflux.app/validator"
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"net/url"
|
||||
|
||||
"miniflux.app/locale"
|
||||
)
|
||||
|
@ -27,3 +28,8 @@ func (v *ValidationError) String() string {
|
|||
func (v *ValidationError) Error() error {
|
||||
return errors.New(v.String())
|
||||
}
|
||||
|
||||
func isValidURL(absoluteURL string) bool {
|
||||
_, err := url.ParseRequestURI(absoluteURL)
|
||||
return err == nil
|
||||
}
|
||||
|
|
22
validator/validator_test.go
Normal file
22
validator/validator_test.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
// Copyright 2021 Frédéric Guillot. All rights reserved.
|
||||
// Use of this source code is governed by the Apache 2.0
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package validator // import "miniflux.app/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)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue