feed.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. if !IsValidRegex(request.BlocklistRules) {
  24. return NewValidationError("error.feed_invalid_blocklist_rule")
  25. }
  26. if !IsValidRegex(request.KeeplistRules) {
  27. return NewValidationError("error.feed_invalid_keeplist_rule")
  28. }
  29. return nil
  30. }
  31. // ValidateFeedModification validates feed modification.
  32. func ValidateFeedModification(store *storage.Storage, userID int64, request *model.FeedModificationRequest) *ValidationError {
  33. if request.FeedURL != nil {
  34. if *request.FeedURL == "" {
  35. return NewValidationError("error.feed_url_not_empty")
  36. }
  37. if !IsValidURL(*request.FeedURL) {
  38. return NewValidationError("error.invalid_feed_url")
  39. }
  40. }
  41. if request.SiteURL != nil {
  42. if *request.SiteURL == "" {
  43. return NewValidationError("error.site_url_not_empty")
  44. }
  45. if !IsValidURL(*request.SiteURL) {
  46. return NewValidationError("error.invalid_site_url")
  47. }
  48. }
  49. if request.Title != nil {
  50. if *request.Title == "" {
  51. return NewValidationError("error.feed_title_not_empty")
  52. }
  53. }
  54. if request.CategoryID != nil {
  55. if !store.CategoryIDExists(userID, *request.CategoryID) {
  56. return NewValidationError("error.feed_category_not_found")
  57. }
  58. }
  59. if request.BlocklistRules != nil {
  60. if !IsValidRegex(*request.BlocklistRules) {
  61. return NewValidationError("error.feed_invalid_blocklist_rule")
  62. }
  63. }
  64. if request.KeeplistRules != nil {
  65. if !IsValidRegex(*request.KeeplistRules) {
  66. return NewValidationError("error.feed_invalid_keeplist_rule")
  67. }
  68. }
  69. return nil
  70. }