Class: StructuredStore::SchemaInspector

Inherits:
Object
  • Object
show all
Defined in:
lib/structured_store/schema_inspector.rb

Overview

This class inspects a JSON Schema and provides methods to retrieve property and definition schemas.

It allows us to abstract away the implementation details of JSONSchemer and provides a clean interface for working with JSON Schemas.

Constant Summary collapse

MAX_JSON_INPUT_SIZE_BYTES =
1_048_576

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ SchemaInspector

Returns a new instance of SchemaInspector.



14
15
16
# File 'lib/structured_store/schema_inspector.rb', line 14

def initialize(schema)
  @original_schema = schema
end

Instance Method Details

#definition_schema(definition_name) ⇒ Object



28
29
30
# File 'lib/structured_store/schema_inspector.rb', line 28

def definition_schema(definition_name)
  schema_hash.dig('definitions', definition_name.to_s)
end

#property_schema(property_name) ⇒ Object



24
25
26
# File 'lib/structured_store/schema_inspector.rb', line 24

def property_schema(property_name)
  schema_hash.dig('properties', property_name.to_s)
end

#schema_hashObject



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/structured_store/schema_inspector.rb', line 45

def schema_hash
  @schema_hash =
    case @original_schema
    when Hash
      # TODO: ensure the hash is safe to use (e.g. not too large)
      @original_schema.deep_stringify_keys
    when String
      safe_parse_json(@original_schema)
    else
      raise ArgumentError, "Unsupported schema type: #{@original_schema.class}"
    end
end

#valid_schema?Boolean

Returns:

  • (Boolean)


18
19
20
21
22
# File 'lib/structured_store/schema_inspector.rb', line 18

def valid_schema?
  JSONSchemer.draft201909.valid?(schema_hash)
rescue ArgumentError
  false
end