Module: Committee::Validation

Included in:
ParamValidator, ResponseValidator
Defined in:
lib/committee/validation.rb

Instance Method Summary collapse

Instance Method Details

#check_format(format, value, identifier) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/committee/validation.rb', line 17

def check_format(format, value, identifier)
  case format
  when "date-time"
    value =~ /^(\d{4})-(\d{2})-(\d{2})T(\d{2})\:(\d{2})\:(\d{2})(\.\d{1,})?(Z|[+-](\d{2})\:(\d{2}))$/
  when "email"
    value =~ /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i
  when "uuid"
    value =~ /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/
  else
    true
  end
end

#check_format!(format, value, identifier) ⇒ Object

Raises:



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/committee/validation.rb', line 3

def check_format!(format, value, identifier)
  return if !format
  return if check_format(format, value, identifier)

  description = case identifier
  when String
    %{Invalid format for key "#{identifier}": expected "#{value}" to be "#{format}".}
  when Array
    %{Invalid format at "#{identifier.join(":")}": expected "#{value}" to be "#{format}".}
  end

  raise InvalidFormat, description
end

#check_pattern(pattern, value, identifier) ⇒ Object



77
78
79
# File 'lib/committee/validation.rb', line 77

def check_pattern(pattern, value, identifier)
  !pattern || value =~ pattern
end

#check_pattern!(pattern, value, identifier) ⇒ Object

Raises:



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/committee/validation.rb', line 64

def check_pattern!(pattern, value, identifier)
  return if check_pattern(pattern, value, identifier)

  description = case identifier
  when String
    %{Invalid pattern for key "#{identifier}": expected #{value} to match "#{pattern}".}
  when Array
      %{Invalid pattern at "#{identifier.join(":")}": expected #{value} to match "#{pattern}".}
  end

  raise InvalidPattern, description
end

#check_type(allowed_types, value, identifier) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/committee/validation.rb', line 43

def check_type(allowed_types, value, identifier)
  types = case value
  when NilClass
    ["null"]
  when TrueClass, FalseClass
    ["boolean"]
  when Bignum, Fixnum
    ["integer", "number"]
  when Float
    ["number"]
  when Hash
    ["object"]
  when String
    ["string"]
  else
    ["unknown"]
  end

  !(allowed_types & types).empty?
end

#check_type!(allowed_types, value, identifier) ⇒ Object

Raises:



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/committee/validation.rb', line 30

def check_type!(allowed_types, value, identifier)
  return if check_type(allowed_types, value, identifier)

  description = case identifier
  when String
    %{Invalid type for key "#{identifier}": expected #{value} to be #{allowed_types}.}
  when Array
    %{Invalid type at "#{identifier.join(":")}": expected #{value} to be #{allowed_types}.}
  end

  raise InvalidType, description
end