Skip to content

Commit

Permalink
Merge pull request #214 from Mth0158/213-incorrect-error-message-when…
Browse files Browse the repository at this point in the history
…-validator-is-used-with-if-or-on

213 incorrect error message when validator is used with if or on
  • Loading branch information
Mth0158 authored Nov 13, 2023
2 parents bd26c11 + 6baac5c commit c855029
Show file tree
Hide file tree
Showing 47 changed files with 837 additions and 133 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ BUNDLE_GEMFILE=gemfiles/rails_next.gemfile bundle exec rake test

Tips:
- To focus a specific test, use the `focus` class method provided by [minitest-focus](https://github.com/minitest/minitest-focus)
- To focus a specific file, use the TEST option provided by minitest, e.g. to only run size_validator_test.rb file you will execute the following command: `bundle exec rake test TEST=test/validators/size_validator_test.rb``
- To focus a specific file, use the TEST option provided by minitest, e.g. to only run size_validator_test.rb file you will execute the following command: `bundle exec rake test TEST=test/validators/size_validator_test.rb`


## Known issues
Expand Down
2 changes: 1 addition & 1 deletion lib/active_storage_validations/limit_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def check_validity!
end

def validate_each(record, attribute, _)
files = Array.wrap(record.send(attribute)).compact.uniq
files = Array.wrap(record.send(attribute)).reject { |file| file.blank? }.compact.uniq
flat_options = unfold_procs(record, self.options, AVAILABLE_CHECKS)
errors_options = initialize_error_options(options)
errors_options[:min] = flat_options[:min]
Expand Down
3 changes: 2 additions & 1 deletion lib/active_storage_validations/size_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def validate_each(record, attribute, _value)
errors_options[:file_size] = number_to_human_size(file.blob.byte_size)
errors_options[:min_size] = number_to_human_size(min_size(flat_options))
errors_options[:max_size] = number_to_human_size(max_size(flat_options))
error_type = "file_size_not_#{flat_options.keys.first}".to_sym
keys = AVAILABLE_CHECKS & flat_options.keys
error_type = "file_size_not_#{keys.first}".to_sym

add_error(record, attribute, error_type, **errors_options)
break
Expand Down
76 changes: 0 additions & 76 deletions test/active_storage_validations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -475,82 +475,6 @@ class ActiveStorageValidations::Test < ActiveSupport::TestCase
end
end

def dummy_file
{ io: File.open(Rails.root.join('public', 'apple-touch-icon.png')), filename: 'dummy_file.png', content_type: 'image/png' }
end

def pdf_file
{ io: File.open(Rails.root.join('public', 'pdf.pdf')), filename: 'pdf_file.pdf', content_type: 'application/pdf' }
end

def bad_dummy_file
{ io: File.open(Rails.root.join('public', 'apple-touch-icon.png')), filename: 'bad_dummy_file.png', content_type: 'text/plain' }
end

def image_150x150_file
{ io: File.open(Rails.root.join('public', 'image_150x150.png')), filename: 'image_150x150_file.png', content_type: 'image/png' }
end

def image_700x500_file
{ io: File.open(Rails.root.join('public', 'image_700x500.png')), filename: 'image_700x500_file.png', content_type: 'image/png' }
end

def image_800x600_file
{ io: File.open(Rails.root.join('public', 'image_800x600.png')), filename: 'image_800x600_file.png', content_type: 'image/png' }
end

def image_600x800_file
{ io: File.open(Rails.root.join('public', 'image_600x800.png')), filename: 'image_600x800_file.png', content_type: 'image/png' }
end

def image_1200x900_file
{ io: File.open(Rails.root.join('public', 'image_1200x900.png')), filename: 'image_1200x900_file.png', content_type: 'image/png' }
end

def image_1300x1000_file
{ io: File.open(Rails.root.join('public', 'image_1300x1000.png')), filename: 'image_1300x1000_file.png', content_type: 'image/png' }
end

def image_1920x1080_file
{ io: File.open(Rails.root.join('public', 'image_1920x1080.png')), filename: 'image_1920x1080_file.png', content_type: 'image/png' }
end

def html_file
{ io: File.open(Rails.root.join('public', '500.html')), filename: 'html_file.html', content_type: 'text/html' }
end

def webp_file
{ io: File.open(Rails.root.join('public', '1_sm_webp.png')), filename: '1_sm_webp.png', content_type: 'image/webp' }
end

def webp_file_wrong
{ io: File.open(Rails.root.join('public', '1_sm_webp.png')), filename: '1_sm_webp.png', content_type: 'image/png' }
end

def docx_file
{ io: File.open(Rails.root.join('public', 'example.docx')), filename: 'example.docx', content_type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' }
end

def sheet_file
{ io: File.open(Rails.root.join('public', 'example.xlsx')), filename: 'example.xlsx', content_type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }
end

def pages_file
{ io: File.open(Rails.root.join('public', 'example.pages')), filename: 'example.pages', content_type: 'application/vnd.apple.pages' }
end

def numbers_file
{ io: File.open(Rails.root.join('public', 'example.numbers')), filename: 'example.numbers', content_type: 'application/vnd.apple.numbers' }
end

def tar_file
{ io: File.open(Rails.root.join('public', '404.html.tar')), filename: '404.html.tar', content_type: 'application/x-tar' }
end

def tar_file_with_image_content_type
{ io: File.open(Rails.root.join('public', '404.html.tar')), filename: '404.png', content_type: 'image/png' }
end

def image_string_io
string_io = StringIO.new().tap {|io| io.binmode }
IO.copy_stream(File.open(Rails.root.join('public', 'image_1920x1080.png')), string_io)
Expand Down
5 changes: 5 additions & 0 deletions test/dummy/app/models/aspect_ratio.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module AspectRatio
def self.table_name_prefix
'aspect_ratio_'
end
end
7 changes: 7 additions & 0 deletions test/dummy/app/models/aspect_ratio/validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module AspectRatio
module Validator
def self.table_name_prefix
'aspect_ratio_validator_'
end
end
end
16 changes: 16 additions & 0 deletions test/dummy/app/models/aspect_ratio/validator/with_if.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: aspect_ratio_validator_with_ifs
#
# title :string
# id :integer not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
#

class AspectRatio::Validator::WithIf < ApplicationRecord
has_one_attached :with_if
validates :with_if, aspect_ratio: :square, if: -> { self.title == 'Right title' }
end
15 changes: 15 additions & 0 deletions test/dummy/app/models/aspect_ratio/validator/with_on.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: aspect_ratio_validator_with_ons
#
# id :integer not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
#

class AspectRatio::Validator::WithOn < ApplicationRecord
has_one_attached :with_on
validates :with_on, aspect_ratio: :square, on: %i(create update destroy custom)
end
7 changes: 7 additions & 0 deletions test/dummy/app/models/attached/validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Attached
module Validator
def self.table_name_prefix
'attached_validator_'
end
end
end
16 changes: 16 additions & 0 deletions test/dummy/app/models/attached/validator/with_if.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: attached_validator_with_ifs
#
# title :string
# id :integer not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
#

class Attached::Validator::WithIf < ApplicationRecord
has_one_attached :with_if
validates :with_if, attached: true, if: -> { self.title == 'Right title' }
end
15 changes: 15 additions & 0 deletions test/dummy/app/models/attached/validator/with_on.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: attached_validator_with_ons
#
# id :integer not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
#

class Attached::Validator::WithOn < ApplicationRecord
has_one_attached :with_on
validates :with_on, attached: true, on: %i(create update destroy custom)
end
7 changes: 7 additions & 0 deletions test/dummy/app/models/content_type/validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module ContentType
module Validator
def self.table_name_prefix
'content_type_validator_'
end
end
end
16 changes: 16 additions & 0 deletions test/dummy/app/models/content_type/validator/with_if.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: content_type_validator_with_ifs
#
# title :string
# id :integer not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
#

class ContentType::Validator::WithIf < ApplicationRecord
has_one_attached :with_if
validates :with_if, content_type: :webp, if: -> { self.title == 'Right title' }
end
15 changes: 15 additions & 0 deletions test/dummy/app/models/content_type/validator/with_on.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: content_type_validator_with_on
#
# id :integer not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
#

class ContentType::Validator::WithOn < ApplicationRecord
has_one_attached :with_on
validates :with_on, content_type: :webp, on: %i(create update destroy custom)
end
7 changes: 7 additions & 0 deletions test/dummy/app/models/dimension/validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Dimension
module Validator
def self.table_name_prefix
'dimension_validator_'
end
end
end
16 changes: 16 additions & 0 deletions test/dummy/app/models/dimension/validator/with_if.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: dimension_validator_with_ifs
#
# title :string
# id :integer not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
#

class Dimension::Validator::WithIf < ApplicationRecord
has_one_attached :with_if
validates :with_if, dimension: { width: 150, height: 150 }, if: -> { self.title == 'Right title' }
end
15 changes: 15 additions & 0 deletions test/dummy/app/models/dimension/validator/with_on.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: dimension_validator_with_ons
#
# id :integer not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
#

class Dimension::Validator::WithOn < ApplicationRecord
has_one_attached :with_on
validates :with_on, dimension: { width: 150, height: 150 }, on: %i(create update destroy custom)
end
5 changes: 5 additions & 0 deletions test/dummy/app/models/limit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Limit
def self.table_name_prefix
'limit_'
end
end
7 changes: 7 additions & 0 deletions test/dummy/app/models/limit/validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Limit
module Validator
def self.table_name_prefix
'limit_validator_'
end
end
end
16 changes: 16 additions & 0 deletions test/dummy/app/models/limit/validator/with_if.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: limit_validator_with_ifs
#
# title :string
# id :integer not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
#

class Limit::Validator::WithIf < ApplicationRecord
has_one_attached :with_if
validates :with_if, limit: { min: 1 }, if: -> { self.title == 'Right title' }
end
15 changes: 15 additions & 0 deletions test/dummy/app/models/limit/validator/with_on.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: limit_validator_with_ons
#
# id :integer not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
#

class Limit::Validator::WithOn < ApplicationRecord
has_one_attached :with_on
validates :with_on, limit: { min: 1 }, on: %i(create update destroy custom)
end
5 changes: 5 additions & 0 deletions test/dummy/app/models/processable_image.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module ProcessableImage
def self.table_name_prefix
'processable_image_'
end
end
7 changes: 7 additions & 0 deletions test/dummy/app/models/processable_image/validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module ProcessableImage
module Validator
def self.table_name_prefix
'processable_image_validator_'
end
end
end
16 changes: 16 additions & 0 deletions test/dummy/app/models/processable_image/validator/with_if.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: processable_image_validator_with_ifs
#
# title :string
# id :integer not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
#

class ProcessableImage::Validator::WithIf < ApplicationRecord
has_one_attached :with_if
validates :with_if, processable_image: true, if: -> { self.title == 'Right title' }
end
15 changes: 15 additions & 0 deletions test/dummy/app/models/processable_image/validator/with_on.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: processable_image_validator_with_ons
#
# id :integer not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
#

class ProcessableImage::Validator::WithOn < ApplicationRecord
has_one_attached :with_on
validates :with_on, processable_image: true, on: %i(create update destroy custom)
end
7 changes: 7 additions & 0 deletions test/dummy/app/models/size/validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Size
module Validator
def self.table_name_prefix
'size_validator_'
end
end
end
16 changes: 16 additions & 0 deletions test/dummy/app/models/size/validator/with_if.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: size_validator_with_ifs
#
# title :string
# id :integer not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
#

class Size::Validator::WithIf < ApplicationRecord
has_one_attached :with_if
validates :with_if, size: { less_than: 2.kilobytes }, if: -> { self.title == 'Right title' }
end
Loading

0 comments on commit c855029

Please sign in to comment.