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

ActiveStorage attachment validations not working #31656

Closed
swordray opened this Issue Jan 9, 2018 · 10 comments

Comments

Projects
None yet
8 participants
@swordray

swordray commented Jan 9, 2018

Steps to reproduce

class User < ActiveRecord::Base
  has_one_attached :avatar

  validates_each :avatar do |record, attr, value|
    record.errors.add attr unless record.avatar.attachment.blob.content_type.starts_with?('image/')
  end
end

Expected behavior

A non-image avatar should not be saved.

Actual behavior

saved.

System configuration

Rails version: master@428939b

Ruby version: 2.5.0

@georgeclaghorn

This comment has been minimized.

Member

georgeclaghorn commented Jan 9, 2018

Active Storage doesn’t support blob validations yet, so this is the expected behavior.

@rubiii

This comment has been minimized.

Contributor

rubiii commented Feb 8, 2018

Any plans on supporting validation?

@kvoshell

This comment has been minimized.

kvoshell commented Feb 15, 2018

if avatar.attached? && !avatar.attachment.blob.content_type.in?(%w(image/png image/jpeg))
    errors.add(:avatar, 'Must be an image file')
end

This works for me.

@georgeclaghorn

This comment has been minimized.

Member

georgeclaghorn commented Feb 15, 2018

That won’t prevent the blob from being stored.

@tomrossi7

This comment has been minimized.

Contributor

tomrossi7 commented Feb 16, 2018

I've used this code for now. I believe this will remove the attachment, blob, and file:

  validate :logo_validation

  def logo_validation
    if logo.attached?
      if logo.blob.byte_size > 1000000
        logo.purge
        errors[:base] << 'Too big'
      elsif !logo.blob.content_type.starts_with?('image/')
        logo.purge
        errors[:base] << 'Wrong format'
      end
    end
  end

@Dejosel

This comment has been minimized.

Dejosel commented May 24, 2018

@tomrossi7 when i try to use the validation (e.g. a PDF) the whole rails code is broken...

@SampsonCrowley

This comment has been minimized.

SampsonCrowley commented May 24, 2018

how is this closed? this is a MAJOR feature lapse. just because it's expected, doesn't mean it's correct

@SampsonCrowley

This comment has been minimized.

SampsonCrowley commented May 24, 2018

even further, how is ActiveStorage even released if it can't be validated?

@Dejosel

This comment has been minimized.

Dejosel commented May 25, 2018

Answer us please, Any plans on supporting validation?

@ben-rabid

This comment has been minimized.

ben-rabid commented May 27, 2018

+1

@rails rails locked and limited conversation to collaborators May 27, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.