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.
249 250 251 252 253 |
# File 'lib/iknow_params/serializer.rb', line 249 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.
248 249 250 |
# File 'lib/iknow_params/serializer.rb', line 248 def schema @schema end |
Instance Method Details
#dump(val, json: false) ⇒ Object
263 264 265 266 267 268 269 270 |
# File 'lib/iknow_params/serializer.rb', line 263 def dump(val, json: false) matches_type!(val) if json val else JSON.dump(val) end end |
#load(structure) ⇒ Object
255 256 257 258 259 260 261 |
# File 'lib/iknow_params/serializer.rb', line 255 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.message}") end |
#matches_type?(val) ⇒ Boolean
272 273 274 |
# File 'lib/iknow_params/serializer.rb', line 272 def matches_type?(val) JSON::Validator.validate(schema, val, validate_schema: @validate_schema) end |