Class: JSON::Schema::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/json-schema/schema/reader.rb

Overview

When an unregistered schema is encountered, the Reader is used to fetch its contents and register it with the Validator.

This default reader will read schemas from the filesystem or from a URI.

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Reader

The behavior of the schema reader can be controlled by providing callbacks to determine whether to permit reading referenced schemas. The options accept_uri and accept_file should be procs which accept a URI or Pathname object, and return a boolean value indicating whether to read the referenced schema.

URIs using the file scheme will be normalized into Pathname objects and passed to the accept_file callback.

Examples:

Reject all unregistered schemas

JSON::Validator.schema_reader = JSON::Schema::Reader.new(
  :accept_uri => false,
  :accept_file => false
)

Only permit URIs from certain hosts

JSON::Validator.schema_reader = JSON::Schema::Reader.new(
  :accept_file => false,
  :accept_uri => proc { |uri| ['mycompany.com', 'json-schema.org'].include?(uri.host) }
)

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • accept_uri (Boolean, #call) — default: true
  • accept_file (Boolean, #call) — default: true

73
74
75
76
# File 'lib/json-schema/schema/reader.rb', line 73

def initialize(options = {})
  @accept_uri = options.fetch(:accept_uri, true)
  @accept_file = options.fetch(:accept_file, true)
end

Instance Method Details

#accept_file?(pathname) ⇒ Boolean

Parameters:

  • pathname (Pathname)

Returns:

  • (Boolean)

109
110
111
112
113
114
115
# File 'lib/json-schema/schema/reader.rb', line 109

def accept_file?(pathname)
  if @accept_file.respond_to?(:call)
    @accept_file.call(pathname)
  else
    @accept_file
  end
end

#accept_uri?(uri) ⇒ Boolean

Parameters:

  • uri (Addressable::URI)

Returns:

  • (Boolean)

99
100
101
102
103
104
105
# File 'lib/json-schema/schema/reader.rb', line 99

def accept_uri?(uri)
  if @accept_uri.respond_to?(:call)
    @accept_uri.call(uri)
  else
    @accept_uri
  end
end

#read(location) ⇒ JSON::Schema

Parameters:

  • location (#to_s)

    The location from which to read the schema

Returns:

Raises:

  • (JSON::Schema::ReadRefused)

    if accept_uri or accept_file indicated the schema could not be read

  • (JSON::Schema::ParseError)

    if the schema was not a valid JSON object

  • (JSON::Schema::ReadFailed)

    if reading the location was acceptable but the attempt to retrieve it failed


85
86
87
88
89
90
91
92
93
94
95
# File 'lib/json-schema/schema/reader.rb', line 85

def read(location)
  uri  = JSON::Util::URI.parse(location.to_s)
  body = if uri.scheme.nil? || uri.scheme == 'file'
           uri = JSON::Util::URI.file_uri(uri)
           read_file(Pathname.new(uri.path).expand_path)
         else
           read_uri(uri)
         end

  JSON::Schema.new(JSON::Validator.parse(body), uri)
end