No commit activity in last 3 years
No release in over 3 years
CarrierWave extension to validate files uploaded using carrierwave.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.5
~> 10.2.2
~> 2.14.1
~> 1.3.9

Runtime

< 0.11.0, >= 0.8.0
>= 3.2.0
 Project Readme

CarrierWave-Validators

Build Status

CarrierWave extension to validate uploaded files.

Installation

Install the latest stable release:

$[sudo] gem install carrierwave-validators

In Rails, add it to your Gemfile:

gem 'carrierwave-validators'

And then execute:

$ bundle

Finally, restart the server to apply the changes.

Getting Started

Carrierwave-Validators supports following validators to validate your attachment:

  • SizeValidator - Validates the file size of uploaded file

SizeValidator

SizeValidator validates the file size of files uploaded using CarrierWave.

Example Usage, in the model User with Carrierwave attachment avatar:

class User < ActiveRecord::Base
  mount_uploader :avatar, AvatarUploader
  validates :avatar, :size => { :in => 0..50.kilobytes } ## Validates if uploaded file is within 0 Bytes to 50 KiloBytes
end

Alternatively,

 validates :avatar, :size => { :in => 0..50.kilobytes }

can be specified as

 validates_file_size :avatar, :in => 0..50.kilobytes  

Available options:

  • in : in between a Range of bytes , for example

      ## uploaded file size is within 0 Bytes to 50 KiloBytes
      validates :avatar, :size => { :in => 0..50.kilobytes } 
    
  • less_than : less than a number in bytes, for example

      ## uploaded file size is less than 5 MegaBytes
      validates :avatar, :size => { :less_than => 5.megabytes } 
    
  • greater_than : greater than a number in bytes, for example

      ## uploaded file size is greater than 5 KiloBytes
      validates :avatar, :size => { :greater_than => 5.kilobytes } 
    
  • less_than_or_equal_to : less than or equal to a number in bytes, for example

      ## uploaded file size is less than or equal to 1 MegaByte
      validates :avatar, :size => { :less_than_or_equal_to => 1.megabytes } 
    
  • greater_than_or_equal_to : greater than or equal to a number in bytes, for example

      ## uploaded file size is greater than or equal to 5 KiloBytes
      validates :avatar, :size => { :greater_than_or_equal_to => 5.kilobytes } 
    
  • message : error message to display, for example

       validates :avatar, :size => { :in => 0..50.kilobytes, :message => "Your custom error message" }   
    

#i18n

The Active Record validations use the Rails i18n framework. Include the following keys in your translation files:

en:
  errors:
    messages:
      in: "file size must be in between %{min} %{min_unit} and %{max} %{max_unit}"
      greater_than: "file size must be greater than %{count} %{unit}"
      greater_than_or_equal_to: "file size must be greater than or equal to %{count} %{unit}"
      less_than: "file size must be less than %{count} %{unit}"
      less_than_or_equal_to: "file size must be less than or equal to %{count} %{unit}"

  size:
    units:
      one: "Byte"
      other: "Bytes"

TODO

  • Add content-type validation