ActiveStorage Validations: content_type, byte_size, and presence#1
Draft
seanabrahams wants to merge 5 commits intomainfrom
Draft
ActiveStorage Validations: content_type, byte_size, and presence#1seanabrahams wants to merge 5 commits intomainfrom
seanabrahams wants to merge 5 commits intomainfrom
Conversation
Co-authored-by: Abhishek Chandrasekhar <me@abhchand.me>
…odel instance and attribute name.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Building directly on top of rails#41178 (which built on top of rails#35390) this PR attempts to pick up where rails#41178 left off in implementing validations for ActiveStorage attributes with support for direct uploads as well.
There are two major differences:
User.avatar). This way we know to apply the ActiveStorage validations defined for theUsermodel'savatarattribute. Further, rather than pass this information in public, leaving it open to tampering, it is signed:I'm not entirely happy with the method name,
to_signed_validation_id, and welcome suggestions. Perhapsdirect_upload_validation_id? I went with theto_*_idformat to match GlobalID'sto_global_idmethod but it feels odd in this context.@user.avatar.direct_upload_validation_id?This is what a direct upload input element ends up looking like:
{ "errors": { "content_type": [ "is not included in the list" ], "byte_size": [ "must be less than or equal to 5 MB" ] } }One question:
There was a previous note in active_storage/validations/attachment_content_type_validator.rb:
It wasn't clear to me how best to share the code from
ActiveModel::Validators::FormatValidatorthus I opted to copy, paste, and refine it for this use case. Would be grateful for a seasoned Rails contributor to chime in here.Thank you Abhishek Chandrasekhar and Alex Ghiculescu for your original work!
Co-authored-by: Abhishek Chandrasekhar me@abhchand.me
Co-authored-by: Alex Ghiculescu alex@tanda.co