class Kirico::CharsetValidator
Constants
- CHECKS
- KANJI_RULE
- KATAKANA_RULE
- LATIN_RULE
- NUMERIC_RULE
Public Instance Methods
check_validity!()
click to toggle source
# File lib/kirico/validators/charset_validator.rb, line 57 def check_validity! keys = CHECKS.keys & ([options[:accept]].flatten || []) raise ArgumentError, ':accept unspecified. Specify the :numeric, :latin, :katakana, :kanji, or :all option.' if keys.empty? end
retrieve_error_chars(value, error_chars = [])
click to toggle source
# File lib/kirico/validators/charset_validator.rb, line 68 def retrieve_error_chars(value, error_chars = []) # value.blank? はタブ文字等の場合でも true となるため注意 return error_chars if value.nil? || value.empty? # e-Gov では U+FF0D (FULLWIDTH HYPHEN-MINUS) を許容しているようだ # -> Shift_JIS ではなく CP932 が適当 match = regex(options[:accept]).match(value.encode('CP932')) return error_chars if match.nil? ch = match.captures[0].encode('UTF-8') error_chars << ch retrieve_error_chars(value.gsub(/#{Regexp.escape(ch)}/, ''), error_chars) rescue Encoding::UndefinedConversionError => e ch = e.error_char error_chars << ch retrieve_error_chars(value.gsub(/#{Regexp.escape(ch)}/, ''), error_chars) end
validate_each(record, attribute, value)
click to toggle source
# File lib/kirico/validators/charset_validator.rb, line 62 def validate_each(record, attribute, value) error_chars = retrieve_error_chars(value) record.errors.add(attribute, (options[:message] || :invalid_charset), error_chars: error_chars.join(', ')) unless error_chars.empty? end
Private Instance Methods
regex(accept = [])
click to toggle source
# File lib/kirico/validators/charset_validator.rb, line 88 def regex(accept = []) joined_rules = CHECKS.select { |k, _v| accept.include?(k) }.values.join str = "[^#{joined_rules}]".encode('CP932') /(#{str})/ end