Sha256: ee61c06b15995f752b3a1008aa186c1e9d132905e77f647c672771e1d9d901fe

Contents?: true

Size: 1.42 KB

Versions: 9

Compression:

Stored size: 1.42 KB

Contents

module Lurker
  module Json
    class Object < Schema
      def merge!(schema)
        unless schema.is_a?(Hash)
          raise TypeError, "Unable to merge #{schema.class} into JSON object"
        end

        schema.each do |property, property_schema|
          if @schema[Json::PROPERTIES].key?(property)
            @schema[Json::PROPERTIES][property].merge!(property_schema)
            next
          end

          replace!(property, property_schema)
        end
      end

      def replace!(property, property_schema)
        @schema[Json::PROPERTIES][property] = Lurker::Json::Parser.typed(subschema_options)
          .parse_property(property, property_schema)
      end

      private

      def initialize_default_properties
        @schema[Json::DESCRIPTION] ||= ''
        @schema[Json::TYPE] ||= Json::OBJECT
        @schema[Json::ADDITIONAL_PROPERTIES] = !!@schema[Json::ADDITIONAL_PROPERTIES]
        @schema[Json::REQUIRED] ||= []
        @schema[Json::PROPERTIES] ||= {}
      end

      def parse_schema(schema)
        @schema = {}

        schema = schema.dup
        if schema.key?(Json::PROPERTIES)
          @schema.merge!(schema)
        end

        initialize_default_properties

        (schema.delete(Json::PROPERTIES) || schema).each do |property, property_schema|
          @schema[Json::PROPERTIES][property] = @parser.typed.parse_property(
            property, property_schema)
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
lurker-1.0.0 lib/lurker/json/schema/object.rb
lurker-0.6.12 lib/lurker/json/schema/object.rb
lurker-0.6.11 lib/lurker/json/schema/object.rb
lurker-0.6.10 lib/lurker/json/schema/object.rb
lurker-0.6.9 lib/lurker/json/schema/object.rb
lurker-0.6.8 lib/lurker/json/schema/object.rb
lurker-0.6.7 lib/lurker/json/schema/object.rb
lurker-0.6.6 lib/lurker/json/schema/object.rb
lurker-0.6.5 lib/lurker/json/schema/object.rb