Class: Owasp::Esapi::Validator::BaseRule
- Inherits:
-
Object
- Object
- Owasp::Esapi::Validator::BaseRule
- Defined in:
- lib/validator/base_rule.rb
Overview
A ValidationRule performs syntax and possibly semantic validation of a single piece of data from an untrusted source.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#allow_nil ⇒ Object
Returns the value of attribute allow_nil.
-
#encoder ⇒ Object
Returns the value of attribute encoder.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
-
#initialize(name, encoder = nil) ⇒ BaseRule
constructor
A new instance of BaseRule.
-
#safe(context, string) ⇒ Object
Try to call get valid, then call sanitize, finally return a default value.
-
#sanitize(context, input) ⇒ Object
The method is similar to getSafe except that it returns a harmless object that may or may not have any similarity to the original input (in some cases you may not care).
-
#valid(context, input) ⇒ Object
Parse the input, raise exceptions if validation fails sub classes need to implment this method as the base class will always raise an exception.
-
#valid?(context, input) ⇒ Boolean
return true if the input passes validation.
-
#validate(context, input, errors = nil) ⇒ Object
Parse the input, calling the valid method if an exception if thrown it will be added to the ValidatorErrorList object.
-
#whitelist(input, list) ⇒ Object
Removes characters that aren’t in the whitelist from the input String.
Constructor Details
Instance Attribute Details
#allow_nil ⇒ Object
Returns the value of attribute allow_nil.
16 17 18 |
# File 'lib/validator/base_rule.rb', line 16 def allow_nil @allow_nil end |
#encoder ⇒ Object
Returns the value of attribute encoder.
16 17 18 |
# File 'lib/validator/base_rule.rb', line 16 def encoder @encoder end |
#name ⇒ Object
Returns the value of attribute name.
16 17 18 |
# File 'lib/validator/base_rule.rb', line 16 def name @name end |
Instance Method Details
#safe(context, string) ⇒ Object
Try to call get valid, then call sanitize, finally return a default value
57 58 59 60 61 62 63 64 65 |
# File 'lib/validator/base_rule.rb', line 57 def safe(context,string) valid = nil begin valid = valid(context,input) rescue ValidationException => e return sanitize(context,input) end return valid end |
#sanitize(context, input) ⇒ Object
The method is similar to getSafe except that it returns a harmless object that may or may not have any similarity to the original input (in some cases you may not care). In most cases this should be the same as the getSafe method only instead of throwing an exception, return some default value. Subclasses should implment this method
72 73 74 |
# File 'lib/validator/base_rule.rb', line 72 def sanitize(context,input) input end |
#valid(context, input) ⇒ Object
Parse the input, raise exceptions if validation fails sub classes need to implment this method as the base class will always raise an exception
52 53 54 |
# File 'lib/validator/base_rule.rb', line 52 def valid(context,input) raise Owasp::Esapi::ValidationException.new(input,input,context) end |
#valid?(context, input) ⇒ Boolean
return true if the input passes validation
25 26 27 28 29 30 31 32 33 |
# File 'lib/validator/base_rule.rb', line 25 def valid?(context,input) valid = false begin valid(context,input) valid = true rescue Exception =>e end valid end |
#validate(context, input, errors = nil) ⇒ Object
Parse the input, calling the valid method if an exception if thrown it will be added to the ValidatorErrorList object. This method allows for multiple rules to be executed and collect all the errors that were invoked along the way.
39 40 41 42 43 44 45 46 47 |
# File 'lib/validator/base_rule.rb', line 39 def validate(context,input, errors=nil) valid = nil begin valid = valid(context,input) rescue ValidationException => e errors<< e unless errors.nil? end input end |
#whitelist(input, list) ⇒ Object
Removes characters that aren’t in the whitelist from the input String. chars is expected to be string
78 79 80 81 82 83 84 |
# File 'lib/validator/base_rule.rb', line 78 def whitelist(input,list) rc = '' input.chars do |c| rc << c if list.include?(c) end rc end |