entry.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. "errors"
  6. "fmt"
  7. "miniflux.app/v2/internal/model"
  8. )
  9. // ValidateEntriesStatusUpdateRequest validates a status update for a list of entries.
  10. func ValidateEntriesStatusUpdateRequest(request *model.EntriesStatusUpdateRequest) error {
  11. if len(request.EntryIDs) == 0 {
  12. return errors.New(`the list of entries cannot be empty`)
  13. }
  14. return ValidateEntryStatus(request.Status)
  15. }
  16. // ValidateEntriesStatusAndStarredUpdateRequest validates a status and/or starred update
  17. // for a list of entries. At least one of the status or starred fields must be specified.
  18. // This is used by the API, which can update the read status, the starred state, or both.
  19. func ValidateEntriesStatusAndStarredUpdateRequest(request *model.EntriesStatusUpdateRequest) error {
  20. if len(request.EntryIDs) == 0 {
  21. return errors.New(`the list of entries cannot be empty`)
  22. }
  23. if request.Status == "" && request.Starred == nil {
  24. return errors.New(`either the status or the starred field must be specified`)
  25. }
  26. if request.Status != "" {
  27. return ValidateEntryStatus(request.Status)
  28. }
  29. return nil
  30. }
  31. // ValidateEntryStatus makes sure the entry status is valid.
  32. func ValidateEntryStatus(status string) error {
  33. switch status {
  34. case model.EntryStatusRead, model.EntryStatusUnread:
  35. return nil
  36. }
  37. return fmt.Errorf(`invalid entry status, valid status values are: %q and %q`, model.EntryStatusRead, model.EntryStatusUnread)
  38. }
  39. // ValidateEntryOrder makes sure the sorting order is valid.
  40. func ValidateEntryOrder(order string) error {
  41. switch order {
  42. case "id", "status", "changed_at", "published_at", "created_at", "category_title", "category_id", "title", "author":
  43. return nil
  44. }
  45. return errors.New(`invalid entry order, valid order values are: "id", "status", "changed_at", "published_at", "created_at", "category_title", "category_id", "title", "author"`)
  46. }
  47. // ValidateEntryModification makes sure the entry modification is valid.
  48. func ValidateEntryModification(request *model.EntryUpdateRequest) error {
  49. if request.Title != nil && *request.Title == "" {
  50. return errors.New(`the entry title cannot be empty`)
  51. }
  52. if request.Content != nil && *request.Content == "" {
  53. return errors.New(`the entry content cannot be empty`)
  54. }
  55. return nil
  56. }