[ActiveStorage] Feature Request: attachment validations

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

[ActiveStorage] Feature Request: attachment validations

Matt Yanchek
Hey all, with ActiveStorage coming out very soon I am very quickly finding a need for some validations for attachments before storing. Some very quick validation helpers would be great for example content type and size would be huge wins in protecting an application that is expecting to only allow images.

Just a quick example:

class User < ApplicationRecord
  has_one_attached
:avatar
 
  validates
:avatar, attachment: { content_types: 'image/*', size: 50 }
end

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [ActiveStorage] Feature Request: attachment validations

Luis Felipe Sanchez
I think this would be a great feature to have. Looking around the code I find that one main issue is that doing attribute assignment like `@record.file=attachable`
uploads the attachable to the service right away. We could make it so that `ActiveStorage::Attached` stores the attachable as an instance variable and a new method called `attach!` actually uploads the file.
Then one could validate that `file.content_type` is valid.
On Monday, April 2, 2018 at 8:36:22 AM UTC-5, Matt Yanchek wrote:
Hey all, with ActiveStorage coming out very soon I am very quickly finding a need for some validations for attachments before storing. Some very quick validation helpers would be great for example content type and size would be huge wins in protecting an application that is expecting to only allow images.

Just a quick example:

class User < ApplicationRecord
  has_one_attached
:avatar
 
  validates
:avatar, attachment: { content_types: 'image/*', size: 50 }
end

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [ActiveStorage] Feature Request: attachment validations

ifomichev
In reply to this post by Matt Yanchek
Hi all,

I think this is one of the essential features that are missing in Active Storage. Thus I'm pretty sure it's gonna be implemented pretty soon one way or another, and I wonder what is the maintainers' plan for it, if there is any.

I know about active_storage_validations gem, but its functionality is quite limited and the gem itself is pretty self-inconsistent and raw (though it's the best publicly available gem I could find, kudos to the maintainers!)

One approach I'm thinking of would be to adapt paperclip's validators for Active Storage (thanks to MIT license), and I think I could do it, but I'm not sure if it's gonna be accepted. One doesn't have to invent the wheel, but I'd like to hear an expert opinion.

Thank you,

Ivan

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [ActiveStorage] Feature Request: attachment validations

Rob Zolkos-2

On Mon, Nov 26, 2018 at 3:36 PM <[hidden email]> wrote:
Hi all,

I think this is one of the essential features that are missing in Active Storage. Thus I'm pretty sure it's gonna be implemented pretty soon one way or another, and I wonder what is the maintainers' plan for it, if there is any.

I know about active_storage_validations gem, but its functionality is quite limited and the gem itself is pretty self-inconsistent and raw (though it's the best publicly available gem I could find, kudos to the maintainers!)

One approach I'm thinking of would be to adapt paperclip's validators for Active Storage (thanks to MIT license), and I think I could do it, but I'm not sure if it's gonna be accepted. One doesn't have to invent the wheel, but I'd like to hear an expert opinion.

Thank you,

Ivan

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [ActiveStorage] Feature Request: attachment validations

ifomichev
Hmmm...

Hi Rob,

thanks for your reply!

I see how one can validate presence of a blob from this change, but I'm not sure what could be the syntax for validating content type, filename, or file size after this change. Could you please elaborate on that? The commit you referred to provides neither documentation nor tests for these cases.

Best regards,
Ivan

On Monday, November 26, 2018 at 9:53:12 PM UTC+1, Rob Zolkos wrote:
Rails 6 will have validations for AS  <a href="https://github.com/rails/rails/commit/e8682c5bf051517b0b265e446aa1a7eccfd47bf7#diff-c76fb6202b7f95a08fe12f40c4999ac9R11" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Frails%2Frails%2Fcommit%2Fe8682c5bf051517b0b265e446aa1a7eccfd47bf7%23diff-c76fb6202b7f95a08fe12f40c4999ac9R11\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFVP55zgiYWKBzR9RFEDMQpi_HlSQ&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Frails%2Frails%2Fcommit%2Fe8682c5bf051517b0b265e446aa1a7eccfd47bf7%23diff-c76fb6202b7f95a08fe12f40c4999ac9R11\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFVP55zgiYWKBzR9RFEDMQpi_HlSQ&#39;;return true;">https://github.com/rails/rails/commit/e8682c5bf051517b0b265e446aa1a7eccfd47bf7#diff-c76fb6202b7f95a08fe12f40c4999ac9R11

On Mon, Nov 26, 2018 at 3:36 PM <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="67mpOiLlCQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">ifom...@...> wrote:
Hi all,

I think this is one of the essential features that are missing in Active Storage. Thus I'm pretty sure it's gonna be implemented pretty soon one way or another, and I wonder what is the maintainers' plan for it, if there is any.

I know about active_storage_validations gem, but its functionality is quite limited and the gem itself is pretty self-inconsistent and raw (though it's the best publicly available gem I could find, kudos to the maintainers!)

One approach I'm thinking of would be to adapt paperclip's validators for Active Storage (thanks to MIT license), and I think I could do it, but I'm not sure if it's gonna be accepted. One doesn't have to invent the wheel, but I'd like to hear an expert opinion.

Thank you,

Ivan

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="67mpOiLlCQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonrails-co...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="67mpOiLlCQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
Visit this group at <a href="https://groups.google.com/group/rubyonrails-core" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/group/rubyonrails-core&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/rubyonrails-core&#39;;return true;">https://groups.google.com/group/rubyonrails-core.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [ActiveStorage] Feature Request: attachment validations

George Claghorn-2
Validations are planned for Rails 6. Here’s a rough sketch of the API I have in mind:

    validates_attached :logo, presence: true, byte_size: { less_than: 10.megabytes, message: "must be smaller than 10 MB" }, content_type: /\Aimage\//

I intended to implement this myself, and laid the groundwork for it in the commit Rob mentioned, but Igor Kasyanchuk asked if he could fold active_storage_validations into Active Storage proper: https://github.com/rails/rails/issues/33741. Since September, I’ve been giving him time to open a PR.

Please feel free to investigate yourself. Rails 6 is slated for early next year, so if nobody else opens a PR before then, I’ll come back to validations after the holidays.

On Mon, Nov 26, 2018 at 4:13 PM <[hidden email]> wrote:
Hmmm...

Hi Rob,

thanks for your reply!

I see how one can validate presence of a blob from this change, but I'm not sure what could be the syntax for validating content type, filename, or file size after this change. Could you please elaborate on that? The commit you referred to provides neither documentation nor tests for these cases.

Best regards,
Ivan

On Monday, November 26, 2018 at 9:53:12 PM UTC+1, Rob Zolkos wrote:

On Mon, Nov 26, 2018 at 3:36 PM <[hidden email]> wrote:
Hi all,

I think this is one of the essential features that are missing in Active Storage. Thus I'm pretty sure it's gonna be implemented pretty soon one way or another, and I wonder what is the maintainers' plan for it, if there is any.

I know about active_storage_validations gem, but its functionality is quite limited and the gem itself is pretty self-inconsistent and raw (though it's the best publicly available gem I could find, kudos to the maintainers!)

One approach I'm thinking of would be to adapt paperclip's validators for Active Storage (thanks to MIT license), and I think I could do it, but I'm not sure if it's gonna be accepted. One doesn't have to invent the wheel, but I'd like to hear an expert opinion.

Thank you,

Ivan

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [ActiveStorage] Feature Request: attachment validations

ifomichev
Hi George,

thanks for your positive feedback! I'll see how far I can get :)

Best regards,
Ivan

On Tuesday, November 27, 2018 at 4:21:28 PM UTC+1, George Claghorn wrote:
Validations are planned for Rails 6. Here’s a rough sketch of the API I have in mind:

    validates_attached :logo, presence: true, byte_size: { less_than: 10.megabytes, message: "must be smaller than 10 MB" }, content_type: /\Aimage\//

I intended to implement this myself, and laid the groundwork for it in the commit Rob mentioned, but Igor Kasyanchuk asked if he could fold active_storage_validations into Active Storage proper: <a href="https://github.com/rails/rails/issues/33741" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Frails%2Frails%2Fissues%2F33741\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGarnkgTrq4dMjv6j9wQ6mopq3hxg&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Frails%2Frails%2Fissues%2F33741\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGarnkgTrq4dMjv6j9wQ6mopq3hxg&#39;;return true;">https://github.com/rails/rails/issues/33741. Since September, I’ve been giving him time to open a PR.

Please feel free to investigate yourself. Rails 6 is slated for early next year, so if nobody else opens a PR before then, I’ll come back to validations after the holidays.

On Mon, Nov 26, 2018 at 4:13 PM <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="n89juZwhCgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">ifom...@...> wrote:
Hmmm...

Hi Rob,

thanks for your reply!

I see how one can validate presence of a blob from this change, but I'm not sure what could be the syntax for validating content type, filename, or file size after this change. Could you please elaborate on that? The commit you referred to provides neither documentation nor tests for these cases.

Best regards,
Ivan

On Monday, November 26, 2018 at 9:53:12 PM UTC+1, Rob Zolkos wrote:
Rails 6 will have validations for AS  <a href="https://github.com/rails/rails/commit/e8682c5bf051517b0b265e446aa1a7eccfd47bf7#diff-c76fb6202b7f95a08fe12f40c4999ac9R11" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Frails%2Frails%2Fcommit%2Fe8682c5bf051517b0b265e446aa1a7eccfd47bf7%23diff-c76fb6202b7f95a08fe12f40c4999ac9R11\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFVP55zgiYWKBzR9RFEDMQpi_HlSQ&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Frails%2Frails%2Fcommit%2Fe8682c5bf051517b0b265e446aa1a7eccfd47bf7%23diff-c76fb6202b7f95a08fe12f40c4999ac9R11\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFVP55zgiYWKBzR9RFEDMQpi_HlSQ&#39;;return true;">https://github.com/rails/rails/commit/e8682c5bf051517b0b265e446aa1a7eccfd47bf7#diff-c76fb6202b7f95a08fe12f40c4999ac9R11

On Mon, Nov 26, 2018 at 3:36 PM <[hidden email]> wrote:
Hi all,

I think this is one of the essential features that are missing in Active Storage. Thus I'm pretty sure it's gonna be implemented pretty soon one way or another, and I wonder what is the maintainers' plan for it, if there is any.

I know about active_storage_validations gem, but its functionality is quite limited and the gem itself is pretty self-inconsistent and raw (though it's the best publicly available gem I could find, kudos to the maintainers!)

One approach I'm thinking of would be to adapt paperclip's validators for Active Storage (thanks to MIT license), and I think I could do it, but I'm not sure if it's gonna be accepted. One doesn't have to invent the wheel, but I'd like to hear an expert opinion.

Thank you,

Ivan

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-co...@googlegroups.com.
To post to this group, send email to [hidden email].
Visit this group at <a href="https://groups.google.com/group/rubyonrails-core" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/group/rubyonrails-core&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/rubyonrails-core&#39;;return true;">https://groups.google.com/group/rubyonrails-core.
For more options, visit <a href="https://groups.google.com/d/optout" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="n89juZwhCgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonrails-co...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="n89juZwhCgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
Visit this group at <a href="https://groups.google.com/group/rubyonrails-core" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/group/rubyonrails-core&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/rubyonrails-core&#39;;return true;">https://groups.google.com/group/rubyonrails-core.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.