Class: Owasp::Esapi::Validator::FloatRule

Inherits:
BaseRule
  • Object
show all
Defined in:
lib/validator/float_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) ⇒ FloatRule

Returns a new instance of FloatRule.



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

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

Instance Attribute Details

#maxObject

Returns the value of attribute max.



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

def max
  @max
end

#minObject

Returns the value of attribute min.



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

def min
  @min
end

Instance Method Details

#sanitize(context, input) ⇒ Object

This will call valid and return a 0 if its invalid



65
66
67
68
69
70
71
72
# File 'lib/validator/float_rule.rb', line 65

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/validator/float_rule.rb', line 16

def valid(context,input)
  if input.nil?
    if @allow_nil
      return nil
    end
    puts "::#{input}::"
    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 = Float(clean)
    #check min
    if i < @min
      raise Owasp::Esapi::ValidationException.new(user,log,context)
    end
    # check max
    if i > @max
      raise Owasp::Esapi::ValidationException.new(user,log,context)
    end
    # check infinity
    if i.infinite?
      user = "#{context}: Invalid number input: context"
      log = "Invalid double input is infinite context=#{context} input=#{input}"
      raise Owasp::Esapi::ValidationException.new(user,log,context)
    end
    # checknan
    if i.nan?
      user = "#{context}: Invalid number input: context"
      log = "Invalid double input not a number context=#{context} input=#{input}"
      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