1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-08-01 17:38:37 +00:00

Refactor entry validation

This commit is contained in:
Frédéric Guillot 2021-01-04 15:32:32 -08:00 committed by fguillot
parent 806b9545a9
commit 11e110bc7d
14 changed files with 192 additions and 202 deletions

View file

@ -5,11 +5,10 @@
package model // import "miniflux.app/model"
import (
"fmt"
"time"
)
// Entry statuses
// Entry statuses and default sorting order.
const (
EntryStatusUnread = "unread"
EntryStatusRead = "read"
@ -35,52 +34,15 @@ type Entry struct {
ShareCode string `json:"share_code"`
Starred bool `json:"starred"`
ReadingTime int `json:"reading_time"`
Enclosures EnclosureList `json:"enclosures,omitempty"`
Enclosures EnclosureList `json:"enclosures"`
Feed *Feed `json:"feed,omitempty"`
}
// Entries represents a list of entries.
type Entries []*Entry
// ValidateEntryStatus makes sure the entry status is valid.
func ValidateEntryStatus(status string) error {
switch status {
case EntryStatusRead, EntryStatusUnread, EntryStatusRemoved:
return nil
}
return fmt.Errorf(`Invalid entry status, valid status values are: "%s", "%s" and "%s"`, EntryStatusRead, EntryStatusUnread, 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"`)
}
// 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"`)
}
// 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
// EntriesStatusUpdateRequest represents a request to change entries status.
type EntriesStatusUpdateRequest struct {
EntryIDs []int64 `json:"entry_ids"`
Status string `json:"status"`
}

View file

@ -1,57 +0,0 @@
// Copyright 2017 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 model // import "miniflux.app/model"
import "testing"
func TestValidateEntryStatus(t *testing.T) {
for _, status := range []string{EntryStatusRead, EntryStatusUnread, 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`)
}
}
func TestValidateEntryDirection(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 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`)
}
}