Class: Owasp::Esapi::Validator::IntegerRule

Inherits:
BaseRule
  • Object
show all
Defined in:
lib/validator/integer_rule.rb

Instance Attribute Summary collapse

Attributes inherited from BaseRule

#allow_nil, #encoder, #name

Instance Method Summary collapse

Methods inherited from BaseRule

#safe, #valid?, #validate, #whitelist

Constructor Details

#initialize(type, encoder = nil, min = nil, max = nil) ⇒ IntegerRule

Returns a new instance of IntegerRule.



7
8
9
10
11
12
13
# File 'lib/validator/integer_rule.rb', line 7

def initialize(type,encoder=nil,min=nil,max=nil)
  super(type,encoder)
  @min = min
  @max = max
  @min = Integer::MIN if min.nil?
  @max = Integer::MAX if max.nil?
end

Instance Attribute Details

#maxObject

Returns the value of attribute max.



5
6
7
# File 'lib/validator/integer_rule.rb', line 5

def max
  @max
end

#minObject

Returns the value of attribute min.



5
6
7
# File 'lib/validator/integer_rule.rb', line 5

def min
  @min
end

Instance Method Details

#sanitize(context, input) ⇒ Object

SThis will call valid and return a 0 if its invalid



50
51
52
53
54
55
56
57
# File 'lib/validator/integer_rule.rb', line 50

def sanitize(context,input)
  result = 0
  begin
    result= valid(context,input)
  rescue ValidationException => e
  end
  result
end

#valid(context, input) ⇒ Object

Validate the input context as an integer



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/validator/integer_rule.rb', line 16

def valid(context,input)
  if input.nil?
    if @allow_nil
      return nil
    end
    user = "#{context}: Input number required"
    log = "Input number required: context=#{context}, input=#{input}"
    raise Owasp::Esapi::ValidationException.new(user,log,context)
  end
  clean = @encoder.canonicalize(input)
  if @min > @max
    user = "#{context}: Invalid number input: context"
    log = "Validation parameter error for number: maxValue ( #{max}) must be greater than minValue ( #{min}) for #{context}"
    raise Owasp::Esapi::ValidationException.new(user,log,context)
  end
  begin
    user = "Invalid number input must be between #{min} and #{max}: context=#{context}"
    log = "Invalid number input must be between #{min} and #{max}: context=#{context}, input=#{input}"
    i = Integer(clean)
    if i < @min
      raise Owasp::Esapi::ValidationException.new(user,log,context)
    end
    if i > @max
      raise Owasp::Esapi::ValidationException.new(user,log,context)
    end
    return i
  rescue Exception => e
    user = "#{context}: Input number required"
    log = "Input number required: context=#{context}, input=#{input}"
    raise Owasp::Esapi::ValidationException.new(user,log,context)
  end
end