Class: Grape::Validations::CoerceValidator
- Inherits:
-
Base
- Object
- Base
- Grape::Validations::CoerceValidator
show all
- Defined in:
- lib/grape/validations/validators/coerce.rb
Instance Attribute Summary
Attributes inherited from Base
#attrs
Instance Method Summary
collapse
Methods inherited from Base
convert_to_short_name, #fail_fast?, inherited, #message, #options_key?, #validate!
Constructor Details
8
9
10
11
|
# File 'lib/grape/validations/validators/coerce.rb', line 8
def initialize(*_args)
super
@converter = Types.build_coercer(type, @option[:method])
end
|
Instance Method Details
#validate(request) ⇒ Object
13
14
15
|
# File 'lib/grape/validations/validators/coerce.rb', line 13
def validate(request)
super
end
|
#validate_param!(attr_name, params) ⇒ Object
17
18
19
20
21
22
23
|
# File 'lib/grape/validations/validators/coerce.rb', line 17
def validate_param!(attr_name, params)
raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: message(:coerce) unless params.is_a? Hash
return unless requires_coercion?(params[attr_name])
new_value = coerce_value(params[attr_name])
raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: message(:coerce) unless valid_type?(new_value)
params[attr_name] = new_value
end
|