Class: Validation::Rule::DiasporaId

Inherits:
Object
  • Object
show all
Defined in:
lib/diaspora_federation/validators/rules/diaspora_id.rb

Overview

diaspora* ID validation rule

A simple rule to validate the base structure of diaspora* IDs.

Constant Summary collapse

DIASPORA_ID_MAX_LENGTH =

Maximum length of a full diaspora* ID

255
DIASPORA_ID_REGEX =

The Regex for a valid diaspora* ID

begin
  username       = "[[:lower:]\\d\\-\\.\\_]+"
  hostname_part  = "[[:lower:]\\d\\-]"
  hostname       = "#{hostname_part}+(?:[.]#{hostname_part}*)*"
  ipv4           = "(?:[\\d]{1,3}\\.){3}[\\d]{1,3}"
  ipv6           = "\\[(?:[[:xdigit:]]{0,4}:){0,7}[[:xdigit:]]{1,4}\\]"
  ip_addr        = "(?:#{ipv4}|#{ipv6})"
  domain         = "(?:#{hostname}|#{ip_addr})"
  port           = "(?::[\\d]+)?"

  "#{username}\\@#{domain}#{port}"
end
DIASPORA_ID =

The Regex for validating a full diaspora* ID

/\A#{DIASPORA_ID_REGEX}\z/u

Instance Method Summary collapse

Instance Method Details

#error_keySymbol

The error key for this rule


29
30
31
# File 'lib/diaspora_federation/validators/rules/diaspora_id.rb', line 29

def error_key
  :diaspora_id
end

#paramsHash

This rule has no params.


43
44
45
# File 'lib/diaspora_federation/validators/rules/diaspora_id.rb', line 43

def params
  {}
end

#valid_value?(value) ⇒ Boolean

Determines if value is a valid diaspora* ID


34
35
36
37
38
39
# File 'lib/diaspora_federation/validators/rules/diaspora_id.rb', line 34

def valid_value?(value)
  return false unless value.is_a?(String)
  return false if value.length > DIASPORA_ID_MAX_LENGTH

  value =~ DIASPORA_ID
end