验证子域的格式

Validate format of subdomain

如何正确验证子域格式?

这是我得到的:

  validates :subdomain, uniqueness: true, case_sensitive: false
  validates :subdomain, format: { with: /\A[A-Za-z0-9-]+\z/, message: "not a valid subdomain" }
  validates :subdomain, exclusion: { in: %w(support blog billing help api www host admin en ru pl ua us), message: "%{value} is reserved." }
  validates :subdomain, length: { maximum: 20 }
  before_validation :downcase_subdomain
  protected
    def downcase_subdomain
      self.subdomain.downcase! if attribute_present?("subdomain")
    end  

问题:

是否有像电子邮件那样的标准 REGEX 子域验证?子域使用的最佳正则表达式是什么?

validates :email, format: { with: URI::MailTo::EMAIL_REGEXP }, allow_blank: true

RFC 1035 定义子域语法如下:

<subdomain> ::= <label> | <subdomain> "." <label>

<label> ::= <letter> [ [ <ldh-str> ] <let-dig> ]

<ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str>

<let-dig-hyp> ::= <let-dig> | "-"

<let-dig> ::= <letter> | <digit>

<letter> ::= any one of the 52 alphabetic characters A through Z in
upper case and a through z in lower case

<digit> ::= any one of the ten digits 0 through 9

以及仁慈的人类可读描述。

[Labels] must start with a letter, end with a letter or digit, and have as interior characters only letters, digits, and hyphen. There are also some restrictions on the length. Labels must be 63 characters or less.

我们可以使用正则表达式和单独的长度限制来完成大部分工作。

validates :subdomain, format: {
  with: %r{\A[a-z](?:[a-z0-9-]*[a-z0-9])?\z}i, message: "not a valid subdomain"
}, length: { in: 1..63 }

将正则表达式分解成多个部分进行解释。

%r{
  \A
  [a-z]                       # must start with a letter
  (?:
    [a-z0-9-]*                # might contain alpha-numerics or a dash
    [a-z0-9]                  # must end with a letter or digit
  )?                          # that's all optional
 \z
}ix

我们可能会想使用更简单的 /\A[a-z][a-z0-9-]*[a-z0-9]?\z/i 但这允许 foo-.

另见 Regexp for subdomain