verquest 0.8.0 → 0.10.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +21 -0
- data/README.md +128 -47
- data/Rakefile +2 -1
- data/docs/rails.md +14 -2
- data/lib/verquest/base/private_class_methods.rb +166 -6
- data/lib/verquest/base/public_class_methods.rb +64 -28
- data/lib/verquest/configuration.rb +11 -1
- data/lib/verquest/correction.rb +40 -0
- data/lib/verquest/gem_version.rb +1 -1
- data/lib/verquest/query/coercer.rb +14 -9
- data/lib/verquest/query/lenient_coercer.rb +138 -0
- data/lib/verquest/version.rb +93 -10
- data/lib/verquest/versions.rb +10 -0
- metadata +4 -1
data/lib/verquest/version.rb
CHANGED
|
@@ -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
|
-
|
|
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
|
-
#
|
|
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,7 +170,9 @@ 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
|
|
|
@@ -193,6 +214,8 @@ module Verquest
|
|
|
193
214
|
|
|
194
215
|
# Validate request parameters against the version's validation schema
|
|
195
216
|
#
|
|
217
|
+
# Inserts missing defaults into params when {#inserts_property_defaults?} is true.
|
|
218
|
+
#
|
|
196
219
|
# @param params [Hash] The request parameters to validate
|
|
197
220
|
# @return [Array<Hash>] An array of validation error details, or empty if valid
|
|
198
221
|
def validate_params(params:)
|
|
@@ -206,6 +229,13 @@ module Verquest
|
|
|
206
229
|
end
|
|
207
230
|
end
|
|
208
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
|
+
|
|
209
239
|
# Get the mapping for a specific property
|
|
210
240
|
#
|
|
211
241
|
# @param property [Symbol, String] The property name (regular or declared in a condition branch)
|
|
@@ -238,9 +268,26 @@ module Verquest
|
|
|
238
268
|
# Converts query or path strings to the types the validation schema expects
|
|
239
269
|
#
|
|
240
270
|
# @param params [Hash] The parsed request parameters
|
|
271
|
+
# @param lenient [Boolean] Whether to correct values a lenient request tolerates (see {Query::LenientCoercer})
|
|
272
|
+
# @yieldparam correction [Verquest::Correction] Each correction, when lenient
|
|
241
273
|
# @return [Hash] Converted params for a query or path version, the same params otherwise
|
|
242
|
-
def coerce_params(params)
|
|
243
|
-
|
|
274
|
+
def coerce_params(params, lenient: false, &on_correction)
|
|
275
|
+
return params unless parameters?
|
|
276
|
+
return query_coercer.call(params) unless lenient
|
|
277
|
+
|
|
278
|
+
Query::LenientCoercer.new(schema: validation_schema, explodes: query_explodes, required: required_property_names, on_correction:).call(params)
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
# Returns the root properties the validation schema requires, always (`required`), when another
|
|
282
|
+
# property is present (`dependentRequired`) or in a condition's branch (`then`/`else`, also in `allOf`,
|
|
283
|
+
# and any alternative of `anyOf` or `oneOf`)
|
|
284
|
+
#
|
|
285
|
+
# The list is conservative: a name stays in it even when its trigger is absent or its condition
|
|
286
|
+
# doesn't apply, so a lenient request rejects `{"per" => "0"}` instead of removing `per`.
|
|
287
|
+
#
|
|
288
|
+
# @return [Array<String>] The property names
|
|
289
|
+
def required_property_names
|
|
290
|
+
required_names(validation_schema)
|
|
244
291
|
end
|
|
245
292
|
|
|
246
293
|
# Returns OpenAPI parameter objects for this query or path version, in OpenAPI 3.0 forms under :openapi30
|
|
@@ -299,9 +346,24 @@ module Verquest
|
|
|
299
346
|
|
|
300
347
|
private
|
|
301
348
|
|
|
349
|
+
# Collects names from `then`/`else` branches and `allOf`, `anyOf` and `oneOf` entries, conservatively
|
|
350
|
+
# (a name any alternative requires counts); `if` and `not` don't require anything
|
|
351
|
+
#
|
|
352
|
+
# @param schema [Hash] An object schema, a condition's branch or an entry of one of them
|
|
353
|
+
# @return [Array<String>] The names the schema, its branches and its entries require
|
|
354
|
+
def required_names(schema)
|
|
355
|
+
names = schema.fetch("required", []) | schema.fetch("dependentRequired", {}).values.flatten
|
|
356
|
+
branches = schema.values_at("then", "else").compact + schema.values_at("allOf", "anyOf", "oneOf").compact.flatten
|
|
357
|
+
|
|
358
|
+
branches.reduce(names) { |all, branch| all | required_names(branch) }
|
|
359
|
+
end
|
|
360
|
+
|
|
302
361
|
# @return [Verquest::Query::Coercer, nil] The coercer of a query or path version
|
|
303
362
|
attr_reader :query_coercer
|
|
304
363
|
|
|
364
|
+
# @return [Hash{Array<String> => Boolean}, nil] The explode value of each array property of a query or path version, by path
|
|
365
|
+
attr_reader :query_explodes
|
|
366
|
+
|
|
305
367
|
# @return [Hash{Array<String> => Hash}, nil] The parameter options of each property path of a query or path version
|
|
306
368
|
attr_reader :query_options
|
|
307
369
|
|
|
@@ -315,26 +377,33 @@ module Verquest
|
|
|
315
377
|
@query_options = all_properties.each_value.with_object({}) do |property, options|
|
|
316
378
|
options.merge!(property.query_options(prefix: [], version: name))
|
|
317
379
|
end.freeze
|
|
318
|
-
|
|
319
|
-
@query_coercer = Query::Coercer.new(schema: validation_schema, explodes:)
|
|
380
|
+
@query_explodes = query_options.filter_map { |path, options| [path, options[:explode]] if options.key?(:explode) }.to_h.freeze
|
|
381
|
+
@query_coercer = Query::Coercer.new(schema: validation_schema, explodes: @query_explodes)
|
|
320
382
|
end
|
|
321
383
|
|
|
322
384
|
# Returns the compiled validation schema for the current configuration
|
|
323
385
|
#
|
|
324
386
|
# Compiling is much slower than validating, so each configuration is
|
|
325
|
-
# compiled once and reused.
|
|
326
|
-
#
|
|
387
|
+
# compiled once and reused. Default insertion is turned off when the schema
|
|
388
|
+
# declares no default, since json_schemer would walk every result for
|
|
389
|
+
# nothing. The lock keeps threads from compiling at the same time.
|
|
327
390
|
#
|
|
328
391
|
# @return [JSONSchemer::Schema] The compiled validation schema
|
|
329
392
|
def validation_schemer
|
|
330
|
-
|
|
331
|
-
key = [configuration.json_schema_uri, configuration.insert_property_defaults]
|
|
393
|
+
key = [Verquest.configuration.json_schema_uri, insert_property_defaults_setting]
|
|
332
394
|
|
|
333
395
|
@validation_schemers_lock.synchronize do
|
|
334
396
|
@validation_schemers[key] ||= JSONSchemer.schema(validation_schema, meta_schema: key[0], insert_property_defaults: key[1])
|
|
335
397
|
end
|
|
336
398
|
end
|
|
337
399
|
|
|
400
|
+
# Returns the insert_property_defaults value to compile with
|
|
401
|
+
#
|
|
402
|
+
# @return [Boolean, Symbol] The configured value when the schema declares a default, false otherwise
|
|
403
|
+
def insert_property_defaults_setting
|
|
404
|
+
@property_defaults ? Verquest.configuration.insert_property_defaults : false
|
|
405
|
+
end
|
|
406
|
+
|
|
338
407
|
# Generates the JSON schema for this version
|
|
339
408
|
#
|
|
340
409
|
# Creates a schema object with type, description, required properties,
|
|
@@ -409,6 +478,20 @@ module Verquest
|
|
|
409
478
|
@validation_schema = properties.values.first.to_validation_schema(version: name)
|
|
410
479
|
end
|
|
411
480
|
|
|
481
|
+
# Checks whether a schema declares a default anywhere
|
|
482
|
+
#
|
|
483
|
+
# A property named `default` also counts, which only keeps default insertion on.
|
|
484
|
+
#
|
|
485
|
+
# @param schema [Object] The schema or a part of it
|
|
486
|
+
# @return [Boolean] true if any Hash in it has a "default" key
|
|
487
|
+
def property_defaults?(schema)
|
|
488
|
+
case schema
|
|
489
|
+
when Hash then schema.key?("default") || schema.each_value.any? { |value| property_defaults?(value) }
|
|
490
|
+
when Array then schema.any? { |value| property_defaults?(value) }
|
|
491
|
+
else false
|
|
492
|
+
end
|
|
493
|
+
end
|
|
494
|
+
|
|
412
495
|
# Prepares the parameter mapping for this version
|
|
413
496
|
#
|
|
414
497
|
# Collects mappings from all properties in this version and checks for
|
data/lib/verquest/versions.rb
CHANGED
|
@@ -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.
|
|
4
|
+
version: 0.10.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Petr Hlavicka
|
|
@@ -81,6 +81,7 @@ files:
|
|
|
81
81
|
- lib/verquest/conditions/rules/value_in.rb
|
|
82
82
|
- lib/verquest/conditions/variant_shape.rb
|
|
83
83
|
- lib/verquest/configuration.rb
|
|
84
|
+
- lib/verquest/correction.rb
|
|
84
85
|
- lib/verquest/gem_version.rb
|
|
85
86
|
- lib/verquest/helper_methods/conditional_properties.rb
|
|
86
87
|
- lib/verquest/helper_methods/required_properties.rb
|
|
@@ -100,6 +101,7 @@ files:
|
|
|
100
101
|
- lib/verquest/query.rb
|
|
101
102
|
- lib/verquest/query/coercer.rb
|
|
102
103
|
- lib/verquest/query/definition_check.rb
|
|
104
|
+
- lib/verquest/query/lenient_coercer.rb
|
|
103
105
|
- lib/verquest/query/parameters.rb
|
|
104
106
|
- lib/verquest/result.rb
|
|
105
107
|
- lib/verquest/supported_versions.rb
|
|
@@ -122,6 +124,7 @@ metadata:
|
|
|
122
124
|
homepage_uri: https://github.com/verquest/verquest
|
|
123
125
|
source_code_uri: https://github.com/verquest/verquest
|
|
124
126
|
changelog_uri: https://github.com/verquest/verquest/blob/main/CHANGELOG.md
|
|
127
|
+
rubygems_mfa_required: 'true'
|
|
125
128
|
rdoc_options: []
|
|
126
129
|
require_paths:
|
|
127
130
|
- lib
|