Skip to content

Commit

Permalink
Merge pull request #862 from PRX/fix/description_bytesize
Browse files Browse the repository at this point in the history
Description bytesize
  • Loading branch information
cavis authored Oct 3, 2023
2 parents af7bb23 + c790a80 commit aa112a0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/models/episode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Episode < ApplicationRecord

validates :podcast_id, :guid, presence: true
validates :title, presence: true
validates :description, bytesize: {maximum: 4000}, if: :strict_validations
validates :url, http_url: true
validates :original_guid, presence: true, uniqueness: {scope: :podcast_id}, allow_nil: true
alias_error_messages :item_guid, :original_guid
Expand Down
7 changes: 7 additions & 0 deletions app/models/validators/bytesize_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class BytesizeValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
if value.present? && value.bytesize > options[:maximum]
record.errors.add(attribute, :too_long, count: options[:maximum])
end
end
end
19 changes: 19 additions & 0 deletions test/models/episode_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@
assert minimal_episode.updated_at > 10.minutes.ago
end

it "validates descriptions have a maximum of 4000 bytes" do
e = build_stubbed(:episode, segment_count: 2, published_at: nil, strict_validations: true)

e.description = nil
assert e.valid?

e.description = "a" * 4000
assert e.valid?

e.description = "a" * 4001
refute e.valid?

e.description = "a" * 3999 + "’"
refute e.valid?

e.strict_validations = false
assert e.valid?
end

it "validates unique original guids" do
e1 = create(:episode, original_guid: "original")
e2 = build(:episode, original_guid: "original", podcast: e1.podcast)
Expand Down

0 comments on commit aa112a0

Please sign in to comment.