Class: ParamsValidator::Validator
- Inherits:
-
Object
- Object
- ParamsValidator::Validator
- Defined in:
- lib/params_validator/validator.rb
Overview
This class keeps the rulesets defined in the program and performs the validation between a specific ruleset and a set of parameters
Constant Summary collapse
- ERROR_MESSAGE_NULL =
"Parameter %s cannot be null"- ERROR_MESSAGE_TYPE =
"Parameter %s present but invalid type %s"- ERROR_MESSAGE_BLOCK =
"Parameter %s present but does not match the ruleset"
Instance Attribute Summary collapse
-
#methods_loaded ⇒ Object
readonly
Returns the value of attribute methods_loaded.
Class Method Summary collapse
-
.check_param(valid_param, param) ⇒ Object
This method validates if the specific method is valid.
-
.validate_ruleset(valid_params, params) ⇒ Object
This method validates a params hash against a specific ruleset.
Instance Method Summary collapse
- #has_method_rule?(method_name) ⇒ Boolean
-
#initialize ⇒ Validator
constructor
A new instance of Validator.
-
#load_rules(rules) ⇒ Object
This method loads new rules in memory validatation_rule method do param1 (type) &block param2 (type) &block end.
-
#validate_params(method_name, params) ⇒ Object
This method validates if the specific call to method_name is valid.
-
#validation_rule(method_name, &block) ⇒ Object
This method is part of the DSL and defines a new validator rule.
Constructor Details
#initialize ⇒ Validator
Returns a new instance of Validator.
22 23 24 |
# File 'lib/params_validator/validator.rb', line 22 def initialize @methods_loaded = {} end |
Instance Attribute Details
#methods_loaded ⇒ Object (readonly)
Returns the value of attribute methods_loaded.
20 21 22 |
# File 'lib/params_validator/validator.rb', line 20 def methods_loaded @methods_loaded end |
Class Method Details
.check_param(valid_param, param) ⇒ Object
This method validates if the specific method is valid
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/params_validator/validator.rb', line 125 def check_param(valid_param, param) if valid_param.optional? and param.nil? # argument optional and not present -> continue return nil end if param.nil? # argument mandatory and not present return ERROR_MESSAGE_NULL % valid_param.name else # check argument type is valid unless param.is_a?(valid_param.klass) return ERROR_MESSAGE_TYPE % [valid_param.name, param.class] end # check argument satisfies ruleset (if present) unless valid_param.rule.nil? !valid_param.rule.call(param) and return ERROR_MESSAGE_BLOCK % valid_param.name end end nil end |
.validate_ruleset(valid_params, params) ⇒ Object
This method validates a params hash against a specific ruleset
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/params_validator/validator.rb', line 83 def validate_ruleset(valid_params, params) errors = [] # if params.nil? but the ruleset defines all the parameters as optional # this method should return true if !valid_params.nil? and params.nil? valid_params.each{|param| !param.optional? and ( errors.push "Nil parameters when #{valid_params.length} expected" raise ArgumentError, errors ) } return true end # if just one param -> include as array with length 1 if valid_params.instance_of?(Array) && valid_params.length == 1 && params.instance_of?(String) params = {valid_params[0].name.to_sym => params} end # Validate the params valid_params.each{|key| # get param param = params[key.name.to_sym] check_result = Validator.check_param(key, param) unless check_result.nil? errors.push check_result end } unless errors.empty? raise ArgumentError, errors end # no exception -> successfully validated true end |
Instance Method Details
#has_method_rule?(method_name) ⇒ Boolean
48 49 50 51 |
# File 'lib/params_validator/validator.rb', line 48 def has_method_rule?(method_name) !method_name.is_a?(Symbol) and method_name = method_name.to_sym @methods_loaded.has_key?(method_name) end |
#load_rules(rules) ⇒ Object
This method loads new rules in memory validatation_rule method do
param1 (type) &block
param2 (type) &block
end
33 34 35 |
# File 'lib/params_validator/validator.rb', line 33 def load_rules(rules) self.instance_eval(rules) end |
#validate_params(method_name, params) ⇒ Object
This method validates if the specific call to method_name is valid
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/params_validator/validator.rb', line 58 def validate_params(method_name, params) errors = [] # fetch method rulesets method = @methods_loaded[method_name.to_sym] if method.nil? # TODO enable devel or prod mode to raise or not an exception errors.push "Unable to validate method #{method_name} with (#{@methods_loaded.keys.length}) keys #{@methods_loaded.keys}" raise ArgumentError, errors end # fetch all params (optional and mandatory) check_params = method.parameters self.class.validate_ruleset(check_params, params) end |
#validation_rule(method_name, &block) ⇒ Object
This method is part of the DSL and defines a new validator rule
42 43 44 45 46 |
# File 'lib/params_validator/validator.rb', line 42 def validation_rule(method_name, &block) method = MethodValidation.new(method_name) block_given? and method.block &block @methods_loaded[:"#{method_name}"] = method end |