Class: Rack::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/validator/sinatra.rb

Defined Under Namespace

Modules: Sinatra

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, lazy_mode = true) ⇒ Validator

Returns a new instance of Validator.



11
12
13
14
15
16
17
# File 'lib/rack/validator/sinatra.rb', line 11

def initialize(params, lazy_mode = true)
  @params = params
  @invalid_params = []
  @missing_params = []
  @messages = []
  @lazy_mode = lazy_mode
end

Instance Attribute Details

#invalid_paramsObject (readonly)

Returns the value of attribute invalid_params.



6
7
8
# File 'lib/rack/validator/sinatra.rb', line 6

def invalid_params
  @invalid_params
end

#lazy_modeObject

Returns the value of attribute lazy_mode.



9
10
11
# File 'lib/rack/validator/sinatra.rb', line 9

def lazy_mode
  @lazy_mode
end

#messagesObject (readonly)

Returns the value of attribute messages.



8
9
10
# File 'lib/rack/validator/sinatra.rb', line 8

def messages
  @messages
end

#missing_paramsObject (readonly)

Returns the value of attribute missing_params.



7
8
9
# File 'lib/rack/validator/sinatra.rb', line 7

def missing_params
  @missing_params
end

Instance Method Details

#downcase(keys) ⇒ Object



27
28
29
# File 'lib/rack/validator/sinatra.rb', line 27

def downcase(keys)
  keys.each { |k| @params[k.to_s] = @params[k.to_s].to_s.downcase if @params[k.to_s]}
end

#has_errors?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/rack/validator/sinatra.rb', line 19

def has_errors?
  @invalid_params.length > 0 or @missing_params.length > 0
end

#is_boolean(key) ⇒ Object



130
131
132
133
134
135
136
137
138
# File 'lib/rack/validator/sinatra.rb', line 130

def is_boolean(key)
  if lazy_check_disabled
    key = key.to_s
    unless @params[key] == 'true' || @params[key] == 'false'
      @invalid_params.push(key)
      @messages.push("#{key} is not a boolean")
    end
  end
end

#is_email(key) ⇒ Object



120
121
122
123
124
125
126
127
128
# File 'lib/rack/validator/sinatra.rb', line 120

def is_email(key)
  if lazy_check_disabled
    key = key.to_s
    unless @params[key].to_s[/^\S+@\S+\.\S+$/]
      @invalid_params.push(key)
      @messages.push("#{key} is not a valid email")
    end
  end
end

#is_float(key) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/rack/validator/sinatra.rb', line 55

def is_float(key)
  if lazy_check_disabled
    key = key.to_s
    value = @params[key]
    unless is_valid_float(value)
      @invalid_params.push(key)
      @messages.push("#{key} is not a float")
    end
  end
end

#is_greater_equal_than(min, key) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rack/validator/sinatra.rb', line 66

def is_greater_equal_than(min, key)
  if lazy_check_disabled
    key = key.to_s
    if !is_valid_float(@params[key])
      if !@params[key] || @params[key].length < min
        @invalid_params.push(key)
        @messages.push("#{key} length is less than #{min}")
      end
    else
      value = is_valid_integer(@params[key]) ? @params[key].to_i : @params[key].to_f
      if  !value || value < min
        @invalid_params.push(key)
        @messages.push("#{key} is less than #{min}")
      end
    end
  end
end

#is_in_range(min, max, key) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rack/validator/sinatra.rb', line 84

def is_in_range(min, max, key)
  if lazy_check_disabled
    key = key.to_s
    unless is_valid_float(@params[key])
      if !@params[key] || @params[key].length < min || @params[key].length > max
        @invalid_params.push(key)
        @messages.push("#{key} length is not in range #{min},#{max}")
      end
    else
      value = is_valid_integer(@params[key]) ? @params[key].to_i : @params[key].to_f
      if !value || value < min || value > max
        @invalid_params.push(key)
        @messages.push("#{key} is not in range #{min},#{max}")
      end
    end
  end
end

#is_integer(key) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/rack/validator/sinatra.rb', line 44

def is_integer(key)
  if lazy_check_disabled
    key = key.to_s
    value = @params[key]
    unless is_valid_integer(value)
      @invalid_params.push(key)
      @messages.push("#{key} is not an integer")
    end
  end
end

#is_less_equal_than(max, key) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/rack/validator/sinatra.rb', line 102

def is_less_equal_than(max, key)
  if lazy_check_disabled
    key = key.to_s
    unless is_valid_float(@params[key])
      if !@params[key] || @params[key].length > max
        @invalid_params.push(key)
        @messages.push("#{key} length is greater than #{max}")
      end
    else
      value = is_valid_integer(@params[key]) ? @params[key].to_i : @params[key].to_f
      if !value || value > max
        @invalid_params.push(key)
        @messages.push("#{key} is greater than #{max}")
      end
    end
  end
end

#matches(regexp, key) ⇒ Object



140
141
142
143
144
145
146
147
148
# File 'lib/rack/validator/sinatra.rb', line 140

def matches(regexp, key)
  if lazy_check_disabled
    key = key.to_s
    unless @params[key] =~ regexp
      @invalid_params.push(key)
      @messages.push("#{key} is not a valid expression")
    end
  end
end

#required(keys) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rack/validator/sinatra.rb', line 31

def required(keys)
  if lazy_check_disabled
    keys.each { |k|
      k = k.to_s
      unless @params.has_key?(k) && !@params[k].empty?
        @invalid_params.push(k)
        @missing_params.push(k)
        @messages.push("#{k} is required")
      end
    }
  end
end

#trimObject



23
24
25
# File 'lib/rack/validator/sinatra.rb', line 23

def trim
  @params.each_key { |k| @params[k] = @params[k].strip if @params[k]}
end