entry.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. // ValidateEntryStatus makes sure the entry status is valid.
  17. func ValidateEntryStatus(status string) error {
  18. switch status {
  19. case model.EntryStatusRead, model.EntryStatusUnread, model.EntryStatusRemoved:
  20. return nil
  21. }
  22. return fmt.Errorf(`invalid entry status, valid status values are: "%s", "%s" and "%s"`, model.EntryStatusRead, model.EntryStatusUnread, model.EntryStatusRemoved)
  23. }
  24. // ValidateEntryOrder makes sure the sorting order is valid.
  25. func ValidateEntryOrder(order string) error {
  26. switch order {
  27. case "id", "status", "changed_at", "published_at", "created_at", "category_title", "category_id", "title", "author":
  28. return nil
  29. }
  30. return errors.New(`invalid entry order, valid order values are: "id", "status", "changed_at", "published_at", "created_at", "category_title", "category_id", "title", "author"`)
  31. }
  32. // ValidateEntryModification makes sure the entry modification is valid.
  33. func ValidateEntryModification(request *model.EntryUpdateRequest) error {
  34. if request.Title != nil && *request.Title == "" {
  35. return errors.New(`the entry title cannot be empty`)
  36. }
  37. if request.Content != nil && *request.Content == "" {
  38. return errors.New(`the entry content cannot be empty`)
  39. }
  40. return nil
  41. }