Sha256: a2a1269dfa06acd2065525e0a9d9f8369a41d0ea5ac42248de13b453280fbfcc

Contents?: true

Size: 865 Bytes

Versions: 5

Compression:

Stored size: 865 Bytes

Contents

# frozen_string_literal: true

module Jimmy
  class Schema
    # The JSON Schema draft 7 schema URI
    SCHEMA = 'http://json-schema.org/draft-07/schema#'

    # Get the schema as a plain Hash. Given an +id+, the +$id+ and +$schema+
    # keys will also be set.
    # @param [Json::URI, URI, String] id
    # @return [Hash, true, false]
    def as_json(id: '', index: nil)
      id = Json::URI.new(id)

      if index.nil? && id.absolute?
        return top_level_json(id) { super index: {}, id: id }
      end

      return true if anything?
      return false if nothing?

      super index: index || {}, id: id
    end

    private

    def top_level_json(id)
      hash = {
        '$id'     => id.to_s,
        '$schema' => SCHEMA
      }
      if nothing?
        hash['not'] = true
      else
        hash.merge! yield
      end
      hash
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
jimmy-2.1.0 lib/jimmy/schema/json.rb
jimmy-2.0.3 lib/jimmy/schema/json.rb
jimmy-2.0.2 lib/jimmy/schema/json.rb
jimmy-2.0.1 lib/jimmy/schema/json.rb
jimmy-2.0.0 lib/jimmy/schema/json.rb