Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate_presence_of fails for Shrine UploadedFile since empty string was introduced #1228

Closed
klaustopher opened this issue Jun 25, 2019 · 7 comments · Fixed by #1231
Closed

Comments

@klaustopher
Copy link

klaustopher commented Jun 25, 2019

This somehow relates to #1223 and #1218, but I think it is also different. Please feel free to close and merge with one of the open issues.

Currently our update to 4.1.0 is blocked because on of our test cases is failing with the new version.

The model is super simple:

class ImageUpload < ApplicationRecord
  include ImageUploader::Attachment.new(:image)
  validates_presence_of :image
end

I have played around a bit and the shrine uploader simply ignores assigning "" to the image attribute:

>> up = FactoryBot.build(:image_upload)
# => #<ImageUpload id: nil, image_data: {"id"=>"a4cd635bb1252991f6c539e3d27220cd.jpg", "storage"=>"cache", "metadata"=>{"filename"=>"image_upload_fixture.jpg", "size"=>12183, "mime_type"=>"image/jpeg", "width"=>100, "height"=>200}}, created_at: nil, updated_at: nil>

>> up.image
# => #<ImageUploader::UploadedFile:0x00007fb961af2bf8 @data={"id"=>"a4cd635bb1252991f6c539e3d27220cd.jpg", "storage"=>"cache", "metadata"=>{"filename"=>"image_upload_fixture.jpg", "size"=>12183, "mime_type"=>"image/jpeg", "width"=>100, "height"=>200}}>

>> up.image = ''
# => ""

>> up.image
# => #<ImageUploader::UploadedFile:0x00007fb961afaf88 @data={"id"=>"a4cd635bb1252991f6c539e3d27220cd.jpg", "storage"=>"cache", "metadata"=>{"filename"=>"image_upload_fixture.jpg", "size"=>12183, "mime_type"=>"image/jpeg", "width"=>100, "height"=>200}}>

>> up.image = nil
# => nil

>> up.image
# => nil

Checking all the conditions:

>> ImageUpload.reflect_on_association(:image)
# => nil

>> ImageUpload.attribute_types['image']
# => #<ActiveModel::Type::Value:0x00007fd607362d58 @precision=nil, @scale=nil, @limit=nil>

>> ImageUpload.attribute_types['image'].respond_to?(:coder)
# => false

Shrine seems to use an unspecified ActiveModel::Type class.


For now, we have added a few custom tests instead of validate_presence_of for the field, but maybe it's possible to make the empty string check optional with a modifier?

@mcmire
Copy link
Collaborator

mcmire commented Jun 25, 2019

Thanks for the report! I have a branch in the works that should fix this issue, but I'll leave this open in the meantime.

@vitalinfo
Copy link

the same problem with serializable fields, like
serialize :data, Array

got an exception:

     Failure/Error: it { is_expected.to validate_presence_of :data }
     
     ActiveRecord::SerializationTypeMismatch:
       can't serialize `data`: was supposed to be a Array, but was a String. -- ""
....

@benedikt
Copy link

benedikt commented Jul 4, 2019

I'm running into similar problems with attributes (or rather methods) that get validated but are not handling strings but objects. In theory I could make all setters handle empty strings, but it feels like the wrong path.

@mcmire
Copy link
Collaborator

mcmire commented Jul 7, 2019

@klaustopher @vitalinfo @benedikt Hey, I just pushed up a branch, fix-presence-validation-for-activemodel, that should fix this issue. Can you temporarily switch to it in your Gemfile and see if makes your tests pass?

@benedikt
Copy link

benedikt commented Jul 8, 2019

@mcmire Yes, that works. Thanks a lot for your efforts, Elliot!

@klaustopher
Copy link
Author

Tested revision 11f4483 and it works fine with Shrine.

@mcmire
Copy link
Collaborator

mcmire commented Jul 16, 2019

Fixed in v4.1.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants