Class: Aspera::Schema::Reader
- Inherits:
-
Object
- Object
- Aspera::Schema::Reader
- Defined in:
- lib/aspera/schema/reader.rb
Overview
JSON schema reader
Instance Attribute Summary collapse
-
#current ⇒ Object
readonly
Returns the value of attribute current.
Class Method Summary collapse
-
.from_query_params(params) ⇒ Reader
Build a synthetic Reader from an OAS
parametersarray (entries within: query).
Instance Method Summary collapse
-
#[](key) ⇒ Hash, ...
Shortcut to access current value at path.
-
#dig(*path) ⇒ Object
Find sub path relative to current Honors $ref.
-
#each_property(prefix = '', on_variant: nil, parent_required: []) {|property_schema, name, full_name, required| ... } ⇒ nil
Recursively traverse schema properties with a block.
-
#initialize(root, current = nil) ⇒ Hash?
constructor
Read schema from file or from cache.
-
#resolve_ref(ref) ⇒ Object
Resolve a $ref string to a Reader.
-
#to_rows ⇒ Array<Hash>
Convert this schema to a flat array of field descriptors.
Constructor Details
Instance Attribute Details
#current ⇒ Object (readonly)
Returns the value of attribute current.
17 18 19 |
# File 'lib/aspera/schema/reader.rb', line 17 def current @current end |
Class Method Details
.from_query_params(params) ⇒ Reader
Build a synthetic Reader from an OAS parameters array (entries with in: query).
Produces a JSON Schema object whose properties map each query param name to its schema,
with the OAS-level description and required merged in.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/aspera/schema/reader.rb', line 25 def from_query_params(params) properties = {} required_names = [] params.each do |param| next unless param['in'] == 'query' name = param['name'] prop = (param['schema'] || {}).dup prop['description'] = param['description'] if param['description'] && !prop.key?('description') properties[name] = prop required_names << name if param['required'] end synthetic = {'type' => 'object', 'properties' => properties} synthetic['required'] = required_names unless required_names.empty? new(synthetic) end |
Instance Method Details
#[](key) ⇒ Hash, ...
Shortcut to access current value at path
45 46 47 |
# File 'lib/aspera/schema/reader.rb', line 45 def [](key) @current[key] end |
#dig(*path) ⇒ Object
Find sub path relative to current Honors $ref
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/aspera/schema/reader.rb', line 51 def dig(*path) current = @current path.each do |p| Aspera.assert(current.key?(p)) { "schema: #{p} in #{path}" } current = current[p] Aspera.assert_type(current, Hash) { 'schema' } if current.key?('$ref') ref = current['$ref'] Aspera.assert(ref.start_with?('#/')) { "schema $ref must start with '#/': #{ref}" } current = @root.dig(*ref[2..].split('/')) end end Reader.new(@root, current) end |
#each_property(prefix = '', on_variant: nil, parent_required: []) {|property_schema, name, full_name, required| ... } ⇒ nil
Recursively traverse schema properties with a block.
If the current node has oneOf, each variant is traversed in turn and
on_variant is called (if given) before each variant's properties.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/aspera/schema/reader.rb', line 93 def each_property(prefix = '', on_variant: nil, parent_required: [], &block) required_names = parent_required + Array(@current['required']) if @current.key?('oneOf') # Build reverse map: $ref -> discriminant value, from discriminator.mapping if present discriminant_by_ref = {} if @current.dig('discriminator', 'mapping').is_a?(Hash) @current['discriminator']['mapping'].each do |value, ref| discriminant_by_ref[ref] = value end end discriminant_property = @current.dig('discriminator', 'propertyName') # Properties common to the variants, except the selector, given with each variant (`on_variant`) each_listed_property(prefix, on_variant, required_names, except: discriminant_property, &block) if @current.key?('properties') @current['oneOf'].each do |variant_node| ref = variant_node['$ref'] variant_reader = ref ? resolve_ref(ref) : Reader.new(@root, variant_node) discriminant_value = ref ? discriminant_by_ref[ref] : nil on_variant&.call(variant_reader, discriminant_property, discriminant_value) variant_reader.each_property(prefix, on_variant: on_variant, parent_required: required_names, &block) end return end if @current.key?('allOf') # Merge all branches: each branch contributes its properties (no variants). # A property is listed once, with its first declaration: a later one may be empty, # only to allow the property under `additionalProperties: false`. # A property is required if any branch requires it. branches = @current['allOf'].map { |branch_node| branch_reader(branch_node) } required_names += branches.flat_map { |branch| Array(branch.current['required']) } listed = Set.new branches.each do |branch| # A branch may only constrain properties of other branches, e.g. with `required` next unless PROPERTY_SOURCES.any? { |key| branch.current.key?(key) } branch.each_property(prefix, on_variant: on_variant, parent_required: required_names) do |property_schema, name, full_name, required| yield(property_schema, name, full_name, required) if listed.add?(full_name) end end return end each_listed_property(prefix, on_variant, required_names, &block) end |
#resolve_ref(ref) ⇒ Object
Resolve a $ref string to a Reader
67 68 69 70 |
# File 'lib/aspera/schema/reader.rb', line 67 def resolve_ref(ref) Aspera.assert(ref.start_with?('#/')) { "schema $ref must start with '#/': #{ref}" } Reader.new(@root, @root.dig(*ref[2..].split('/'))) end |
#to_rows ⇒ Array<Hash>
Convert this schema to a flat array of field descriptors. Returns raw semantic fields with no ANSI or formatting, suitable for JSON/YAML output or MCP consumption. An AI or script can use the result directly to build a valid payload.
Each Hash entry contains:
name [String] dot/bracket-path of the field (e.g. "recipients[].name")
type [String] JSON type string (e.g. "string", "boolean", "Array[object]")
required [Boolean] true when the field is in its immediate parent's required list
description [String] human description (may contain Markdown **bold** / `code`)
default [Object] (optional) default value as native Ruby type
enum [Array] (optional) list of allowed string values
complete [String] (optional) kind of value proposed by shell completion (`x-cli-complete`), e.g. `file`
181 182 183 184 185 |
# File 'lib/aspera/schema/reader.rb', line 181 def to_rows rows = [] collect_rows(rows, self, '') rows end |