Sha256: b798b63b935442a88573125405bfe99a33f81f09375b943b24af49ef1cce58ff

Contents?: true

Size: 1.41 KB

Versions: 5

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true
module JSONSchemer
  module Output
    FRAGMENT_ENCODE_REGEX = /[^\w?\/:@\-.~!$&'()*+,;=]/

    attr_reader :keyword, :schema

    def x_error
      return @x_error if defined?(@x_error)
      @x_error = schema.parsed['x-error']&.message(error_key)
    end

  private

    def result(instance, instance_location, keyword_location, valid, nested = nil, type: nil, annotation: nil, details: nil, ignore_nested: false)
      Result.new(self, instance, instance_location, keyword_location, valid, nested, type, annotation, details, ignore_nested, valid ? 'annotations' : 'errors')
    end

    def escaped_keyword
      @escaped_keyword ||= Location.escape_json_pointer_token(keyword)
    end

    def join_location(location, keyword)
      Location.join(location, keyword)
    end

    def fragment_encode(location)
      Format.percent_encode(location, FRAGMENT_ENCODE_REGEX)
    end

    # :nocov:
    if Symbol.method_defined?(:name)
      def stringify(key)
        key.is_a?(Symbol) ? key.name : key.to_s
      end
    else
      def stringify(key)
        key.to_s
      end
    end
    # :nocov:

    def deep_stringify_keys(obj)
      case obj
      when Hash
        obj.each_with_object({}) do |(key, value), out|
          out[stringify(key)] = deep_stringify_keys(value)
        end
      when Array
        obj.map { |item| deep_stringify_keys(item) }
      else
        obj
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
json_schemer-2.3.0 lib/json_schemer/output.rb
json_schemer-2.2.1 lib/json_schemer/output.rb
json_schemer-2.2.0 lib/json_schemer/output.rb
json_schemer-2.1.1 lib/json_schemer/output.rb
json_schemer-2.1.0 lib/json_schemer/output.rb