Class: IknowParams::Serializer::JsonWithSchema
- Inherits:
-
IknowParams::Serializer
- Object
- IknowParams::Serializer
- IknowParams::Serializer::JsonWithSchema
- Defined in:
- lib/iknow_params/serializer.rb,
lib/iknow_params/serializer.rb
Overview
Adds Rails conveniences
Direct Known Subclasses
Defined Under Namespace
Classes: Rails
Instance Attribute Summary collapse
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
Attributes inherited from IknowParams::Serializer
Instance Method Summary collapse
- #dump(val, json: false) ⇒ Object
-
#initialize(schema, validate_schema: true) ⇒ JsonWithSchema
constructor
A new instance of JsonWithSchema.
- #load(structure) ⇒ Object
- #matches_type?(val) ⇒ Boolean
Methods inherited from IknowParams::Serializer
for, for!, json_value?, #matches_type!, singleton
Constructor Details
#initialize(schema, validate_schema: true) ⇒ JsonWithSchema
Returns a new instance of JsonWithSchema.
260 261 262 263 264 |
# File 'lib/iknow_params/serializer.rb', line 260 def initialize(schema, validate_schema: true) @schema = schema @validate_schema = validate_schema super(nil) end |
Instance Attribute Details
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
259 260 261 |
# File 'lib/iknow_params/serializer.rb', line 259 def schema @schema end |
Instance Method Details
#dump(val, json: false) ⇒ Object
274 275 276 277 278 279 280 281 |
# File 'lib/iknow_params/serializer.rb', line 274 def dump(val, json: false) matches_type!(val) if json val else JSON.dump(val) end end |
#load(structure) ⇒ Object
266 267 268 269 270 271 272 |
# File 'lib/iknow_params/serializer.rb', line 266 def load(structure) structure = JSON.parse(structure) if structure.is_a?(::String) matches_type!(structure, err: LoadError) structure rescue JSON::ParserError => ex raise LoadError.new("Invalid JSON: #{ex.}") end |
#matches_type?(val) ⇒ Boolean
283 284 285 |
# File 'lib/iknow_params/serializer.rb', line 283 def matches_type?(val) JSON::Validator.validate(schema, val, validate_schema: @validate_schema) end |