Class: Grape::Validations::Types::Json
- Inherits:
-
Virtus::Attribute
- Object
- Virtus::Attribute
- Grape::Validations::Types::Json
- Defined in:
- lib/grape/validations/types/json.rb
Overview
Virtus::Attribute
implementation that handles coercion and type checking for parameters that are complex types given as JSON-encoded strings. It accepts both JSON objects and arrays of objects, and will coerce the input to a Hash
or Array
object respectively. In either case the Grape validation system will apply nested validation rules to all returned objects.
Direct Known Subclasses
Instance Method Summary collapse
-
#coerce(input) ⇒ Hash, ...
Coerce the input into a JSON-like data structure.
-
#value_coerced?(value) ⇒ true, false
Checks that the input was parsed successfully and isn’t something odd such as an array of primitives.
Instance Method Details
#coerce(input) ⇒ Hash, ...
Coerce the input into a JSON-like data structure.
18 19 20 21 22 |
# File 'lib/grape/validations/types/json.rb', line 18 def coerce(input) # Allow nulls and blank strings return if input.nil? || input =~ /^\s*$/ JSON.parse(input, symbolize_names: true) end |
#value_coerced?(value) ⇒ true, false
Checks that the input was parsed successfully and isn’t something odd such as an array of primitives.
29 30 31 |
# File 'lib/grape/validations/types/json.rb', line 29 def value_coerced?(value) value.is_a?(::Hash) || coerced_collection?(value) end |