feed.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright 2021 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package validator // import "miniflux.app/validator"
  5. import (
  6. "miniflux.app/model"
  7. "miniflux.app/storage"
  8. )
  9. // ValidateFeedCreation validates feed creation.
  10. func ValidateFeedCreation(store *storage.Storage, userID int64, request *model.FeedCreationRequest) *ValidationError {
  11. if request.FeedURL == "" || request.CategoryID <= 0 {
  12. return NewValidationError("error.feed_mandatory_fields")
  13. }
  14. if !isValidURL(request.FeedURL) {
  15. return NewValidationError("error.invalid_feed_url")
  16. }
  17. if store.FeedURLExists(userID, request.FeedURL) {
  18. return NewValidationError("error.feed_already_exists")
  19. }
  20. if !store.CategoryIDExists(userID, request.CategoryID) {
  21. return NewValidationError("error.feed_category_not_found")
  22. }
  23. return nil
  24. }
  25. // ValidateFeedModification validates feed modification.
  26. func ValidateFeedModification(store *storage.Storage, userID int64, request *model.FeedModificationRequest) *ValidationError {
  27. if request.FeedURL != nil {
  28. if *request.FeedURL == "" {
  29. return NewValidationError("error.feed_url_not_empty")
  30. }
  31. if !isValidURL(*request.FeedURL) {
  32. return NewValidationError("error.invalid_feed_url")
  33. }
  34. }
  35. if request.SiteURL != nil {
  36. if *request.SiteURL == "" {
  37. return NewValidationError("error.site_url_not_empty")
  38. }
  39. if !isValidURL(*request.SiteURL) {
  40. return NewValidationError("error.invalid_site_url")
  41. }
  42. }
  43. if request.Title != nil {
  44. if *request.Title == "" {
  45. return NewValidationError("error.feed_title_not_empty")
  46. }
  47. }
  48. if request.CategoryID != nil {
  49. if !store.CategoryIDExists(userID, *request.CategoryID) {
  50. return NewValidationError("error.feed_category_not_found")
  51. }
  52. }
  53. return nil
  54. }