Class: TelegramValidator

Inherits:
AccountNameValidator show all
Defined in:
lib/telegram_validator.rb

Overview

Validates Telegram usernames. Telegram usernames are 5-32 characters of alphanumerics and underscores; they must start with a letter and cannot end with an underscore. Username lookups are case-insensitive. A leading @ is stripped before validation.

The following error message keys are used to localize invalid usernames:

| | | |:-------------------------------|:------------------------------------------------------| | telegram_too_short | Username is less than 5 characters. | | telegram_too_long | Username is over 32 characters. | | telegram_invalid_chars | Username contains characters outside [A-Za-z0-9_]. | | telegram_invalid_first_char | Username doesn't start with a letter. | | telegram_trailing_underscore | Username ends with an underscore. |

Options

| | | |:-------------|:-------------------------------------------------| | :message | A custom message to use if the username is invalid. | | :allow_nil | If true, nil values are allowed. |

Examples:

validates :telegram, telegram: true

Instance Method Summary collapse

Methods inherited from AccountNameValidator

add_validation, error_key_prefix, first_char, max_length, min_length, valid_chars

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object

Overrides AccountNameValidator#validate_each to strip an optional leading @ before applying validations.



38
39
40
41
42
43
# File 'lib/telegram_validator.rb', line 38

def validate_each(record, attribute, value)
  if value.is_a?(String) && value.start_with?("@")
    value = value[1..]
  end
  super
end