Class: FilenameValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
lib/autoload/filename_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate(record) ⇒ Object


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/autoload/filename_validator.rb', line 3

def validate(record)
  errors = record.errors[:name]
  # \ : * ? " < > | are not allowed
  if record.name =~ /[\/\\:\*\?"<>\|]/
    errors <<
      I18n.t('activerecord.errors.messages.filename_validator.invalid_characters',
             characters: '\ : * ? " < > |')
  # Tailing dots are not allowed
  elsif record.name =~ /\.+\z/
    errors << I18n.t('activerecord.errors.messages.filename_validator.tailing_dots')
  # Leading or tailing whitespaces are not allowed
  elsif record.name =~ /(\A\s+.*)|(.*\s+\z)/
    errors << I18n.t('activerecord.errors.messages.filename_validator.whitespaces')
  end
end