Class: DiscoveryV1::Validation::ResolveSchemaRef Private

Inherits:
Object
  • Object
show all
Defined in:
lib/discovery_v1/validation/resolve_schema_ref.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Resolve a JSON schema reference to a Google Discovery V1 API schema

This class uses the Google Discovery V1 API to get the schemas. Any schema reference in the form { "$ref": "schema_name" } will be resolved by looking up the schema name in the Google Discovery V1 API and returning the schema object (as a Hash).

This means that { "$ref": "cell_data" } is resolved by returning DiscoveryV1::Validation::LoadSchemas.new(logger:).call['cell_data'].

An RuntimeError is raised if DiscoveryV1::Validation::LoadSchemas.new.call does not have a key matching the schema name.

Examples:

logger = Logger.new(STDOUT, level: Logger::INFO)
ref_resolver = DiscoveryV1::Validation::ResolveSchemaRef.new(logger:)
people_schema = { 'type' => 'array', 'items' => { '$ref' => 'person' } }
json_validator = JSONSchemer.schema(people_schema, ref_resolver:)
people_json = [{ 'name' => { 'first' => 'John', 'last' => 'Doe' } }]

# Trying to validate people_json using json_validator as follows:

json_validator.validate(people_json)

# will try to load the referenced schema for 'person'. json_validator will
# do this by calling `ref_resolver.call(URI.parse('json-schemer://schema/person'))`

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rest_description:, logger: Logger.new(nil)) ⇒ ResolveSchemaRef

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a new schema resolver

Parameters:



41
42
43
44
# File 'lib/discovery_v1/validation/resolve_schema_ref.rb', line 41

def initialize(rest_description:, logger: Logger.new(nil))
  @rest_description = rest_description
  @logger = logger
end

Instance Attribute Details

#loggerLogger (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The logger to use internally

Currently, only debug messages are logged.

Returns:

  • (Logger)


65
66
67
# File 'lib/discovery_v1/validation/resolve_schema_ref.rb', line 65

def logger
  @logger
end

#rest_descriptionGoogle::Apis::DiscoveryV1::RestDescription (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The Google Discovery V1 API description to load schemas from

Examples:

rest_description = DiscoveryV1.discovery_service.get_rest_description('sheets', 'v4')
resolver = DiscoveryV1::Validation::ResolveSchemaRef.new(rest_description:)
resolver.rest_description == rest_description # => true

Returns:



55
56
57
# File 'lib/discovery_v1/validation/resolve_schema_ref.rb', line 55

def rest_description
  @rest_description
end

Instance Method Details

#call(ref) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Resolve a JSON schema reference

Parameters:

  • ref (URI)

    the reference to resolve usually in the form "json-schemer://schema/[name]"

Returns:

  • (Hash)

    the schema object as a hash



75
76
77
78
79
80
81
82
# File 'lib/discovery_v1/validation/resolve_schema_ref.rb', line 75

def call(ref)
  schema_name = ref.path[1..]
  logger.debug { "Reading schema #{schema_name}" }
  schemas = DiscoveryV1::Validation::LoadSchemas.new(rest_description:, logger:).call
  schemas[schema_name].tap do |schema_object|
    raise "Schema for #{ref} not found" unless schema_object
  end
end