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
|
@@ -8,6 +8,9 @@ module Verquest
|
|
|
8
8
|
#
|
|
9
9
|
# @api private
|
|
10
10
|
module Base::PrivateClassMethods
|
|
11
|
+
# Property methods a filter can declare its property with
|
|
12
|
+
FILTER_PROPERTY_METHODS = %i[field array enum].freeze
|
|
13
|
+
|
|
11
14
|
# Resolves the version to use, either from the provided version,
|
|
12
15
|
# configuration's current_version, or raises an error if none is available
|
|
13
16
|
#
|
|
@@ -17,12 +20,7 @@ module Verquest
|
|
|
17
20
|
# @raise [Verquest::VersionNotFoundError] If no version matches, or supported_versions leaves the version out
|
|
18
21
|
# @raise [Verquest::DefinitionError] If supported_versions max sorts before every declared version
|
|
19
22
|
def resolve(version)
|
|
20
|
-
|
|
21
|
-
version = instance_exec(&Verquest.configuration.current_version)
|
|
22
|
-
elsif version.nil?
|
|
23
|
-
raise ArgumentError, "Version must be provided or set by Verquest.configuration.current_version"
|
|
24
|
-
end
|
|
25
|
-
|
|
23
|
+
version = requested_version(version)
|
|
26
24
|
check_supported_version!(version)
|
|
27
25
|
versions.resolve(version)
|
|
28
26
|
end
|
|
@@ -38,6 +36,70 @@ module Verquest
|
|
|
38
36
|
|
|
39
37
|
private
|
|
40
38
|
|
|
39
|
+
# Returns the version a request asks for: the given one or the configuration's current_version
|
|
40
|
+
#
|
|
41
|
+
# @param version [String, nil] The specific version
|
|
42
|
+
# @return [String] The requested version
|
|
43
|
+
# @raise [ArgumentError] If no version is provided and no current_version is configured
|
|
44
|
+
def requested_version(version)
|
|
45
|
+
return version unless version.nil?
|
|
46
|
+
raise ArgumentError, "Version must be provided or set by Verquest.configuration.current_version" unless Verquest.configuration.current_version
|
|
47
|
+
|
|
48
|
+
instance_exec(&Verquest.configuration.current_version)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Returns whether a request corrects values instead of rejecting them
|
|
52
|
+
#
|
|
53
|
+
# @param version [String] The requested version, before resolving
|
|
54
|
+
# @param lenient [Boolean, nil] An explicit choice, or nil to compare the version with lenient_through
|
|
55
|
+
# @return [Boolean]
|
|
56
|
+
def lenient_request?(version, lenient)
|
|
57
|
+
return lenient unless lenient.nil?
|
|
58
|
+
|
|
59
|
+
through = Verquest.configuration.lenient_through
|
|
60
|
+
!through.nil? && SupportedVersions.at_or_before?(version, through)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Validates params; a lenient request removes the declared root properties that fail, other than
|
|
64
|
+
# filters and required ones, and validates again
|
|
65
|
+
#
|
|
66
|
+
# @param params [Hash] The coerced params
|
|
67
|
+
# @param version [Verquest::Version] The resolved version
|
|
68
|
+
# @param lenient [Boolean] Whether the request is lenient
|
|
69
|
+
# @return [Array(Hash, Array<Hash>, Array<String>)] The params, their validation errors and the removed property names
|
|
70
|
+
def validated_params(params, version, lenient:)
|
|
71
|
+
errors = version.validate_params(params:)
|
|
72
|
+
return [params, errors, []] unless lenient && errors.any?
|
|
73
|
+
|
|
74
|
+
invalid = invalid_property_names(errors, params, version)
|
|
75
|
+
return [params, errors, []] if invalid.empty?
|
|
76
|
+
|
|
77
|
+
params = params.except(*invalid)
|
|
78
|
+
[params, version.validate_params(params:), invalid]
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Replaces the coercion's corrections of removed properties with an :invalid one each
|
|
82
|
+
#
|
|
83
|
+
# @param corrections [Array<Verquest::Correction>] The coercion's corrections
|
|
84
|
+
# @param invalid [Array<String>] The removed property names
|
|
85
|
+
# @param sent [Hash] The params as sent, for the corrections' `from`
|
|
86
|
+
# @return [Array<Verquest::Correction>] The corrections
|
|
87
|
+
def with_invalid_corrections(corrections, invalid, sent)
|
|
88
|
+
corrections.reject { |correction| invalid.include?(correction.name) } +
|
|
89
|
+
invalid.map { |name| Correction.for(name, rule: :invalid, from: sent[name], to: nil) }
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# Removing a required property would only replace its error with "missing required properties"
|
|
93
|
+
#
|
|
94
|
+
# @return [Array<String>] The declared root properties with a validation error, other than filters and required ones
|
|
95
|
+
def invalid_property_names(errors, params, version)
|
|
96
|
+
removable = version.all_properties.keys - version.filters.keys - version.required_property_names
|
|
97
|
+
|
|
98
|
+
errors.filter_map { |error| Correction.root_name(error[:pointer]) }.uniq.select do |name|
|
|
99
|
+
params.key?(name) && removable.include?(name)
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
41
103
|
# @return [Verquest::Version, Verquest::Properties::Object] The current scope being defined
|
|
42
104
|
attr_reader :current_scope
|
|
43
105
|
|
|
@@ -87,6 +149,18 @@ module Verquest
|
|
|
87
149
|
raise VersionNotFoundError, "Version '#{version}' is outside supported_versions (#{range}) of #{name || inspect}"
|
|
88
150
|
end
|
|
89
151
|
|
|
152
|
+
# Copies the hashes and arrays in params, so inserting defaults leaves the caller's params alone
|
|
153
|
+
#
|
|
154
|
+
# @param value [Object] The params or a value inside them
|
|
155
|
+
# @return [Object] A copy of hashes and arrays, other values as they are
|
|
156
|
+
def copy_params(value)
|
|
157
|
+
case value
|
|
158
|
+
when Hash then value.transform_values { |item| copy_params(item) }
|
|
159
|
+
when Array then value.map { |item| copy_params(item) }
|
|
160
|
+
else value
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
|
|
90
164
|
# Defines a new version with optional inheritance from another version
|
|
91
165
|
#
|
|
92
166
|
# @param name [String] The name/identifier of the version
|
|
@@ -106,6 +180,7 @@ module Verquest
|
|
|
106
180
|
|
|
107
181
|
@default_options = {}
|
|
108
182
|
@current_scope = version
|
|
183
|
+
@defining_version = version
|
|
109
184
|
|
|
110
185
|
instance_exec(&block)
|
|
111
186
|
ensure
|
|
@@ -336,6 +411,37 @@ module Verquest
|
|
|
336
411
|
add_property(array, parameter_examples)
|
|
337
412
|
end
|
|
338
413
|
|
|
414
|
+
# Defines a property and the block that filters by its processed value
|
|
415
|
+
#
|
|
416
|
+
# The property is declared with `field`, `array` or `enum` (`as:`), with the other options. The
|
|
417
|
+
# block is stored, not run: {PublicClassMethods#apply_filters} calls it later, and `self` inside it
|
|
418
|
+
# is the request class, so the class's private class methods are available. Filters are inherited,
|
|
419
|
+
# excluded and replaced with their properties.
|
|
420
|
+
#
|
|
421
|
+
# @example
|
|
422
|
+
# filter :status, as: :array, type: :string, explode: false do |target, value|
|
|
423
|
+
# target.where(status: value)
|
|
424
|
+
# end
|
|
425
|
+
#
|
|
426
|
+
# @param name [Symbol] The name of the property
|
|
427
|
+
# @param as [Symbol] `:field` (default), `:array` or `:enum`
|
|
428
|
+
# @param options [Hash] Options for the property method
|
|
429
|
+
# @yieldparam target [Object] What is being filtered, as returned by the previous filter
|
|
430
|
+
# @yieldparam value [Object] The property's processed value, never nil or empty
|
|
431
|
+
# @yieldreturn [Object] The filtered target
|
|
432
|
+
# @return [void]
|
|
433
|
+
# @raise [Verquest::DefinitionError] Without a block, with another `as:`, with `map:`, or outside a version's root
|
|
434
|
+
def filter(name, as: :field, **options, &block)
|
|
435
|
+
label = "Filter '#{name}' in version '#{@defining_version&.name}'"
|
|
436
|
+
raise DefinitionError, "#{label} needs a block that filters the target" unless block
|
|
437
|
+
raise DefinitionError, "#{label} can't be declared as: #{as.inspect}, only as :field, :array or :enum" unless FILTER_PROPERTY_METHODS.include?(as)
|
|
438
|
+
raise DefinitionError, "#{label} can't use map:, apply_filters reads the value by its name" if options.key?(:map)
|
|
439
|
+
raise DefinitionError, "#{label} must be declared at the root of the version, outside object, collection, one_of and conditions" unless current_scope.is_a?(Version)
|
|
440
|
+
|
|
441
|
+
send(as, name, **options)
|
|
442
|
+
current_scope.add_filter(name, block)
|
|
443
|
+
end
|
|
444
|
+
|
|
339
445
|
# Defines a oneOf property for polymorphic schemas
|
|
340
446
|
#
|
|
341
447
|
# Creates a JSON Schema oneOf structure where exactly one of the defined
|
|
@@ -366,6 +472,60 @@ module Verquest
|
|
|
366
472
|
@current_scope = previous_scope if block_given?
|
|
367
473
|
end
|
|
368
474
|
|
|
475
|
+
# Errors for root keys that only another version declares
|
|
476
|
+
#
|
|
477
|
+
# @param params [Hash] The params being processed
|
|
478
|
+
# @param version [Verquest::Version] The resolved version
|
|
479
|
+
# @return [Array<Hash>] One error per key, in the format of validation errors
|
|
480
|
+
def other_version_key_errors(params, version)
|
|
481
|
+
other_names = versions.property_names - version.all_properties.keys
|
|
482
|
+
|
|
483
|
+
params.keys.filter_map do |key|
|
|
484
|
+
key = key.to_s
|
|
485
|
+
next unless other_names.include?(key)
|
|
486
|
+
|
|
487
|
+
{pointer: "/#{key}", type: "other_version", message: "`#{key}` isn't available in version #{version.name}", details: nil}
|
|
488
|
+
end
|
|
489
|
+
end
|
|
490
|
+
|
|
491
|
+
# Reports failed params according to the validation_error_handling setting
|
|
492
|
+
#
|
|
493
|
+
# @param errors [Array<Hash>] The errors
|
|
494
|
+
# @return [Verquest::Result] A failure, when validation_error_handling is :result
|
|
495
|
+
# @raise [Verquest::InvalidParamsError] When validation_error_handling is :raise
|
|
496
|
+
def validation_failure(errors)
|
|
497
|
+
case Verquest.configuration.validation_error_handling
|
|
498
|
+
when :raise
|
|
499
|
+
raise InvalidParamsError.new("Validation failed", errors:)
|
|
500
|
+
when :result
|
|
501
|
+
Result.failure(errors)
|
|
502
|
+
end
|
|
503
|
+
end
|
|
504
|
+
|
|
505
|
+
# Maps valid params according to the validation_error_handling setting
|
|
506
|
+
#
|
|
507
|
+
# @param params [Hash] The validated params
|
|
508
|
+
# @param version [Verquest::Version] The resolved version
|
|
509
|
+
# @return [Verquest::Result, Hash] A Result when validation_error_handling is :result, the mapped params otherwise
|
|
510
|
+
# @raise [Verquest::MappingError] When mapping fails and validation_error_handling is :raise
|
|
511
|
+
def mapped_result(params, version)
|
|
512
|
+
mapped_params = version.map_params(params)
|
|
513
|
+
|
|
514
|
+
case Verquest.configuration.validation_error_handling
|
|
515
|
+
when :raise
|
|
516
|
+
mapped_params
|
|
517
|
+
when :result
|
|
518
|
+
Result.success(mapped_params)
|
|
519
|
+
end
|
|
520
|
+
rescue MappingError => e
|
|
521
|
+
case Verquest.configuration.validation_error_handling
|
|
522
|
+
when :raise
|
|
523
|
+
raise
|
|
524
|
+
when :result
|
|
525
|
+
Result.failure([{message: e.message, type: "mapping_error"}])
|
|
526
|
+
end
|
|
527
|
+
end
|
|
528
|
+
|
|
369
529
|
# Adds a property to the current scope
|
|
370
530
|
#
|
|
371
531
|
# @param property [Verquest::Properties::Base] The property to add
|
|
@@ -15,44 +15,80 @@ module Verquest
|
|
|
15
15
|
# @param version [String, nil] Specific version to use, defaults to configuration setting
|
|
16
16
|
# @param validate [Boolean, nil] Whether to validate the params, defaults to configuration setting
|
|
17
17
|
# @param remove_extra_root_keys [Boolean, nil] Whether to remove extra keys at the root level, defaults to configuration setting
|
|
18
|
+
# @param reject_other_version_keys [Boolean, nil] Whether to report root keys only another version declares as validation errors (only when validating), defaults to configuration setting
|
|
19
|
+
# @param lenient [Boolean, nil] Whether a query or path request corrects values instead of rejecting them, defaults to
|
|
20
|
+
# comparing the requested version with the configuration's lenient_through
|
|
21
|
+
# @yieldparam correction [Verquest::Correction] Each correction a lenient request made
|
|
18
22
|
# @return [Verquest::Result, Hash] When validation_error_handling is :result, returns a Success result with mapped params or Failure result with validation errors.
|
|
19
23
|
# When validation_error_handling is :raise, returns mapped params directly or raises InvalidParamsError with validation errors.
|
|
20
|
-
def process(params, version: nil, validate: nil, remove_extra_root_keys: nil)
|
|
24
|
+
def process(params, version: nil, validate: nil, remove_extra_root_keys: nil, reject_other_version_keys: nil, lenient: nil, &on_correction)
|
|
21
25
|
validate = Verquest.configuration.validate_params if validate.nil?
|
|
22
26
|
remove_extra_root_keys = Verquest.configuration.remove_extra_root_keys if remove_extra_root_keys.nil?
|
|
27
|
+
reject_other_version_keys = Verquest.configuration.reject_other_version_keys if reject_other_version_keys.nil?
|
|
23
28
|
|
|
29
|
+
version = requested_version(version)
|
|
24
30
|
version_class = resolve(version)
|
|
31
|
+
lenient = lenient_request?(version, lenient) && version_class.parameters?
|
|
25
32
|
|
|
26
33
|
params = params.dup
|
|
27
34
|
params = params.to_unsafe_h if params.respond_to?(:to_unsafe_h)
|
|
35
|
+
|
|
36
|
+
if validate && reject_other_version_keys && (other_version_errors = other_version_key_errors(params, version_class)).any?
|
|
37
|
+
return validation_failure(other_version_errors)
|
|
38
|
+
end
|
|
39
|
+
|
|
28
40
|
params = params.slice(*version_class.all_properties.keys) if remove_extra_root_keys && !version_class.combination?
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
41
|
+
sent = params
|
|
42
|
+
corrections = []
|
|
43
|
+
params = version_class.coerce_params(params, lenient:) { |correction| corrections << correction }
|
|
44
|
+
params = copy_params(params) if validate && version_class.inserts_property_defaults?
|
|
45
|
+
|
|
46
|
+
params, errors, invalid = validate ? validated_params(params, version_class, lenient:) : [params, [], []]
|
|
47
|
+
with_invalid_corrections(corrections, invalid, sent).each(&on_correction) if on_correction
|
|
48
|
+
return validation_failure(errors) if errors.any?
|
|
49
|
+
|
|
50
|
+
mapped_result(params, version_class)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Returns the names of the root properties the version declares, as clients send them
|
|
54
|
+
#
|
|
55
|
+
# @example
|
|
56
|
+
# DealsIndexQuery.property_names(version: "2025-06") # => ["page", "archived", "sort_direction", "status", "created"]
|
|
57
|
+
#
|
|
58
|
+
# @param version [String, nil] Specific version to use, defaults to configuration setting
|
|
59
|
+
# @return [Array<String>] Root property names in declaration order, inherited ones included
|
|
60
|
+
def property_names(version: nil)
|
|
61
|
+
resolve(version).all_properties.keys
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Returns the names of the filters the version declares
|
|
65
|
+
#
|
|
66
|
+
# @param version [String, nil] Specific version to use, defaults to configuration setting
|
|
67
|
+
# @return [Array<String>] Filter names in declaration order
|
|
68
|
+
def filter_names(version: nil)
|
|
69
|
+
resolve(version).filters.keys
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Filters a target with the version's filters
|
|
73
|
+
#
|
|
74
|
+
# Runs the filter blocks in declaration order. Each block gets the previous block's result and the
|
|
75
|
+
# value of its property in params; a filter whose value is nil or empty is skipped.
|
|
76
|
+
#
|
|
77
|
+
# @example
|
|
78
|
+
# params = DealsQuery.process(request.query_parameters)
|
|
79
|
+
# DealsQuery.apply_filters(Deal.all, params)
|
|
80
|
+
#
|
|
81
|
+
# @param target [Object] What to filter, such as an ActiveRecord relation
|
|
82
|
+
# @param params [Hash{String => Object}] Processed params with String keys, as {#process} returns them (the
|
|
83
|
+
# result's `value` when validation_error_handling is :result); Symbol keys aren't read
|
|
84
|
+
# @param version [String, nil] Specific version to use, defaults to configuration setting
|
|
85
|
+
# @return [Object] The filtered target
|
|
86
|
+
def apply_filters(target, params, version: nil)
|
|
87
|
+
resolve(version).filters.reduce(target) do |filtered, (name, block)|
|
|
88
|
+
value = params[name]
|
|
89
|
+
next filtered if value.nil? || (value.respond_to?(:empty?) && value.empty?)
|
|
90
|
+
|
|
91
|
+
block.call(filtered, value)
|
|
56
92
|
end
|
|
57
93
|
end
|
|
58
94
|
|
|
@@ -65,9 +65,17 @@ module Verquest
|
|
|
65
65
|
# @!attribute [rw] query_array_explode
|
|
66
66
|
# Default explode value of array properties in query requests (`location :query`)
|
|
67
67
|
# @return [Boolean] true for repeated keys like `status[]=a&status[]=b` (default), false for comma-separated values like `status=a,b`
|
|
68
|
+
#
|
|
69
|
+
# @!attribute [rw] reject_other_version_keys
|
|
70
|
+
# Controls whether `process` reports root keys that only another version of the request declares, when it validates params
|
|
71
|
+
# @return [Boolean] true to report them as validation errors, false to treat them as unknown keys (default)
|
|
72
|
+
#
|
|
73
|
+
# @!attribute [rw] lenient_through
|
|
74
|
+
# The last version whose query and path requests are corrected instead of rejected (see {Verquest::Correction})
|
|
75
|
+
# @return [String, nil] A version name; nil (default) makes every request strict
|
|
68
76
|
attr_accessor :validate_params, :json_schema_version, :validation_error_handling,
|
|
69
77
|
:remove_extra_root_keys, :insert_property_defaults, :default_additional_properties,
|
|
70
|
-
:query_array_explode
|
|
78
|
+
:query_array_explode, :reject_other_version_keys, :lenient_through
|
|
71
79
|
|
|
72
80
|
# @!attribute [r] current_version
|
|
73
81
|
# A callable object that returns the current API version to use when not explicitly specified
|
|
@@ -95,6 +103,8 @@ module Verquest
|
|
|
95
103
|
@custom_field_types = {}
|
|
96
104
|
@default_additional_properties = false
|
|
97
105
|
@query_array_explode = true
|
|
106
|
+
@reject_other_version_keys = false
|
|
107
|
+
@lenient_through = nil
|
|
98
108
|
end
|
|
99
109
|
|
|
100
110
|
# Sets the current version strategy using a callable object
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Verquest
|
|
4
|
+
# A query or path value a lenient request corrected instead of rejecting
|
|
5
|
+
#
|
|
6
|
+
# @!attribute [r] path
|
|
7
|
+
# @return [String] JSON pointer of the corrected value, such as "/sort_direction", "/status/0" or
|
|
8
|
+
# "/created/gte"; an :invalid correction points at the removed root property
|
|
9
|
+
# @!attribute [r] rule
|
|
10
|
+
# @return [Symbol] :blank, :enum_case, :maximum, :datetime_offset or :invalid
|
|
11
|
+
# @!attribute [r] from
|
|
12
|
+
# @return [Object] The value the client sent; don't echo it unescaped
|
|
13
|
+
# @!attribute [r] to
|
|
14
|
+
# @return [Object, nil] The corrected value, nil when the property was removed
|
|
15
|
+
Correction = Data.define(:path, :rule, :from, :to) do
|
|
16
|
+
# Builds a JSON pointer, escaping "~" and "/" in the segments
|
|
17
|
+
#
|
|
18
|
+
# @param segments [Array<String>] The value's path
|
|
19
|
+
# @return [String] The pointer, such as "/status/0"
|
|
20
|
+
def self.pointer(segments) = segments.map { |segment| "/#{segment.gsub(%r{[~/]}, "~" => "~0", "/" => "~1")}" }.join
|
|
21
|
+
|
|
22
|
+
# Reads the root property's name from a JSON pointer
|
|
23
|
+
#
|
|
24
|
+
# @param pointer [String] A JSON pointer, such as "/status/0"
|
|
25
|
+
# @return [String, nil] The unescaped first segment, nil for the root pointer ""
|
|
26
|
+
def self.root_name(pointer) = pointer.split("/")[1]&.gsub(/~[01]/, "~0" => "~", "~1" => "/")
|
|
27
|
+
|
|
28
|
+
# Builds the correction of a root property
|
|
29
|
+
#
|
|
30
|
+
# @param name [String] The root property's name
|
|
31
|
+
# @param rule [Symbol] The rule that corrected the value
|
|
32
|
+
# @param from [Object] The value the client sent
|
|
33
|
+
# @param to [Object, nil] The corrected value, nil when the property was removed
|
|
34
|
+
# @return [Verquest::Correction]
|
|
35
|
+
def self.for(name, rule:, from:, to:) = new(path: pointer([name]), rule:, from:, to:)
|
|
36
|
+
|
|
37
|
+
# @return [String] The root property's name, such as "status" for "/status/0"
|
|
38
|
+
def name = self.class.root_name(path)
|
|
39
|
+
end
|
|
40
|
+
end
|
data/lib/verquest/gem_version.rb
CHANGED
|
@@ -78,7 +78,7 @@ module Verquest
|
|
|
78
78
|
return coerced unless coerced.equal?(NO_MATCH)
|
|
79
79
|
end
|
|
80
80
|
|
|
81
|
-
value_schema
|
|
81
|
+
variants(value_schema).each do |variant|
|
|
82
82
|
coerced = coerce(value, variant, path)
|
|
83
83
|
return coerced unless coerced.equal?(NO_MATCH)
|
|
84
84
|
end
|
|
@@ -86,6 +86,11 @@ module Verquest
|
|
|
86
86
|
NO_MATCH
|
|
87
87
|
end
|
|
88
88
|
|
|
89
|
+
# @return [Array<Hash>] The schema's anyOf and oneOf variants
|
|
90
|
+
def variants(value_schema)
|
|
91
|
+
value_schema.values_at("anyOf", "oneOf").compact.flatten
|
|
92
|
+
end
|
|
93
|
+
|
|
89
94
|
# @return [Array<String>] The schema's types, inferred from enum and const values without a type
|
|
90
95
|
def types(value_schema)
|
|
91
96
|
if value_schema.key?("type")
|
|
@@ -102,11 +107,11 @@ module Verquest
|
|
|
102
107
|
# @return [Object] The value converted to the type or NO_MATCH
|
|
103
108
|
def coerce_type(type, value, value_schema, path)
|
|
104
109
|
case type
|
|
105
|
-
when "integer" then coerce_integer(value)
|
|
106
|
-
when "number" then coerce_number(value)
|
|
110
|
+
when "integer" then coerce_integer(value, value_schema, path)
|
|
111
|
+
when "number" then coerce_number(value, value_schema, path)
|
|
107
112
|
when "boolean" then coerce_boolean(value)
|
|
108
113
|
when "null" then (value.nil? || value == "") ? nil : NO_MATCH
|
|
109
|
-
when "string" then coerce_string(value, value_schema)
|
|
114
|
+
when "string" then coerce_string(value, value_schema, path)
|
|
110
115
|
when "array" then coerce_array(value, value_schema, path)
|
|
111
116
|
when "object" then value.is_a?(::Hash) ? coerce_properties(value, value_schema, path) : NO_MATCH
|
|
112
117
|
else NO_MATCH
|
|
@@ -114,7 +119,7 @@ module Verquest
|
|
|
114
119
|
end
|
|
115
120
|
|
|
116
121
|
# @return [Integer, Object] The integer or NO_MATCH
|
|
117
|
-
def coerce_integer(value)
|
|
122
|
+
def coerce_integer(value, _value_schema, _path)
|
|
118
123
|
return value if value.is_a?(::Integer)
|
|
119
124
|
return NO_MATCH unless value.is_a?(::String) && INTEGER_PATTERN.match?(value)
|
|
120
125
|
|
|
@@ -122,7 +127,7 @@ module Verquest
|
|
|
122
127
|
end
|
|
123
128
|
|
|
124
129
|
# @return [Numeric, Object] The number or NO_MATCH
|
|
125
|
-
def coerce_number(value)
|
|
130
|
+
def coerce_number(value, _value_schema, _path)
|
|
126
131
|
return value if value.is_a?(::Numeric)
|
|
127
132
|
return NO_MATCH unless value.is_a?(::String) && NUMBER_PATTERN.match?(value)
|
|
128
133
|
|
|
@@ -139,7 +144,7 @@ module Verquest
|
|
|
139
144
|
# A string outside the schema's enum tries the next type, so "" can still map to nil
|
|
140
145
|
#
|
|
141
146
|
# @return [String, Object] The string or NO_MATCH
|
|
142
|
-
def coerce_string(value, value_schema)
|
|
147
|
+
def coerce_string(value, value_schema, _path)
|
|
143
148
|
return NO_MATCH unless value.is_a?(::String)
|
|
144
149
|
return NO_MATCH if value_schema.key?("enum") && !value_schema["enum"].include?(value)
|
|
145
150
|
|
|
@@ -152,8 +157,8 @@ module Verquest
|
|
|
152
157
|
items = split(value, path)
|
|
153
158
|
return items if items.equal?(NO_MATCH) || !array_schema.key?("items")
|
|
154
159
|
|
|
155
|
-
items.map do |item|
|
|
156
|
-
coerced = coerce(item, array_schema["items"], path)
|
|
160
|
+
items.each_with_index.map do |item, index|
|
|
161
|
+
coerced = coerce(item, array_schema["items"], path + [index.to_s])
|
|
157
162
|
coerced.equal?(NO_MATCH) ? item : coerced
|
|
158
163
|
end
|
|
159
164
|
end
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Verquest
|
|
4
|
+
module Query
|
|
5
|
+
# Converts query parameters like {Coercer} and corrects values a lenient request tolerates
|
|
6
|
+
#
|
|
7
|
+
# Each correction is reported to the `on_correction` callable as a {Verquest::Correction}.
|
|
8
|
+
# A value gets at most one correction. Values no rule corrects are left for validation.
|
|
9
|
+
#
|
|
10
|
+
# @example
|
|
11
|
+
# coercer = LenientCoercer.new(schema:, explodes: {}, required: [], on_correction: ->(correction) { log(correction) })
|
|
12
|
+
# coercer.call({"page" => ""}) # => {}
|
|
13
|
+
class LenientCoercer < Coercer
|
|
14
|
+
# A date-time with at most 9 fraction digits, without an offset or with an offset without a colon
|
|
15
|
+
DATE_TIME_TO_FIX = /\A(?<date>\d{4}-\d{2}-\d{2})[Tt ](?<time>\d{2}:\d{2}:\d{2}(?:\.\d{1,9})?)(?:(?<hours>[+-]\d{2})(?<minutes>\d{2}))?\z/
|
|
16
|
+
|
|
17
|
+
# @param schema [Hash] The version's validation schema
|
|
18
|
+
# @param explodes [Hash{Array<String> => Boolean}] The explode value of each array property, by path
|
|
19
|
+
# @param required [Array<String>] Root properties that are never removed (see {Verquest::Version#required_property_names})
|
|
20
|
+
# @param on_correction [#call, nil] Called with each {Verquest::Correction}
|
|
21
|
+
def initialize(schema:, explodes:, required:, on_correction:)
|
|
22
|
+
super(schema:, explodes:)
|
|
23
|
+
@required = required
|
|
24
|
+
@on_correction = on_correction
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
private
|
|
28
|
+
|
|
29
|
+
attr_reader :required, :on_correction
|
|
30
|
+
|
|
31
|
+
# Removes blank root values before converting the rest; a required property is kept, so it
|
|
32
|
+
# fails validation with its own error
|
|
33
|
+
#
|
|
34
|
+
# @return [Hash] A copy of the hash without blank root values, with each known property converted
|
|
35
|
+
def coerce_properties(hash, object_schema, path)
|
|
36
|
+
return super unless path.empty?
|
|
37
|
+
|
|
38
|
+
properties = object_schema.fetch("properties", {})
|
|
39
|
+
blank = hash.keys.select { |key| removable_blank?(key.to_s, hash[key], properties[key.to_s]) }
|
|
40
|
+
blank.each { |key| correct(path + [key.to_s], :blank, hash[key], nil) }
|
|
41
|
+
|
|
42
|
+
super(hash.except(*blank), object_schema, path)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# @return [Boolean] Whether the value is a blank string of a property that isn't required and rejects it
|
|
46
|
+
def removable_blank?(name, value, property_schema)
|
|
47
|
+
!required.include?(name) && blank_property?(value, property_schema)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# @return [Boolean] Whether the value is a blank string that the known property rejects
|
|
51
|
+
def blank_property?(value, property_schema)
|
|
52
|
+
!property_schema.nil? && value.is_a?(::String) && value.strip.empty? && !accepts_blank?(property_schema)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Strict validation accepts a blank value for an array (it becomes []), a property that
|
|
56
|
+
# accepts null, and a string without constraints, also as an anyOf or oneOf variant,
|
|
57
|
+
# so correcting it would only add noise
|
|
58
|
+
#
|
|
59
|
+
# @return [Boolean] Whether the schema accepts a blank string as it is
|
|
60
|
+
def accepts_blank?(value_schema)
|
|
61
|
+
types = types(value_schema)
|
|
62
|
+
|
|
63
|
+
types.include?("array") || types.include?("null") || unconstrained_string?(value_schema, types) ||
|
|
64
|
+
variants(value_schema).any? { |variant| accepts_blank?(variant) }
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# @return [Boolean] Whether the schema is a string without constraints a blank string fails
|
|
68
|
+
def unconstrained_string?(value_schema, types)
|
|
69
|
+
types == ["string"] && (value_schema.keys & %w[enum format pattern minLength const]).empty?
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# @return [Integer, Object] The integer, capped at the schema's maximum, or NO_MATCH
|
|
73
|
+
def coerce_integer(value, value_schema, path)
|
|
74
|
+
capped(super, value, value_schema, path)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# @return [Numeric, Object] The number, capped at the schema's maximum, or NO_MATCH
|
|
78
|
+
def coerce_number(value, value_schema, path)
|
|
79
|
+
capped(super, value, value_schema, path)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# @return [Numeric, Object] The schema's maximum when the coerced number is above it, else the coerced value
|
|
83
|
+
def capped(coerced, value, value_schema, path)
|
|
84
|
+
maximum = value_schema["maximum"]
|
|
85
|
+
return coerced if coerced.equal?(NO_MATCH) || maximum.nil? || coerced <= maximum
|
|
86
|
+
|
|
87
|
+
correct(path, :maximum, value, maximum)
|
|
88
|
+
maximum
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Matches an enum value case-insensitively when exactly one declared value matches,
|
|
92
|
+
# and gives a date-time an offset
|
|
93
|
+
#
|
|
94
|
+
# @return [String, Object] The string, the declared value, the date-time with an offset or NO_MATCH
|
|
95
|
+
def coerce_string(value, value_schema, path)
|
|
96
|
+
coerced = super
|
|
97
|
+
return coerced unless value.is_a?(::String)
|
|
98
|
+
return enum_case(value, value_schema, path) if coerced.equal?(NO_MATCH) && value_schema.key?("enum")
|
|
99
|
+
return with_offset(value, path) if value_schema["format"] == "date-time"
|
|
100
|
+
|
|
101
|
+
coerced
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# @return [String, Object] The only declared value matching case-insensitively or NO_MATCH
|
|
105
|
+
def enum_case(value, value_schema, path)
|
|
106
|
+
matches = value_schema["enum"].select { |declared| declared.is_a?(::String) && declared.casecmp?(value) }
|
|
107
|
+
return NO_MATCH unless matches.one?
|
|
108
|
+
|
|
109
|
+
correct(path, :enum_case, value, matches.first)
|
|
110
|
+
matches.first
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# Adds "Z" to a date-time without an offset, and a colon to a "+HHMM" offset, with "T"
|
|
114
|
+
# between the date and the time
|
|
115
|
+
#
|
|
116
|
+
# @return [String] The date-time, corrected when it matches DATE_TIME_TO_FIX
|
|
117
|
+
def with_offset(value, path)
|
|
118
|
+
match = DATE_TIME_TO_FIX.match(value)
|
|
119
|
+
return value unless match
|
|
120
|
+
|
|
121
|
+
corrected = "#{match[:date]}T#{match[:time]}#{offset(match)}"
|
|
122
|
+
correct(path, :datetime_offset, value, corrected)
|
|
123
|
+
corrected
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# @return [String] "+HH:MM" from a "+HHMM" offset, or "Z" without an offset
|
|
127
|
+
def offset(match) = match[:hours] ? "#{match[:hours]}:#{match[:minutes]}" : "Z"
|
|
128
|
+
|
|
129
|
+
# Reports a correction at the corrected value's JSON pointer
|
|
130
|
+
#
|
|
131
|
+
# @param path [Array<String>] The value's path, with array indexes
|
|
132
|
+
# @return [void]
|
|
133
|
+
def correct(path, rule, from, to)
|
|
134
|
+
on_correction&.call(Correction.new(path: Correction.pointer(path), rule:, from:, to:))
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|