Module: JsonSchemaView::JsonWorldExtensions::CompactOptionalProperties

Included in:
JsonSchemaView::JsonSchemaDefinable
Defined in:
lib/json_schema_view/json_world_extensions/compact_optional_properties.rb

Overview

This module automatically removes properties with null value from encoded json if the property is optional.

JSON Schema distinguishs a optional property and a nullable property. If the value of a optional property is empty, you should remove the property from the encoded json. This module make safe such the optional properties.

Instance Method Summary collapse

Instance Method Details

#properties(options) ⇒ Object

Note:

Overrides JsonWorld::JsonEncodable#properties



13
14
15
16
17
# File 'lib/json_schema_view/json_world_extensions/compact_optional_properties.rb', line 13

def properties(options)
  optional_property_names = self.class.property_definitions.select(&:optional?).map(&:property_name)

  super(options).reject { |key, value| value.nil? && optional_property_names.include?(key) }
end