mirror of
https://github.com/miniflux/v2.git
synced 2025-08-26 18:21:01 +00:00
Refactor entry validation
This commit is contained in:
parent
806b9545a9
commit
11e110bc7d
14 changed files with 192 additions and 202 deletions
40
validator/entry.go
Normal file
40
validator/entry.go
Normal file
|
@ -0,0 +1,40 @@
|
|||
// 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 (
|
||||
"fmt"
|
||||
|
||||
"miniflux.app/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":
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf(`Invalid entry order, valid order values are: "id", "status", "changed_at", "published_at", "created_at", "category_title", "category_id"`)
|
||||
}
|
60
validator/entry_test.go
Normal file
60
validator/entry_test.go
Normal file
|
@ -0,0 +1,60 @@
|
|||
// 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"
|
||||
|
||||
"miniflux.app/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`)
|
||||
}
|
||||
}
|
|
@ -6,6 +6,7 @@ package validator // import "miniflux.app/validator"
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
|
||||
"miniflux.app/locale"
|
||||
|
@ -29,6 +30,29 @@ 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"`)
|
||||
}
|
||||
|
||||
func isValidURL(absoluteURL string) bool {
|
||||
_, err := url.ParseRequestURI(absoluteURL)
|
||||
return err == nil
|
||||
|
|
|
@ -20,3 +20,29 @@ func TestIsValidURL(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
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`)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue