Class: ActiveModel::Validations::OnlyPrintableValidator

Inherits:
EachValidator
  • Object
show all
Defined in:
lib/moca_rlibs/active_model_validators/only_printable.rb

Overview

出力可能なアスキー文字のみかどうかをチェックする(半角英数字+一部の記号)

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/moca_rlibs/active_model_validators/only_printable.rb', line 9

def validate_each(record, attribute, value)
  return if value.blank?

  value = value.presence.to_s
  return if value.match?(%r{\A[a-zA-Z0-9!"#$%&'()*+,-./:;<=>?@\[\\\]^_`{|}~]*\z})

  record.errors[attribute] << 'contain invalid characters (only printable ascii characters)'
end