A Rails gem that adds model validators for Active Storage attachments, covering presence, counts, MIME types, sizes, dimensions, duration, and related checks.
Overview
Active Storage Validations extends Rails models so uploaded files attached through Active Storage can be checked with familiar validates syntax. You can require attachments, cap how many files are allowed, restrict content types, and enforce per-file or combined size limits.
Optional validators lean on external analyzers for richer metadata. Image rules can use libvips or ImageMagick; video and audio checks expect ffmpeg; PDF page counts use poppler. Content-type spoofing protection can call the file or Magika CLI. Analyzed metadata is cached on the blob so repeat validations avoid redundant work.
The gem wires I18n error messages into Rails, can infer HTML accept attributes on file fields from your validators, and emits ActiveSupport::Notifications when analyzers run or time out. Validators accept static values or procs for dynamic limits at validation time.
It targets Rails 7.0.1 onward and Ruby 3.3 onward, installed as a standard Bundler dependency.
Key features
- Attached presence validation
- Upload count limits
- Content type restrictions
- Per-file and total size caps
- Image and video dimensions
- Duration and audio track checks
- PDF page count validation
- Content-type spoofing protection
Use cases
- Validating user avatars
- Enforcing photo album limits
- Blocking disallowed upload types
- Guarding video upload requirements
- PDF attachment rules