Class: ActiveModel::Validations::FileContentTypeValidator
- Inherits:
-
EachValidator
- Object
- EachValidator
- ActiveModel::Validations::FileContentTypeValidator
- Defined in:
- lib/file_validators/validators/file_content_type_validator.rb
Constant Summary collapse
- CHECKS =
i[allow exclude].freeze
- SUPPORTED_MODES =
{ relaxed: :mime_types, strict: :file }.freeze
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.helper_method_name ⇒ Object
9 10 11 |
# File 'lib/file_validators/validators/file_content_type_validator.rb', line 9 def self.helper_method_name :validates_file_content_type end |
Instance Method Details
#check_validity! ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/file_validators/validators/file_content_type_validator.rb', line 36 def check_validity! unless (CHECKS & .keys).present? raise ArgumentError, 'You must at least pass in :allow or :exclude option' end .slice(*CHECKS).each do |option, value| unless value.is_a?(String) || value.is_a?(Array) || value.is_a?(Regexp) || value.is_a?(Proc) raise ArgumentError, ":#{option} must be a string, an array, a regex or a proc" end end end |
#validate_each(record, attribute, value) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/file_validators/validators/file_content_type_validator.rb', line 13 def validate_each(record, attribute, value) begin values = parse_values(value) rescue JSON::ParserError record.errors.add attribute, :invalid return end return if values.empty? mode = option_value(record, :mode) tool = option_value(record, :tool) || SUPPORTED_MODES[mode] allowed_types = option_content_types(record, :allow) forbidden_types = option_content_types(record, :exclude) values.each do |val| content_type = get_content_type(val, tool) validate_whitelist(record, attribute, content_type, allowed_types) validate_blacklist(record, attribute, content_type, forbidden_types) end end |