file_validators 2.0.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b2a185e059287eb886b2f0aca6f8654d0860f97d
4
- data.tar.gz: 087b21a3812ec49de9dd0df345521792631b2925
3
+ metadata.gz: 862ed2a30f76ed277f22f80773319c3627f991dd
4
+ data.tar.gz: c434e6754743f0f6cd819992a0d4475bc5fc8176
5
5
  SHA512:
6
- metadata.gz: ba536373220e03c3e4da79095baad18015dfec6f152cca7fca98f8ba0b1c9b64488b6f447497153e959d3eb1980f07015b32f2c1717638fda8002bb99232d1c7
7
- data.tar.gz: cf6d97528247cae02d8c42824c42938e37e57b2ff73045cd429ecb601c23784ce058afe3ef8969994ef83f7366482132818b724b2c00c0a28390d52650a5fb4a
6
+ metadata.gz: 33426eed1c228b06716a841826221a4f6cb87ed508cc1c58c745ff432c5a18915d8230e5d227146d6a4642488baf3f9e5d4c326b947de7bbb5e6cf8b5d221efc
7
+ data.tar.gz: b0b2f36ce62fc8dd9f93fff6ac65a5a6440499893c3ac09e8a8f053914f89d9180e6a9999c570d49382265688f15ffe7bce10d92809bd012e78573119760afa5
data/.travis.yml CHANGED
@@ -1,7 +1,7 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.2.2
4
3
  - 1.9.3
4
+ - 2.2.2
5
5
  gemfile:
6
6
  - gemfiles/activemodel_4.2.gemfile
7
7
  - gemfiles/activemodel_4.1.gemfile
data/README.md CHANGED
@@ -142,7 +142,6 @@ of the files. It also detects media type spoofing (see more in [security](#secur
142
142
  `:relaxed` mode uses file name to detect the content type using `mime-types` gem.
143
143
  If mode option is not set then the validator uses form supplied content type.
144
144
  ```ruby
145
- # string
146
145
  validates :avatar, file_content_type: { allow: 'image/jpeg', mode: :strict }
147
146
  validates :avatar, file_content_type: { allow: 'image/jpeg', mode: :relaxed }
148
147
  ```
@@ -12,6 +12,7 @@ module ActiveModel
12
12
  end
13
13
 
14
14
  def validate_each(record, attribute, value)
15
+ value = JSON.parse(value) if value.is_a?(String)
15
16
  unless value.blank?
16
17
  mode = option_value(record, :mode)
17
18
  content_type = get_content_type(value, mode)
@@ -63,7 +64,6 @@ module ActiveModel
63
64
  file_name = get_file_name(value)
64
65
  MIME::Types.type_for(file_name).first
65
66
  else
66
- value = JSON.parse(value) if value.is_a?(String)
67
67
  value = OpenStruct.new(value) if value.is_a?(Hash)
68
68
  value.content_type
69
69
  end
@@ -13,9 +13,9 @@ module ActiveModel
13
13
  end
14
14
 
15
15
  def validate_each(record, attribute, value)
16
+ value = JSON.parse(value) if value.is_a?(String)
16
17
  unless value.blank?
17
18
  options.slice(*CHECKS.keys).each do |option, option_value|
18
- value = JSON.parse(value) if value.is_a?(String)
19
19
  value = OpenStruct.new(value) if value.is_a?(Hash)
20
20
  option_value = option_value.call(record) if option_value.is_a?(Proc)
21
21
  unless valid_size?(value.size, option, option_value)
@@ -1,3 +1,3 @@
1
1
  module FileValidators
2
- VERSION = '2.0.0'
2
+ VERSION = '2.0.1'
3
3
  end
@@ -342,6 +342,11 @@ describe 'File Content Type integration with ActiveModel' do
342
342
  before { subject.avatar = "{\"filename\":\"img140910_88338.jpg\",\"content_type\":\"image/jpeg\",\"size\":13150}" }
343
343
  it { is_expected.to be_valid }
344
344
  end
345
+
346
+ context 'empty json string' do
347
+ before { subject.avatar = "{}" }
348
+ it { is_expected.to be_valid }
349
+ end
345
350
  end
346
351
 
347
352
  context 'image data as hash' do
@@ -355,12 +360,17 @@ describe 'File Content Type integration with ActiveModel' do
355
360
  subject { Person.new }
356
361
 
357
362
  context 'for invalid content type' do
358
- before { subject.avatar = { "filename":"img140910_88338.GIF", "content_type":"image/gif", "size":13150 } }
363
+ before { subject.avatar = { "filename" => "img140910_88338.GIF", "content_type" => "image/gif", "size" => 13150 } }
359
364
  it { is_expected.not_to be_valid }
360
365
  end
361
366
 
362
367
  context 'for valid content type' do
363
- before { subject.avatar = { "filename":"img140910_88338.jpg", "content_type":"image/jpeg", "size":13150 } }
368
+ before { subject.avatar = { "filename" => "img140910_88338.jpg", "content_type" => "image/jpeg", "size" => 13150 } }
369
+ it { is_expected.to be_valid }
370
+ end
371
+
372
+ context 'empty hash' do
373
+ before { subject.avatar = {} }
364
374
  it { is_expected.to be_valid }
365
375
  end
366
376
  end
@@ -226,6 +226,11 @@ describe 'File Size Validator integration with ActiveModel' do
226
226
  before { subject.avatar = "{\"filename\":\"img140910_88338.GIF\",\"content_type\":\"image/gif\",\"size\":33150}" }
227
227
  it { is_expected.to be_valid }
228
228
  end
229
+
230
+ context 'empty json string' do
231
+ before { subject.avatar = "{}" }
232
+ it { is_expected.to be_valid }
233
+ end
229
234
  end
230
235
 
231
236
  context 'image data as hash' do
@@ -239,12 +244,17 @@ describe 'File Size Validator integration with ActiveModel' do
239
244
  subject { Person.new }
240
245
 
241
246
  context 'when file size is less than the specified size' do
242
- before { subject.avatar = { "filename":"img140910_88338.GIF", "content_type":"image/gif", "size":13150 } }
247
+ before { subject.avatar = { "filename" => "img140910_88338.GIF", "content_type" => "image/gif", "size" => 13150 } }
243
248
  it { is_expected.not_to be_valid }
244
249
  end
245
250
 
246
251
  context 'when file size within the specified size' do
247
- before { subject.avatar = { "filename":"img140910_88338.GIF", "content_type":"image/gif", "size":33150 } }
252
+ before { subject.avatar = { "filename" => "img140910_88338.GIF", "content_type" => "image/gif", "size" => 33150 } }
253
+ it { is_expected.to be_valid }
254
+ end
255
+
256
+ context 'empty hash' do
257
+ before { subject.avatar = {} }
248
258
  it { is_expected.to be_valid }
249
259
  end
250
260
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: file_validators
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ahmad Musaffa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-31 00:00:00.000000000 Z
11
+ date: 2015-08-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel