entry.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package validator // import "miniflux.app/v2/internal/validator"
  4. import (
  5. "fmt"
  6. "miniflux.app/v2/internal/model"
  7. )
  8. // ValidateEntriesStatusUpdateRequest validates a status update for a list of entries.
  9. func ValidateEntriesStatusUpdateRequest(request *model.EntriesStatusUpdateRequest) error {
  10. if len(request.EntryIDs) == 0 {
  11. return fmt.Errorf(`the list of entries cannot be empty`)
  12. }
  13. return ValidateEntryStatus(request.Status)
  14. }
  15. // ValidateEntryStatus makes sure the entry status is valid.
  16. func ValidateEntryStatus(status string) error {
  17. switch status {
  18. case model.EntryStatusRead, model.EntryStatusUnread, model.EntryStatusRemoved:
  19. return nil
  20. }
  21. return fmt.Errorf(`invalid entry status, valid status values are: "%s", "%s" and "%s"`, model.EntryStatusRead, model.EntryStatusUnread, model.EntryStatusRemoved)
  22. }
  23. // ValidateEntryOrder makes sure the sorting order is valid.
  24. func ValidateEntryOrder(order string) error {
  25. switch order {
  26. case "id", "status", "changed_at", "published_at", "created_at", "category_title", "category_id", "title", "author":
  27. return nil
  28. }
  29. return fmt.Errorf(`invalid entry order, valid order values are: "id", "status", "changed_at", "published_at", "created_at", "category_title", "category_id", "title", "author"`)
  30. }
  31. // ValidateEntryModification makes sure the entry modification is valid.
  32. func ValidateEntryModification(request *model.EntryUpdateRequest) error {
  33. if request.Title != nil && *request.Title == "" {
  34. return fmt.Errorf(`the entry title cannot be empty`)
  35. }
  36. if request.Content != nil && *request.Content == "" {
  37. return fmt.Errorf(`the entry content cannot be empty`)
  38. }
  39. return nil
  40. }