verquest 0.6.4 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +57 -1
  3. data/README.md +325 -45
  4. data/Rakefile +5 -0
  5. data/docs/rails.md +169 -0
  6. data/lib/verquest/base/condition_class_methods.rb +211 -0
  7. data/lib/verquest/base/private_class_methods.rb +84 -20
  8. data/lib/verquest/base/public_class_methods.rb +51 -1
  9. data/lib/verquest/base.rb +1 -0
  10. data/lib/verquest/conditions/branch.rb +111 -0
  11. data/lib/verquest/conditions/condition.rb +189 -0
  12. data/lib/verquest/conditions/conjunction.rb +39 -0
  13. data/lib/verquest/conditions/fragment_merge.rb +52 -0
  14. data/lib/verquest/conditions/property_merge.rb +47 -0
  15. data/lib/verquest/conditions/rule_set.rb +74 -0
  16. data/lib/verquest/conditions/rules/absent.rb +41 -0
  17. data/lib/verquest/conditions/rules/alternatives.rb +28 -0
  18. data/lib/verquest/conditions/rules/any.rb +43 -0
  19. data/lib/verquest/conditions/rules/equals.rb +22 -0
  20. data/lib/verquest/conditions/rules/names_rule.rb +23 -0
  21. data/lib/verquest/conditions/rules/none.rb +40 -0
  22. data/lib/verquest/conditions/rules/present.rb +38 -0
  23. data/lib/verquest/conditions/rules/property_rule.rb +66 -0
  24. data/lib/verquest/conditions/rules/satisfies.rb +22 -0
  25. data/lib/verquest/conditions/rules/value_in.rb +22 -0
  26. data/lib/verquest/conditions/rules.rb +17 -0
  27. data/lib/verquest/conditions/variant_shape.rb +39 -0
  28. data/lib/verquest/conditions.rb +36 -0
  29. data/lib/verquest/configuration.rb +24 -6
  30. data/lib/verquest/gem_version.rb +1 -1
  31. data/lib/verquest/helper_methods/conditional_properties.rb +126 -0
  32. data/lib/verquest/helper_methods/unnamed_one_of.rb +35 -0
  33. data/lib/verquest/properties/array.rb +15 -1
  34. data/lib/verquest/properties/base.rb +104 -10
  35. data/lib/verquest/properties/collection.rb +63 -19
  36. data/lib/verquest/properties/const.rb +2 -1
  37. data/lib/verquest/properties/object.rb +38 -4
  38. data/lib/verquest/properties/one_of/discriminated_schema.rb +84 -0
  39. data/lib/verquest/properties/one_of.rb +87 -45
  40. data/lib/verquest/properties/reference.rb +61 -17
  41. data/lib/verquest/query/coercer.rb +165 -0
  42. data/lib/verquest/query/definition_check.rb +91 -0
  43. data/lib/verquest/query/parameters.rb +79 -0
  44. data/lib/verquest/query.rb +25 -0
  45. data/lib/verquest/supported_versions.rb +73 -0
  46. data/lib/verquest/transformer/collection.rb +32 -0
  47. data/lib/verquest/transformer/const_as_enum.rb +47 -0
  48. data/lib/verquest/transformer/layout.rb +83 -0
  49. data/lib/verquest/transformer/mapping_layout.rb +166 -0
  50. data/lib/verquest/transformer/one_of.rb +111 -0
  51. data/lib/verquest/transformer/path.rb +188 -0
  52. data/lib/verquest/transformer/plan.rb +183 -0
  53. data/lib/verquest/transformer/scope.rb +64 -0
  54. data/lib/verquest/transformer.rb +43 -880
  55. data/lib/verquest/version.rb +152 -79
  56. data/lib/verquest/versions.rb +7 -0
  57. data/lib/verquest.rb +7 -0
  58. metadata +44 -7
@@ -21,7 +21,9 @@ module Verquest
21
21
  # @return [Boolean] Whether this property is required
22
22
  # @!attribute [rw] map
23
23
  # @return [String, nil] The mapping path for this property
24
- attr_accessor :name, :required, :map
24
+ # @!attribute [rw] parameter_examples
25
+ # @return [Hash, nil] Named OpenAPI Example Objects for this property's query parameter, kept out of the JSON schemas
26
+ attr_accessor :name, :required, :map, :parameter_examples
25
27
 
26
28
  # Adds a child property to this property
27
29
  # @abstract
@@ -58,6 +60,54 @@ module Verquest
58
60
  raise NoMethodError
59
61
  end
60
62
 
63
+ # Adds this property to a transformer layout
64
+ #
65
+ # This is the layout of a scalar property: one source and target pair.
66
+ # Objects, collections, references and oneOfs override it.
67
+ #
68
+ # @param key_prefix [Array<String>] Prefix for the source key
69
+ # @param value_prefix [Array<String>] Prefix for the target value
70
+ # @param layout [Verquest::Transformer::Layout] The layout to add to
71
+ # @param version [String, nil] The version to create the layout for
72
+ # @return [void]
73
+ def layout(key_prefix:, value_prefix:, layout:, version:)
74
+ layout.add_pair((key_prefix + [name]).join("/"), mapping_value_key(value_prefix:))
75
+ end
76
+
77
+ # Returns the query parameter options under this property, by path
78
+ #
79
+ # Options are `explode:` for arrays and `examples:` for properties with
80
+ # parameter examples. Paths without options are left out.
81
+ #
82
+ # @param prefix [Array<String>] Path of the parent property
83
+ # @param version [String] The version to resolve references with
84
+ # @return [Hash{Array<String> => Hash}] The options of each property path
85
+ def query_options(prefix:, version:)
86
+ parameter_examples ? {prefix + [name] => {examples: parameter_examples}} : {}
87
+ end
88
+
89
+ # Returns a copy of this property under another name and map:
90
+ #
91
+ # Used by a reference to a single property, so the property maps as if
92
+ # it were declared where the reference is.
93
+ #
94
+ # @param name [String, Symbol] The new name
95
+ # @param map [String, nil] The new map: option
96
+ # @return [Verquest::Properties::Base] The renamed copy
97
+ def renamed(name:, map:)
98
+ dup.tap { |copy| copy.rename(name, map) }
99
+ end
100
+
101
+ protected
102
+
103
+ # @param name [String, Symbol] The new name
104
+ # @param map [String, nil] The new map: option
105
+ # @return [void]
106
+ def rename(name, map)
107
+ @name = name.to_s
108
+ @map = map
109
+ end
110
+
61
111
  private
62
112
 
63
113
  # @!attribute [r] nullable
@@ -75,10 +125,9 @@ module Verquest
75
125
 
76
126
  # Determines the mapping target key based on mapping configuration
77
127
  # @param value_prefix [Array<String>] Prefix for the target value
78
- # @param collection [Boolean] Whether this is a collection mapping
79
128
  # @return [String] The target mapping key
80
- def mapping_value_key(value_prefix:, collection: false)
81
- value_key = if map.nil?
129
+ def mapping_value_key(value_prefix:)
130
+ if map.nil?
82
131
  (value_prefix + [name]).join("/")
83
132
  elsif map == "/"
84
133
  ""
@@ -87,12 +136,6 @@ module Verquest
87
136
  else
88
137
  (value_prefix + map.split("/")).join("/")
89
138
  end
90
-
91
- if collection
92
- value_key + "[]"
93
- else
94
- value_key
95
- end
96
139
  end
97
140
 
98
141
  # Determines the mapping target value prefix based on mapping configuration
@@ -117,6 +160,57 @@ module Verquest
117
160
 
118
161
  value_prefix
119
162
  end
163
+
164
+ # Adds the mapping of a child property
165
+ #
166
+ # A oneOf gets its own mapping in the `_oneOfs` array, so its metadata
167
+ # can't clash with another oneOf in the same mapping.
168
+ #
169
+ # @param property [Verquest::Properties::Base] The child property
170
+ # @param mapping [Hash] The mapping hash to be updated
171
+ # @param options [Hash] The key_prefix, value_prefix and version for the child mapping
172
+ # @return [void]
173
+ def add_child_mapping(property, mapping:, **options)
174
+ return property.mapping(mapping:, **options) unless property.is_a?(OneOf)
175
+
176
+ one_of_mapping = {}
177
+ property.mapping(mapping: one_of_mapping, **options)
178
+ (mapping["_oneOfs"] ||= []) << one_of_mapping
179
+ end
180
+
181
+ # Adds the mappings of a referenced request's properties under the given prefixes
182
+ #
183
+ # Each property maps itself with the prefixes, instead of the finished
184
+ # mapping of the request being copied, so its oneOfs keep their
185
+ # metadata and land in `_oneOfs`.
186
+ #
187
+ # @param request [Class<Verquest::Base>] The referenced request
188
+ # @param key_prefix [Array<String>] Prefix for the source keys
189
+ # @param value_prefix [Array<String>] Prefix for the target values
190
+ # @param mapping [Hash] The mapping hash to be updated
191
+ # @param version [String] The version to create mapping for
192
+ # @return [void]
193
+ def add_request_mapping(request, key_prefix:, value_prefix:, mapping:, version:)
194
+ referenced = request.resolve(version)
195
+ referenced.all_properties.each_value do |property|
196
+ add_child_mapping(property, key_prefix:, value_prefix:, mapping:, version: referenced.name)
197
+ end
198
+ end
199
+
200
+ # Adds a referenced request's properties to a layout under the given prefixes
201
+ #
202
+ # @param request [Class<Verquest::Base>] The referenced request
203
+ # @param key_prefix [Array<String>] Prefix for the source keys
204
+ # @param value_prefix [Array<String>] Prefix for the target values
205
+ # @param layout [Verquest::Transformer::Layout] The layout to add to
206
+ # @param version [String] The version to create the layout for
207
+ # @return [void]
208
+ def add_request_layout(request, key_prefix:, value_prefix:, layout:, version:)
209
+ referenced = request.resolve(version)
210
+ referenced.all_properties.each_value do |property|
211
+ property.layout(key_prefix:, value_prefix:, layout:, version: referenced.name)
212
+ end
213
+ end
120
214
  end
121
215
  end
122
216
  end
@@ -18,6 +18,9 @@ module Verquest
18
18
  # item: ProductRequest
19
19
  # )
20
20
  class Collection < Base
21
+ include HelperMethods::ConditionalProperties
22
+ include HelperMethods::UnnamedOneOf
23
+
21
24
  # Initialize a new Collection property
22
25
  #
23
26
  # @param name [String, Symbol] The name of the property
@@ -31,6 +34,7 @@ module Verquest
31
34
  raise ArgumentError, "You can not map collection to the root" if map == "/"
32
35
 
33
36
  @properties = {}
37
+ @conditions = []
34
38
 
35
39
  @name = name.to_s
36
40
  @item = item
@@ -51,9 +55,21 @@ module Verquest
51
55
  # @param property [Verquest::Properties::Base] The property to add to the collection items
52
56
  # @return [Verquest::Properties::Base] The added property
53
57
  def add(property)
58
+ check_unnamed_one_of(property)
54
59
  properties[property.name] = property
55
60
  end
56
61
 
62
+ # Add a condition to this collection's item definition
63
+ #
64
+ # @param condition [Verquest::Conditions::Condition] The condition to add
65
+ # @return [Array<Verquest::Conditions::Condition>] The conditions
66
+ # @raise [ArgumentError] If the collection items are defined by `item:`
67
+ def add_condition(condition)
68
+ raise ArgumentError, "condition can not be declared in a collection defined by item:" if has_item?
69
+
70
+ super
71
+ end
72
+
57
73
  # Check if this collection references an external item schema
58
74
  #
59
75
  # @return [Boolean] True if the collection uses an external reference
@@ -61,18 +77,21 @@ module Verquest
61
77
  !item.nil?
62
78
  end
63
79
 
64
- # Check if this collection contains a oneOf property as its item type
80
+ # Check if the items are a oneOf: an unnamed oneOf is the collection's only property
65
81
  #
66
- # @return [Boolean] True if the collection contains a oneOf property
82
+ # @return [Boolean] True if the item schema is the oneOf itself
67
83
  def has_one_of?
68
- properties.values.size == 1 && properties.values.first.is_a?(Verquest::Properties::OneOf)
84
+ # A named oneOf is a property of the item, like any other
85
+ properties.values.size == 1 && properties.values.first.is_a?(Verquest::Properties::OneOf) && properties.values.first.name.nil?
69
86
  end
70
87
 
71
88
  # Returns the oneOf property if this collection contains one
72
89
  #
73
90
  # @return [Verquest::Properties::OneOf, nil] The oneOf property or nil
91
+ # @raise [ArgumentError] If the collection also has conditions
74
92
  def one_of_property
75
93
  return nil unless has_one_of?
94
+ raise ArgumentError, "Conditions can not be declared next to a one_of in a collection" if conditions.any?
76
95
 
77
96
  properties.values.first
78
97
  end
@@ -104,10 +123,11 @@ module Verquest
104
123
  "items" => {
105
124
  "type" => "object",
106
125
  "required" => required_properties,
107
- "properties" => properties.transform_values { |property| property.to_schema[property.name] },
126
+ "properties" => all_properties.transform_values { |property| property.to_schema[property.name] },
108
127
  "additionalProperties" => Verquest.configuration.default_additional_properties
109
128
  }.tap do |schema|
110
129
  schema["dependentRequired"] = dependent_required_properties if dependent_required_properties.any?
130
+ add_conditions(schema)
111
131
  end
112
132
  }.merge(schema_options)
113
133
  }
@@ -141,10 +161,11 @@ module Verquest
141
161
  "items" => {
142
162
  "type" => "object",
143
163
  "required" => required_properties,
144
- "properties" => properties.transform_values { |property| property.to_validation_schema(version: version)[property.name] },
164
+ "properties" => all_properties.transform_values { |property| property.to_validation_schema(version: version)[property.name] },
145
165
  "additionalProperties" => Verquest.configuration.default_additional_properties
146
166
  }.tap do |schema|
147
167
  schema["dependentRequired"] = dependent_required_properties if dependent_required_properties.any?
168
+ add_conditions(schema)
148
169
  end
149
170
  }.merge(schema_options)
150
171
  }
@@ -173,28 +194,51 @@ module Verquest
173
194
  # @param version [String, nil] The version to create mapping for
174
195
  # @return [void]
175
196
  def mapping(key_prefix:, value_prefix:, mapping:, version:)
176
- if has_item?
177
- value_key_prefix = mapping_value_key(value_prefix: value_prefix, collection: true)
178
-
179
- reference_mapping = item.mapping(version:).dup
180
- reference_mapping.transform_keys! { |k| "#{(key_prefix + [name]).join("/")}[]/#{k}" }
181
- reference_mapping.transform_values! { |v| "#{value_key_prefix}/#{v}" }
182
-
183
- mapping.merge!(reference_mapping)
184
- elsif has_one_of?
185
- one_of_mapping = {}
186
- one_of_property.mapping(key_prefix: key_prefix + ["#{name}[]"], value_prefix: mapping_value_prefix(value_prefix: value_prefix, collection: true), mapping: one_of_mapping, version:)
187
- mapping.merge!(one_of_mapping)
197
+ if schema_only_item?
198
+ mapping[(key_prefix + [name]).join("/")] = mapping_value_key(value_prefix:)
199
+ elsif has_item?
200
+ add_request_mapping(item, key_prefix: key_prefix + ["#{name}[]"], value_prefix: mapping_value_prefix(value_prefix: value_prefix, collection: true), mapping:, version:)
188
201
  else
189
- properties.values.each do |property|
190
- property.mapping(key_prefix: key_prefix + ["#{name}[]"], value_prefix: mapping_value_prefix(value_prefix: value_prefix, collection: true), mapping:, version:)
202
+ children = has_one_of? ? [one_of_property] : all_properties.values
203
+ children.each do |property|
204
+ add_child_mapping(property, key_prefix: key_prefix + ["#{name}[]"], value_prefix: mapping_value_prefix(value_prefix: value_prefix, collection: true), mapping:, version:)
191
205
  end
192
206
  end
193
207
  end
194
208
 
209
+ # Adds this collection and its item properties to a transformer layout
210
+ #
211
+ # The collection's source and target paths are added too, so the plan
212
+ # knows where the items go when they hold a oneOf.
213
+ #
214
+ # @param key_prefix [Array<String>] Prefix for the source key
215
+ # @param value_prefix [Array<String>] Prefix for the target value
216
+ # @param layout [Verquest::Transformer::Layout] The layout to add to
217
+ # @param version [String, nil] The version to create the layout for
218
+ # @return [void]
219
+ def layout(key_prefix:, value_prefix:, layout:, version:)
220
+ return super if schema_only_item?
221
+
222
+ item_key_prefix = key_prefix + ["#{name}[]"]
223
+ item_value_prefix = mapping_value_prefix(value_prefix:, collection: true)
224
+ layout.add_collection(item_key_prefix.join("/"), item_value_prefix.join("/"))
225
+ return add_request_layout(item, key_prefix: item_key_prefix, value_prefix: item_value_prefix, layout:, version:) if has_item?
226
+
227
+ (has_one_of? ? [one_of_property] : all_properties.values).each do |property|
228
+ property.layout(key_prefix: item_key_prefix, value_prefix: item_value_prefix, layout:, version:)
229
+ end
230
+ end
231
+
195
232
  private
196
233
 
197
234
  attr_reader :item, :schema_options, :properties, :type
235
+
236
+ # Whether item only provides a schema (not a Verquest::Base subclass), so items pass through unchanged
237
+ #
238
+ # @return [Boolean]
239
+ def schema_only_item?
240
+ has_item? && !(item.is_a?(Class) && item <= Verquest::Base)
241
+ end
198
242
  end
199
243
  end
200
244
  end
@@ -30,7 +30,8 @@ module Verquest
30
30
  # @return [Hash] The schema definition for this constant
31
31
  def to_schema
32
32
  {
33
- name => nullable_schema({"const" => value}.merge(schema_options))
33
+ # Draft 4 and OpenAPI 3.0 have no const, but a one-value enum means the same
34
+ name => nullable_schema((Verquest.configuration.const_keyword? ? {"const" => value} : {"enum" => [value]}).merge(schema_options))
34
35
  }
35
36
  end
36
37
 
@@ -12,6 +12,9 @@ module Verquest
12
12
  # address.add(Verquest::Properties::Field.new(name: :street, type: :string))
13
13
  # address.add(Verquest::Properties::Field.new(name: :city, type: :string, required: true))
14
14
  class Object < Base
15
+ include HelperMethods::ConditionalProperties
16
+ include HelperMethods::UnnamedOneOf
17
+
15
18
  # Initialize a new Object property
16
19
  #
17
20
  # @param name [String, Symbol] The name of the property
@@ -21,6 +24,7 @@ module Verquest
21
24
  # @param schema_options [Hash] Additional JSON schema options for this property
22
25
  def initialize(name:, required: false, nullable: false, map: nil, **schema_options)
23
26
  @properties = {}
27
+ @conditions = []
24
28
 
25
29
  @name = name.to_s
26
30
  @required = required
@@ -44,6 +48,7 @@ module Verquest
44
48
  # @param property [Verquest::Properties::Base] The property to add to this object
45
49
  # @return [Verquest::Properties::Base] The added property
46
50
  def add(property)
51
+ check_unnamed_one_of(property)
47
52
  properties[property.name.to_s] = property
48
53
  end
49
54
 
@@ -55,9 +60,10 @@ module Verquest
55
60
  name => {
56
61
  "type" => type,
57
62
  "required" => required_properties,
58
- "properties" => properties.transform_values { |property| property.to_schema[property.name] }
63
+ "properties" => all_properties.transform_values { |property| property.to_schema[property.name] }
59
64
  }.merge(schema_options).tap do |schema|
60
65
  schema["dependentRequired"] = dependent_required_properties if dependent_required_properties.any?
66
+ add_conditions(schema)
61
67
  end
62
68
  }
63
69
  end
@@ -71,23 +77,51 @@ module Verquest
71
77
  name => {
72
78
  "type" => type,
73
79
  "required" => required_properties,
74
- "properties" => properties.transform_values { |property| property.to_validation_schema(version: version)[property.name] }
80
+ "properties" => all_properties.transform_values { |property| property.to_validation_schema(version: version)[property.name] }
75
81
  }.merge(schema_options).tap do |schema|
76
82
  schema["dependentRequired"] = dependent_required_properties if dependent_required_properties.any?
83
+ add_conditions(schema)
77
84
  end
78
85
  }
79
86
  end
80
87
 
81
88
  # Create mapping for this object property and all its children
82
89
  #
90
+ # Each nested oneOf gets its own mapping in the `_oneOfs` array, so its
91
+ # metadata can't clash with another oneOf.
92
+ #
83
93
  # @param key_prefix [Array<String>] Prefix for the source key
84
94
  # @param value_prefix [Array<String>] Prefix for the target value
85
95
  # @param mapping [Hash] The mapping hash to be updated
86
96
  # @param version [String, nil] The version to create mapping for
87
97
  # @return [void]
88
98
  def mapping(key_prefix:, value_prefix:, mapping:, version: nil)
89
- properties.values.each do |property|
90
- property.mapping(key_prefix: key_prefix + [name], value_prefix: mapping_value_prefix(value_prefix:), mapping:, version:)
99
+ all_properties.values.each do |property|
100
+ add_child_mapping(property, key_prefix: key_prefix + [name], value_prefix: mapping_value_prefix(value_prefix:), mapping:, version:)
101
+ end
102
+ end
103
+
104
+ # Adds this object's properties to a transformer layout
105
+ #
106
+ # @param key_prefix [Array<String>] Prefix for the source key
107
+ # @param value_prefix [Array<String>] Prefix for the target value
108
+ # @param layout [Verquest::Transformer::Layout] The layout to add to
109
+ # @param version [String, nil] The version to create the layout for
110
+ # @return [void]
111
+ def layout(key_prefix:, value_prefix:, layout:, version:)
112
+ all_properties.each_value do |property|
113
+ property.layout(key_prefix: key_prefix + [name], value_prefix: mapping_value_prefix(value_prefix:), layout:, version:)
114
+ end
115
+ end
116
+
117
+ # Returns the query parameter options of this object and its properties
118
+ #
119
+ # @param prefix [Array<String>] Path of the parent property
120
+ # @param version [String] The version to resolve references with
121
+ # @return [Hash{Array<String> => Hash}] The options of each property path
122
+ def query_options(prefix:, version:)
123
+ all_properties.each_value.with_object(super) do |property, options|
124
+ options.merge!(property.query_options(prefix: prefix + [name], version:))
91
125
  end
92
126
  end
93
127
 
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Verquest
4
+ module Properties
5
+ class OneOf < Base
6
+ # Builds a self-contained oneOf validation schema that keeps the discriminator
7
+ #
8
+ # OpenAPI validators (json_schemer's OpenAPI 3.0 and 3.1 dialects) select the
9
+ # discriminated variant through each oneOf entry's `$ref`, so the variants move
10
+ # into local definitions. The content-based identifier makes those `$ref`s resolve
11
+ # against this schema wherever it is embedded, and identical content always gets
12
+ # the same identifier. Other dialects ignore the discriminator and validate every variant.
13
+ #
14
+ # The identifier is set as both `$id` (draft 6 and later, OpenAPI 3.1) and `id`
15
+ # (draft 4, OpenAPI 3.0), and the variants live under `definitions`, which every
16
+ # dialect can reference. The output is the same whatever `json_schema_version` is.
17
+ #
18
+ # @example
19
+ # DiscriminatedSchema.new(definitions: {"dog" => dog_schema, "cat" => cat_schema}, discriminator: "type").to_h
20
+ # # => {
21
+ # # "$id" => "https://verquest.local/one_of/3f1c9a0b2d4e5f67",
22
+ # # "id" => "https://verquest.local/one_of/3f1c9a0b2d4e5f67",
23
+ # # "definitions" => {"dog" => dog_schema, "cat" => cat_schema},
24
+ # # "oneOf" => [{"$ref" => "#/definitions/dog"}, {"$ref" => "#/definitions/cat"}],
25
+ # # "discriminator" => {"propertyName" => "type", "mapping" => {"dog" => "#/definitions/dog", "cat" => "#/definitions/cat"}}
26
+ # # }
27
+ class DiscriminatedSchema
28
+ # Base URI for the schema identifier
29
+ ID_PREFIX = "https://verquest.local/one_of/"
30
+
31
+ # Initialize a new DiscriminatedSchema
32
+ #
33
+ # @param definitions [Hash<String, Hash>] Variant name (discriminator value) => inline validation schema
34
+ # @param discriminator [String] The discriminator property name
35
+ def initialize(definitions:, discriminator:)
36
+ @definitions = definitions
37
+ @discriminator = discriminator
38
+ end
39
+
40
+ # Builds the validation schema
41
+ #
42
+ # @return [Hash] Schema with identifiers, definitions, oneOf refs, and discriminator
43
+ def to_h
44
+ {"$id" => id, "id" => id}.merge(body)
45
+ end
46
+
47
+ private
48
+
49
+ # @!attribute [r] definitions
50
+ # @return [Hash<String, Hash>] Variant name => inline validation schema
51
+ # @!attribute [r] discriminator
52
+ # @return [String] The discriminator property name
53
+ attr_reader :definitions, :discriminator
54
+
55
+ # The schema without the identifiers
56
+ #
57
+ # @return [Hash] Definitions, oneOf refs, and discriminator
58
+ def body
59
+ @body ||= {
60
+ "definitions" => definitions,
61
+ "oneOf" => refs.values.map { |ref| {"$ref" => ref} },
62
+ "discriminator" => {"propertyName" => discriminator, "mapping" => refs}
63
+ }
64
+ end
65
+
66
+ # Content-based identifier, so identical schemas share it
67
+ #
68
+ # `allow_nan` keeps schemas with infinite or NaN numbers (e.g. `maximum: Float::INFINITY`) hashable.
69
+ #
70
+ # @return [String] The identifier URI
71
+ def id
72
+ @id ||= "#{ID_PREFIX}#{Digest::SHA256.hexdigest(JSON.generate(body, allow_nan: true))[0, 16]}"
73
+ end
74
+
75
+ # Local `$ref` for each variant, keyed by variant name
76
+ #
77
+ # @return [Hash<String, String>] Variant name => `$ref` string
78
+ def refs
79
+ @refs ||= definitions.keys.to_h { |name| [name, "#/definitions/#{name}"] }
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end