Class: Respect::FormatValidator

Inherits:
Validator show all
Defined in:
lib/respect/format_validator.rb

Constant Summary

PHONE_NUMBER_REGEXP =
/^((\+|00)\d{1,2})?\d+$/
HOSTNAME_REGEXP =

FIXME(Nicolas Despres): RFC 1034 mentions that a valid domain can be " " in section 3.5. (see www.rfc-editor.org/rfc/rfc1034.txt) but we don't since I don't understand when it is useful.

/^[a-z][a-z0-9-]*(\.[a-z][a-z0-9-]*)*$/i

Instance Method Summary (collapse)

Methods inherited from Validator

constraint_name, #to_h

Constructor Details

- (FormatValidator) initialize(format)

A new instance of FormatValidator



14
15
16
# File 'lib/respect/format_validator.rb', line 14

def initialize(format)
  @format = format
end

Instance Method Details

- (Object) validate(value)



18
19
20
# File 'lib/respect/format_validator.rb', line 18

def validate(value)
  send("validate_#@format", value)
end

- (Object) validate_datetime(value)

Validate date and time string format following RFC 3399 (see tools.ietf.org/html/rfc3339)



49
50
51
52
53
# File 'lib/respect/format_validator.rb', line 49

def validate_datetime(value)
  DateTime.rfc3339(value)
rescue ArgumentError => e
  raise ValidationError, e.message
end

- (Object) validate_email(value)

Validate the given string value describes a well-formed email address following this specification www.w3.org/TR/2012/CR-html5-20121217/forms.html#valid-e-mail-address



25
26
27
28
29
# File 'lib/respect/format_validator.rb', line 25

def validate_email(value)
  unless value =~ /^[a-zA-Z0-9.!#$\%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/
    raise ValidationError, "invalid email address format '#{value}'"
  end
end

- (Object) validate_hostname(value)

Validate that the given string value describes a well-formed host name as specified by RFC 1034. (see www.rfc-editor.org/rfc/rfc1034.txt)



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/respect/format_validator.rb', line 97

def validate_hostname(value)
  match_data = HOSTNAME_REGEXP.match(value)
  if match_data
    value.split('.').each_with_index do |label, i|
      unless label.length <= 63
        raise ValidationError,
              "hostname's #{i.ordinalize} label '#{label}' is not less than 63 characters in '#{value}'"
      end
    end
  else
    raise ValidationError, "invalid hostname '#{value}'"
  end
end

- (Object) validate_ip_addr(value)

Validate that the given string value describes a well-formed IP (IPv6 or IPv4) network address using the standard "ipaddr" ruby module.



88
89
90
91
92
# File 'lib/respect/format_validator.rb', line 88

def validate_ip_addr(value)
  IPAddr.new(value)
rescue ArgumentError => e
  raise ValidationError, "invalid IP address '#{value}' - #{e.message}"
end

- (Object) validate_ipv4_addr(value)

Validate IPV4 using the standard "ipaddr" ruby module.



56
57
58
59
60
61
62
63
64
# File 'lib/respect/format_validator.rb', line 56

def validate_ipv4_addr(value)
  ipaddr = IPAddr.new(value)
  unless ipaddr.ipv4?
    raise ValidationError, "IP address '#{value}' is not IPv4"
  end
  ipaddr
rescue ArgumentError => e
  raise ValidationError, "invalid IPv4 address '#{value}' - #{e.message}"
end

- (Object) validate_ipv6_addr(value)

Validate that the given string value describes a well-formed IPV6 network address using the standard "ipaddr" ruby module.



76
77
78
79
80
81
82
83
84
# File 'lib/respect/format_validator.rb', line 76

def validate_ipv6_addr(value)
  ipaddr = IPAddr.new(value)
  unless ipaddr.ipv6?
    raise ValidationError, "IP address '#{value}' is not IPv6"
  end
  ipaddr
rescue ArgumentError => e
  raise ValidationError, "invalid IPv6 address '#{value}' - #{e.message}"
end

- (Object) validate_phone_number(value)

Validate phone number following E.123 (see en.wikipedia.org/wiki/E.123)



68
69
70
71
72
# File 'lib/respect/format_validator.rb', line 68

def validate_phone_number(value)
  unless value =~ PHONE_NUMBER_REGEXP
    raise ValidationError, "invalid phone number '#{value}'"
  end
end

- (Object) validate_regexp(value)

Validate the given string value describes a regular expression following the Ruby regular expression syntax.



41
42
43
44
45
# File 'lib/respect/format_validator.rb', line 41

def validate_regexp(value)
  Regexp.new(value)
rescue RegexpError => e
  raise ValidationError, "invalid regexp: #{e.message}"
end

- (Object) validate_uri(value)

Validate URI string format following RFC 2396 (see tools.ietf.org/html/rfc2396)



33
34
35
36
37
# File 'lib/respect/format_validator.rb', line 33

def validate_uri(value)
  URI.parse(value)
rescue URI::InvalidURIError => e
  raise ValidationError, "invalid URI: #{e.message}"
end