verquest 0.7.1 → 0.9.0

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.
@@ -46,7 +46,10 @@ module Verquest
46
46
  #
47
47
  # @!attribute [r] location
48
48
  # @return [Symbol] Where the params come from, `:body`, `:query` or `:path`
49
- attr_reader :name, :properties, :schema, :validation_schema, :mapping, :transformer, :external_mapping, :location
49
+ #
50
+ # @!attribute [r] filters
51
+ # @return [Hash{String => Proc}] Filter blocks by property name, in declaration order
52
+ attr_reader :name, :properties, :filters, :schema, :validation_schema, :mapping, :transformer, :external_mapping, :location
50
53
 
51
54
  # @!attribute [rw] schema_options
52
55
  # @return [Hash] Additional JSON schema options for this version
@@ -65,6 +68,7 @@ module Verquest
65
68
  @location = location
66
69
  @schema_options = {}
67
70
  @properties = {}
71
+ @filters = {}
68
72
  @conditions = []
69
73
  # Filled after the version is frozen, so it is created here; freeze is shallow
70
74
  @validation_schemers = {}
@@ -73,19 +77,33 @@ module Verquest
73
77
 
74
78
  # Add a property to this version
75
79
  #
80
+ # A property added under the name of a filter replaces the filter too, so a plain property over an
81
+ # inherited filter stops filtering.
82
+ #
76
83
  # @param property [Verquest::Properties::Base] The property to add
77
84
  # @return [Verquest::Properties::Base] The added property
78
85
  def add(property)
79
86
  check_unnamed_one_of(property)
87
+ filters.delete(property.name)
80
88
  properties[property.name] = property
81
89
  end
82
90
 
83
- # Remove a property from this version by name
91
+ # Store the block that filters by a property's value
92
+ #
93
+ # @param name [Symbol, String] The name of the property the filter reads
94
+ # @param block [Proc] Called with the target and the property's processed value, returns the filtered target
95
+ # @return [Proc] The stored block
96
+ def add_filter(name, block)
97
+ filters[name.to_s] = block
98
+ end
99
+
100
+ # Remove a property, and its filter, from this version by name
84
101
  #
85
102
  # @param property_name [Symbol, String] The name of the property to remove
86
103
  # @return [Verquest::Properties::Base] The removed property
87
104
  # @raise [PropertyNotFoundError] If the property doesn't exist
88
105
  def remove(property_name)
106
+ filters.delete(property_name.to_s)
89
107
  properties.delete(property_name.to_s) || raise(PropertyNotFoundError.new("Property '#{property_name}' is not defined on '#{name}'"))
90
108
  end
91
109
 
@@ -120,6 +138,7 @@ module Verquest
120
138
  next if exclude_properties.include?(property.name.to_sym)
121
139
 
122
140
  add(property)
141
+ add_filter(property.name, version.filters[property.name]) if version.filters.key?(property.name)
123
142
  end
124
143
 
125
144
  version.conditions.each { |condition| add_condition(condition) }
@@ -151,16 +170,21 @@ module Verquest
151
170
  end
152
171
 
153
172
  prepare_parameters if parameters?
173
+ @property_defaults = property_defaults?(validation_schema)
154
174
  @transformer = Transformer.new(layout: transformer_layout)
175
+ @filters.freeze
155
176
  freeze
156
177
  end
157
178
 
158
179
  # Validate the schema against the metaschema
159
180
  #
181
+ # Under `:openapi30` the document (`schema`) is checked, since the validation schema keeps JSON Schema forms
182
+ # OpenAPI 3.0 forbids, such as "null" types.
183
+ #
160
184
  # @return [Boolean] true if the schema is valid, false otherwise
161
185
  def valid_schema?
162
186
  JSONSchemer.valid_schema?(
163
- validation_schema,
187
+ checked_schema,
164
188
  meta_schema: Verquest.configuration.json_schema_uri
165
189
  )
166
190
  end
@@ -169,13 +193,14 @@ module Verquest
169
193
  #
170
194
  # This method validates the schema against the configured JSON Schema metaschema
171
195
  # and returns detailed validation errors if any are found. It uses the JSONSchemer
172
- # library with the schema version specified in the configuration.
196
+ # library with the schema version specified in the configuration. Under `:openapi30` it checks the document,
197
+ # so the pointers refer to `schema`.
173
198
  #
174
199
  # @return [Array<Hash>] An array of validation error details, empty if schema is valid
175
200
  # @see #valid_schema?
176
201
  def validate_schema
177
202
  JSONSchemer.validate_schema(
178
- validation_schema,
203
+ checked_schema,
179
204
  meta_schema: Verquest.configuration.json_schema_uri
180
205
  ).map do |error|
181
206
  {
@@ -189,6 +214,8 @@ module Verquest
189
214
 
190
215
  # Validate request parameters against the version's validation schema
191
216
  #
217
+ # Inserts missing defaults into params when {#inserts_property_defaults?} is true.
218
+ #
192
219
  # @param params [Hash] The request parameters to validate
193
220
  # @return [Array<Hash>] An array of validation error details, or empty if valid
194
221
  def validate_params(params:)
@@ -202,6 +229,13 @@ module Verquest
202
229
  end
203
230
  end
204
231
 
232
+ # Check if validating params inserts the defaults the schema declares
233
+ #
234
+ # @return [Boolean] true when the schema declares a default and the configuration inserts defaults
235
+ def inserts_property_defaults?
236
+ !!insert_property_defaults_setting
237
+ end
238
+
205
239
  # Get the mapping for a specific property
206
240
  #
207
241
  # @param property [Symbol, String] The property name (regular or declared in a condition branch)
@@ -239,11 +273,11 @@ module Verquest
239
273
  parameters? ? query_coercer.call(params) : params
240
274
  end
241
275
 
242
- # Returns OpenAPI parameter objects for this query or path version
276
+ # Returns OpenAPI parameter objects for this query or path version, in OpenAPI 3.0 forms under :openapi30
243
277
  #
244
278
  # @return [Array<Hash>] A new array of parameter objects on each call
245
279
  def parameters
246
- Query::Parameters.new(schema: validation_schema, options: query_options, location:).call
280
+ Query::Parameters.new(schema: document_schema(validation_schema), options: query_options, location:).call
247
281
  end
248
282
 
249
283
  # Map request parameters to internal representation using the transformer
@@ -318,19 +352,26 @@ module Verquest
318
352
  # Returns the compiled validation schema for the current configuration
319
353
  #
320
354
  # Compiling is much slower than validating, so each configuration is
321
- # compiled once and reused. The lock keeps threads from compiling at the
322
- # same time.
355
+ # compiled once and reused. Default insertion is turned off when the schema
356
+ # declares no default, since json_schemer would walk every result for
357
+ # nothing. The lock keeps threads from compiling at the same time.
323
358
  #
324
359
  # @return [JSONSchemer::Schema] The compiled validation schema
325
360
  def validation_schemer
326
- configuration = Verquest.configuration
327
- key = [configuration.json_schema_uri, configuration.insert_property_defaults]
361
+ key = [Verquest.configuration.json_schema_uri, insert_property_defaults_setting]
328
362
 
329
363
  @validation_schemers_lock.synchronize do
330
364
  @validation_schemers[key] ||= JSONSchemer.schema(validation_schema, meta_schema: key[0], insert_property_defaults: key[1])
331
365
  end
332
366
  end
333
367
 
368
+ # Returns the insert_property_defaults value to compile with
369
+ #
370
+ # @return [Boolean, Symbol] The configured value when the schema declares a default, false otherwise
371
+ def insert_property_defaults_setting
372
+ @property_defaults ? Verquest.configuration.insert_property_defaults : false
373
+ end
374
+
334
375
  # Generates the JSON schema for this version
335
376
  #
336
377
  # Creates a schema object with type, description, required properties,
@@ -341,13 +382,14 @@ module Verquest
341
382
  def prepare_schema
342
383
  @schema = {
343
384
  "type" => "object",
344
- "required" => required_properties,
385
+ **required_keyword,
345
386
  "properties" => all_properties.transform_values { |property| property.to_schema[property.name] }
346
387
  }.merge(schema_options).tap do |schema|
347
388
  schema["dependentRequired"] = dependent_required_properties if dependent_required_properties.any?
348
389
  schema["description"] = description if description
349
390
  add_conditions(schema, documentation: true)
350
- end.freeze
391
+ end
392
+ @schema = document_schema(@schema).freeze
351
393
  end
352
394
 
353
395
  # Generates the JSON schema for combination schemas (oneOf at root level)
@@ -360,7 +402,20 @@ module Verquest
360
402
  def prepare_combination_schema
361
403
  raise ArgumentError, "Conditions can not be declared next to a root-level one_of" if conditions.any?
362
404
 
363
- @schema = properties.values.first.to_schema
405
+ @schema = document_schema(properties.values.first.to_schema)
406
+ end
407
+
408
+ # @return [Hash] The schema the meta-schema checks: the document under OpenAPI 3.0, the validation schema otherwise
409
+ def checked_schema
410
+ Verquest.configuration.nullable_keyword? ? schema : validation_schema
411
+ end
412
+
413
+ # Converts a document to OpenAPI 3.0 forms when the configured version needs them
414
+ #
415
+ # @param schema [Hash] The schema in JSON Schema forms
416
+ # @return [Hash] The schema for documents
417
+ def document_schema(schema)
418
+ Verquest.configuration.nullable_keyword? ? Openapi30.document(schema) : schema
364
419
  end
365
420
 
366
421
  # Generates the validation schema for this version
@@ -372,7 +427,7 @@ module Verquest
372
427
  def prepare_validation_schema
373
428
  @validation_schema = {
374
429
  "type" => "object",
375
- "required" => required_properties,
430
+ **required_keyword,
376
431
  "properties" => all_properties.transform_values { |property| property.to_validation_schema(version: name)[property.name] }
377
432
  }.merge(schema_options).tap do |schema|
378
433
  schema["dependentRequired"] = dependent_required_properties if dependent_required_properties.any?
@@ -391,6 +446,20 @@ module Verquest
391
446
  @validation_schema = properties.values.first.to_validation_schema(version: name)
392
447
  end
393
448
 
449
+ # Checks whether a schema declares a default anywhere
450
+ #
451
+ # A property named `default` also counts, which only keeps default insertion on.
452
+ #
453
+ # @param schema [Object] The schema or a part of it
454
+ # @return [Boolean] true if any Hash in it has a "default" key
455
+ def property_defaults?(schema)
456
+ case schema
457
+ when Hash then schema.key?("default") || schema.each_value.any? { |value| property_defaults?(value) }
458
+ when Array then schema.any? { |value| property_defaults?(value) }
459
+ else false
460
+ end
461
+ end
462
+
394
463
  # Prepares the parameter mapping for this version
395
464
  #
396
465
  # Collects mappings from all properties in this version and checks for
@@ -40,6 +40,7 @@ module Verquest
40
40
  def add(version)
41
41
  raise ArgumentError, "Expected a Verquest::Version instance" unless version.is_a?(Verquest::Version)
42
42
 
43
+ @property_names = nil
43
44
  versions[version.name] = version
44
45
  end
45
46
 
@@ -63,6 +64,15 @@ module Verquest
63
64
  versions.keys.sort
64
65
  end
65
66
 
67
+ # Returns the root property names declared by any version
68
+ #
69
+ # Cached until the next version is added.
70
+ #
71
+ # @return [Array<String>] The property names
72
+ def property_names
73
+ @property_names ||= versions.values.flat_map { |version| version.all_properties.keys }.uniq.freeze
74
+ end
75
+
66
76
  private
67
77
 
68
78
  # @!attribute [r] versions
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: verquest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Petr Hlavicka
@@ -85,6 +85,7 @@ files:
85
85
  - lib/verquest/helper_methods/conditional_properties.rb
86
86
  - lib/verquest/helper_methods/required_properties.rb
87
87
  - lib/verquest/helper_methods/unnamed_one_of.rb
88
+ - lib/verquest/openapi30.rb
88
89
  - lib/verquest/properties.rb
89
90
  - lib/verquest/properties/array.rb
90
91
  - lib/verquest/properties/base.rb
@@ -121,6 +122,7 @@ metadata:
121
122
  homepage_uri: https://github.com/verquest/verquest
122
123
  source_code_uri: https://github.com/verquest/verquest
123
124
  changelog_uri: https://github.com/verquest/verquest/blob/main/CHANGELOG.md
125
+ rubygems_mfa_required: 'true'
124
126
  rdoc_options: []
125
127
  require_paths:
126
128
  - lib