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



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

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.



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

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

#paramsObject (readonly)

Returns the value of attribute params.



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

def params
  @params
end

Instance Method Details

#clean_parameters(all_parameters) ⇒ Object



161
162
163
164
165
# File 'lib/rack/validator/sinatra.rb', line 161

def clean_parameters(all_parameters)
  @params.each_key do |key|
    @params.delete key.to_s unless all_parameters.include? key.to_s
  end
end

#downcase(key) ⇒ Object



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

def downcase(key)
  @params[key.to_s] = @params[key.to_s].to_s.downcase if @params[key.to_s]
end

#has_errors?Boolean



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

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

#is_boolean(key) ⇒ Object



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

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



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

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



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

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



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

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



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

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



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

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



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

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

#is_set(array, key) ⇒ Object



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

def is_set(array, key)
  if lazy_check_disabled
    key = key.to_s
    unless array.include? @params[key]
      @invalid_params.push(key)
      @messages.push("#{key} does not match #{array}")
    end
  end
end

#matches(regexp, key) ⇒ Object



151
152
153
154
155
156
157
158
159
# File 'lib/rack/validator/sinatra.rb', line 151

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



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

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

#trim(key) ⇒ Object



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

def trim(key)
  @params[key.to_s] = @params[key.to_s].strip if @params[key.to_s]
end