Use the blob validator for ActiveStorage attachments
masterTo validate has_one_attached or has_many_attached attachments, use the blob validator within your Rails model.
Note: For has_many_attached, the size_range validation is applied to each file individually.
class User < ApplicationRecord
has_one_attached :avatar
has_many_attached :photos
# Allow only web image files (PNG, JPEG, GIF, WebP)
validates :avatar, presence: true, blob: { content_type: :web_image }
# Allow only JPEG/PNG, up to 5MB per file
validates :photos, presence: true, blob: { content_type: ["image/png", "image/jpg", "image/jpeg"], size_range: 1..(5.megabytes) }
# Require specific file extensions
validates :audio, blob: { content_type: "audio/mpeg", extension: %w[mp3] }
endclass User < ApplicationRecord
has_one_attached :avatar
has_many_attached :photos
# Allow only web image files
validates :avatar, presence: true, blob: { content_type: :web_image }
# Allow only JPEG/PNG, up to 5MB per file
validates :photos, presence: true, blob: { content_type: ["image/png", "image/jpg", "image/jpeg"], size_range: 1..(5.megabytes) }
end