class JSON::SchemaDsl::Renderers::Desugar

By default the first renderer that visits the tree. This renderer will translate all kinds of `syntax sugar` to a uniform format that fits json-schema.

Public Instance Methods

visit(entity) click to toggle source

Desugars all syntax sugar in that entity. This resolves concepts like `children` and `nullable` that are not present in the json schema specification itself but are used by the builder to ease the writing of schemas. In turn it

  • Transforms children to properties

  • Translates the `:required` attribute to the parent if true.

  • Collapses the item attribute of arrays into a single entity.

# File lib/json/schema_dsl/renderers/desugar.rb, line 18
def visit(entity)
  traverse(expand_children(nullable(entity)))
end

Private Instance Methods

collapse_items(entity) click to toggle source

Collapses the items into the first child for arrays.

# File lib/json/schema_dsl/renderers/desugar.rb, line 34
def collapse_items(entity)
  items = entity[:items]
  items = items[:children].first if items[:children].to_a.count == 1
  entity.merge(items: items)
end
expand_children(entity) click to toggle source
# File lib/json/schema_dsl/renderers/desugar.rb, line 24
def expand_children(entity)
  return entity unless entity[:children]
  return collapse_items(entity) if entity[:items]

  entity
    .merge(required_properties(entity))
    .merge(properties_properties(entity))
end
nullable(entity) click to toggle source

Translates nullable property into a any_of: [null…]

# File lib/json/schema_dsl/renderers/desugar.rb, line 75
def nullable(entity)
  return entity unless entity[:nullable]

  entity.merge(nullable: nil, any_of: [{ type: 'null' }]) do |k, old, new|
    next unless k == :any_of

    old + new
  end
end
properties_properties(entity) click to toggle source

@param [Hash] entity An object entity-hash that has children. @return [Hash] The hash with children translated into properties and @todo Enable and fix rubocop warning about AbcSize rubocop:disable Metrics/AbcSize

# File lib/json/schema_dsl/renderers/desugar.rb, line 44
def properties_properties(entity)
  entity[:children]
    .filter { |ch| ch[:name].present? }
    .map { |ch| ch[:name] }
    .zip(entity[:children].map { |c| visit(c) })
    .map(&unrequire_property)
    .group_by { |(name, _obj)| name.class }
    .transform_keys { |k| k == Regexp ? :pattern_properties : :properties }
    .transform_values(&:to_h)
    .merge(children: nil)
end
required_properties(entity) click to toggle source

Translates the required-properties of children into the required array of the parent structure.

# File lib/json/schema_dsl/renderers/desugar.rb, line 66
def required_properties(entity)
  requireds = entity[:children]
              .select { |ch| ch[:required] == true }
              .map { |ch| ch[:name].to_s }
  pre_req = entity[:required].is_a?(Array) ? entity[:required] : []
  { required: requireds | pre_req }
end
unrequire_property() click to toggle source

rubocop:enable Metrics/AbcSize

# File lib/json/schema_dsl/renderers/desugar.rb, line 57
def unrequire_property
  lambda do |(name, obj)|
    obj = obj[:required] == true ? obj.merge(required: nil) : obj
    [name, obj]
  end
end