Class: Restapi::Validator::HashValidator
Instance Attribute Summary collapse
#param_description
Class Method Summary
collapse
Instance Method Summary
collapse
find, inherited, #param_name, #to_json, #to_s, #valid?
Constructor Details
#initialize(param_description, argument) ⇒ HashValidator
Returns a new instance of HashValidator.
187
188
189
190
191
192
193
194
|
# File 'lib/restapi/validator.rb', line 187
def initialize(param_description, argument)
super(param_description)
@proc = argument
@hash_params_ordered = []
@hash_params = {}
self.instance_exec(&@proc)
end
|
Instance Attribute Details
#hash_params_ordered ⇒ Object
Returns the value of attribute hash_params_ordered.
181
182
183
|
# File 'lib/restapi/validator.rb', line 181
def hash_params_ordered
@hash_params_ordered
end
|
Class Method Details
.build(param_description, argument, options, block) ⇒ Object
183
184
185
|
# File 'lib/restapi/validator.rb', line 183
def self.build(param_description, argument, options, block)
self.new(param_description, block) if block.is_a?(Proc) && block.arity <= 0 && argument == Hash
end
|
Instance Method Details
#description ⇒ Object
209
210
211
|
# File 'lib/restapi/validator.rb', line 209
def description
"Has to be hash."
end
|
#error ⇒ Object
205
206
207
|
# File 'lib/restapi/validator.rb', line 205
def error
"Has to be hash."
end
|
#expected_type ⇒ Object
220
221
222
|
# File 'lib/restapi/validator.rb', line 220
def expected_type
'hash'
end
|
#param(param_name, *args, &block) ⇒ Object
213
214
215
216
217
218
|
# File 'lib/restapi/validator.rb', line 213
def param(param_name, *args, &block)
param_description = Restapi::ParamDescription.new(param_name, *args, &block)
param_description.parent = self.param_description
@hash_params_ordered << param_description
@hash_params[param_name.to_sym] = param_description
end
|
#validate(value) ⇒ Object
196
197
198
199
200
201
202
203
|
# File 'lib/restapi/validator.rb', line 196
def validate(value)
if @hash_params
@hash_params.each do |k, p|
p.validate(value[k]) if value.has_key?(k) || p.required
end
end
return true
end
|