Class: FullMetalBody::Internal::InputFileValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- FullMetalBody::Internal::InputFileValidator
- Defined in:
- lib/full_metal_body/internal/input_file_validator.rb
Constant Summary collapse
- DEFAULT_CONTENT_TYPES =
'*'.freeze
- DEFAULT_FILE_SIZE =
100.megabytes
Instance Method Summary collapse
Instance Method Details
#check_validity! ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/full_metal_body/internal/input_file_validator.rb', line 9 def check_validity! if [:content_type] value = [:content_type] unless value.is_a?(String) || (value.is_a?(Array) && value.all?(String)) raise ArgumentError, ":content_type must be String or Array<String>" end end if [:file_size] value = [:file_size] raise ArgumentError, ":file_size must be a non-negative Integer" unless value.is_a?(Integer) && value >= 0 end end |
#validate_each(record, attribute, value) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/full_metal_body/internal/input_file_validator.rb', line 22 def validate_each(record, attribute, value) return if value.nil? content_types = [:content_type] || DEFAULT_CONTENT_TYPES file_size = [:file_size] || DEFAULT_FILE_SIZE # type unless value.is_a? ActionDispatch::Http::UploadedFile return record.errors.add(attribute, :file_wrong_type, value: value) end # content type if content_types != '*' && Array(content_types).exclude?(value.content_type) return record.errors.add(attribute, :invalid_content_type) end # file size if value.size > file_size record.errors.add(attribute, :too_large_size) end end |