verquest 0.9.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 +9 -0
- data/README.md +30 -0
- data/docs/rails.md +6 -1
- data/lib/verquest/base/private_class_methods.rb +89 -6
- data/lib/verquest/base/public_class_methods.rb +25 -23
- data/lib/verquest/configuration.rb +6 -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 +36 -4
- metadata +3 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ba6bc529dbf828ec473edcf82a1cedefe5d3b1ac4b581a159e5421ab3dc9f751
|
|
4
|
+
data.tar.gz: 7a1e7463fcd6987fcaaed5fab9b757acff2e7176f7a5a490e6de5c6197e84b8b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 67030d45303503d3e2ea2a8258c7c74081c66582458e228a965cd0335431a1a713ad6617d5dd6a7242ce84ff0e79c9890a7d27f8b704a4e295e857ae9414cdc7
|
|
7
|
+
data.tar.gz: 5f503d96382ca5c77ba193cfd5cabf2e494fa3273b770b1f73a38b07e6e96151983b571cb35def51378278060a1874104c67d9efa91dd6f379a99c55ba2eb8db
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
## [Unreleased]
|
|
2
2
|
|
|
3
|
+
## [0.10.0] - 2026-11-09
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
- `config.lenient_through = "2025-06"` makes query and path requests for that version or an earlier one lenient; `process(..., lenient: true/false)` overrides it per call. A lenient request corrects values instead of rejecting them: blank root values are removed where strict validation would reject them (not for required properties, arrays, properties that accept null, or strings without constraints, also as an anyOf or oneOf variant), enum values match case-insensitively when one value matches, numbers above `maximum` are capped, date-times (with at most 9 fraction digits) without an offset get `Z` and `+HHMM` offsets a colon, with `T` between the date and the time, and root properties that still fail validation are removed, except filters and properties required by `required`, `dependentRequired` or a condition. A value gets at most one correction. `process` yields a `Verquest::Correction` (`path` as the JSON pointer of the corrected value, `rule`, `from` as sent, `to`, and `name` of the root property) for each, also before raising for a validation failure.
|
|
7
|
+
- `property_names(version:)` lists the root property names a version declares, inherited ones included, in declaration order.
|
|
8
|
+
|
|
9
|
+
### Changed
|
|
10
|
+
- The private `Query::Coercer` type hooks `coerce_integer`, `coerce_number` and `coerce_string` take `(value, value_schema, path)`. A subclass overriding them must accept the new arguments.
|
|
11
|
+
|
|
3
12
|
## [0.9.0] - 2026-11-09
|
|
4
13
|
|
|
5
14
|
### Added
|
data/README.md
CHANGED
|
@@ -863,6 +863,32 @@ array :status, type: :string, explode: false, item_schema_options: {enum: %w[ope
|
|
|
863
863
|
|
|
864
864
|
A property reference to another query class reuses its definition, e.g. a shared `expand` parameter: `reference :expand, from: ExpandQuery, property: :expand`.
|
|
865
865
|
|
|
866
|
+
#### Lenient requests
|
|
867
|
+
|
|
868
|
+
Tightening validation can break clients of a released version. `config.lenient_through` makes query and path requests for that version, or an earlier one, correct values instead of rejecting them. Later versions and body requests stay strict, and `process(..., lenient: true/false)` overrides the setting per call.
|
|
869
|
+
|
|
870
|
+
```ruby
|
|
871
|
+
Verquest.configure do |config|
|
|
872
|
+
config.lenient_through = "2025-06"
|
|
873
|
+
end
|
|
874
|
+
```
|
|
875
|
+
|
|
876
|
+
| Rule | Value sent | Correction |
|
|
877
|
+
|------|------------|------------|
|
|
878
|
+
| `blank` | `page=` | Removed, where strict validation would reject it (not for required properties, arrays, properties that accept null, or strings without constraints, also as an anyOf or oneOf variant) |
|
|
879
|
+
| `enum_case` | `sort_direction=desc` | `"DESC"`, when exactly one enum value matches case-insensitively |
|
|
880
|
+
| `maximum` | `limit=200` | Capped at the `maximum`, `100` |
|
|
881
|
+
| `datetime_offset` | `2025-01-12 15:30:00`, `...+0100` | `T` between the date and the time, and `Z` added or a colon added to the offset (`+01:00`) |
|
|
882
|
+
| `invalid` | `page=0` | Removed when it still fails validation, except a filter or a required property, which is still rejected |
|
|
883
|
+
|
|
884
|
+
A value gets at most one correction: a value that is still invalid after a correction is removed, except a filter or a property required by `required`, `dependentRequired` or a condition, which fails validation with its own error, as in strict requests. This is conservative: a property any `dependentRequired` or condition can require is kept even when the trigger is absent or the condition doesn't apply. `process` yields a `Verquest::Correction` for each, also before it raises for a validation failure: `path` is the JSON pointer of the corrected value (`/status/0`, `/created/gte`, or the removed root property), `name` its root property, `rule`, `from` as sent and `to`:
|
|
885
|
+
|
|
886
|
+
```ruby
|
|
887
|
+
params = ListQuery.process(request.query_parameters) do |correction|
|
|
888
|
+
logger.info(correction.to_h.except(:from)) # `from` is what the client sent
|
|
889
|
+
end
|
|
890
|
+
```
|
|
891
|
+
|
|
866
892
|
### Filters
|
|
867
893
|
|
|
868
894
|
`filter` declares a property and the block that filters by its value. The property is a `field` by default, an `array` with `as: :array`, or an `enum` with `as: :enum`, in a body or a query request. `apply_filters` passes the target through the version's filters in declaration order, and skips a filter when its value is nil or empty. `DealsIndexQuery` from [Query parameters](#query-parameters) filters by `status` from `2025-06`, and also by `site_id` from `2025-08`:
|
|
@@ -871,6 +897,7 @@ A property reference to another query class reuses its definition, e.g. a shared
|
|
|
871
897
|
params = DealsIndexQuery.process(request.query_parameters, version: "2025-08")
|
|
872
898
|
DealsIndexQuery.apply_filters(Deal.all, params, version: "2025-08")
|
|
873
899
|
DealsIndexQuery.filter_names(version: "2025-08") # => ["status", "site_id"]
|
|
900
|
+
DealsIndexQuery.property_names(version: "2025-08") # => ["page", "archived", "sort_direction", "status", "created", "site_id"]
|
|
874
901
|
```
|
|
875
902
|
|
|
876
903
|
- Filters follow the rules of properties. A later version inherits them, `exclude_properties` removes one, and redeclaring a filter replaces its block. Redeclaring it as a plain property, such as `field :status`, stops it filtering.
|
|
@@ -960,6 +987,9 @@ Verquest.configure do |config|
|
|
|
960
987
|
# Default explode value of query arrays (true: status[]=a&status[]=b, false: status=a,b)
|
|
961
988
|
config.query_array_explode = true # default
|
|
962
989
|
|
|
990
|
+
# Last version whose query and path requests are corrected instead of rejected, see Lenient requests
|
|
991
|
+
config.lenient_through = nil # default
|
|
992
|
+
|
|
963
993
|
# Reusable field types, see Custom Field Types
|
|
964
994
|
config.custom_field_types = {} # default
|
|
965
995
|
end
|
data/docs/rails.md
CHANGED
|
@@ -25,6 +25,8 @@ end
|
|
|
25
25
|
# config/initializers/verquest.rb
|
|
26
26
|
Verquest.configure do |config|
|
|
27
27
|
config.current_version = -> { Current.api_version }
|
|
28
|
+
# Released versions keep tolerating what they accepted before validation
|
|
29
|
+
config.lenient_through = "2025-06"
|
|
28
30
|
end
|
|
29
31
|
```
|
|
30
32
|
|
|
@@ -64,7 +66,10 @@ module VerquestParams
|
|
|
64
66
|
@path_params = by_location.fetch(:path, []).to_h { |declaration| process_path_param(declaration) }
|
|
65
67
|
# Unknown query keys (cache busters, tracking) are dropped instead of rejected. Add
|
|
66
68
|
# `reject_other_version_keys: true` to reject keys from another version, such as a newer filter.
|
|
67
|
-
|
|
69
|
+
# A lenient request (config.lenient_through) yields each value it corrected instead of rejecting.
|
|
70
|
+
@query_params = by_location[:query]&.first&.dig(:class)&.process(request.query_parameters, remove_extra_root_keys: true) do |correction|
|
|
71
|
+
Rails.logger.info("Corrected query param: #{correction.to_h.except(:from)}")
|
|
72
|
+
end
|
|
68
73
|
@body_params = by_location[:body]&.first&.dig(:class)&.process(request.request_parameters)
|
|
69
74
|
end
|
|
70
75
|
|
|
@@ -20,12 +20,7 @@ module Verquest
|
|
|
20
20
|
# @raise [Verquest::VersionNotFoundError] If no version matches, or supported_versions leaves the version out
|
|
21
21
|
# @raise [Verquest::DefinitionError] If supported_versions max sorts before every declared version
|
|
22
22
|
def resolve(version)
|
|
23
|
-
|
|
24
|
-
version = instance_exec(&Verquest.configuration.current_version)
|
|
25
|
-
elsif version.nil?
|
|
26
|
-
raise ArgumentError, "Version must be provided or set by Verquest.configuration.current_version"
|
|
27
|
-
end
|
|
28
|
-
|
|
23
|
+
version = requested_version(version)
|
|
29
24
|
check_supported_version!(version)
|
|
30
25
|
versions.resolve(version)
|
|
31
26
|
end
|
|
@@ -41,6 +36,70 @@ module Verquest
|
|
|
41
36
|
|
|
42
37
|
private
|
|
43
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
|
+
|
|
44
103
|
# @return [Verquest::Version, Verquest::Properties::Object] The current scope being defined
|
|
45
104
|
attr_reader :current_scope
|
|
46
105
|
|
|
@@ -443,6 +502,30 @@ module Verquest
|
|
|
443
502
|
end
|
|
444
503
|
end
|
|
445
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
|
+
|
|
446
529
|
# Adds a property to the current scope
|
|
447
530
|
#
|
|
448
531
|
# @param property [Verquest::Properties::Base] The property to add
|
|
@@ -16,14 +16,19 @@ module Verquest
|
|
|
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
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
|
|
19
22
|
# @return [Verquest::Result, Hash] When validation_error_handling is :result, returns a Success result with mapped params or Failure result with validation errors.
|
|
20
23
|
# When validation_error_handling is :raise, returns mapped params directly or raises InvalidParamsError with validation errors.
|
|
21
|
-
def process(params, version: nil, validate: nil, remove_extra_root_keys: nil, reject_other_version_keys: nil)
|
|
24
|
+
def process(params, version: nil, validate: nil, remove_extra_root_keys: nil, reject_other_version_keys: nil, lenient: nil, &on_correction)
|
|
22
25
|
validate = Verquest.configuration.validate_params if validate.nil?
|
|
23
26
|
remove_extra_root_keys = Verquest.configuration.remove_extra_root_keys if remove_extra_root_keys.nil?
|
|
24
27
|
reject_other_version_keys = Verquest.configuration.reject_other_version_keys if reject_other_version_keys.nil?
|
|
25
28
|
|
|
29
|
+
version = requested_version(version)
|
|
26
30
|
version_class = resolve(version)
|
|
31
|
+
lenient = lenient_request?(version, lenient) && version_class.parameters?
|
|
27
32
|
|
|
28
33
|
params = params.dup
|
|
29
34
|
params = params.to_unsafe_h if params.respond_to?(:to_unsafe_h)
|
|
@@ -33,30 +38,27 @@ module Verquest
|
|
|
33
38
|
end
|
|
34
39
|
|
|
35
40
|
params = params.slice(*version_class.all_properties.keys) if remove_extra_root_keys && !version_class.combination?
|
|
36
|
-
|
|
41
|
+
sent = params
|
|
42
|
+
corrections = []
|
|
43
|
+
params = version_class.coerce_params(params, lenient:) { |correction| corrections << correction }
|
|
37
44
|
params = copy_params(params) if validate && version_class.inserts_property_defaults?
|
|
38
45
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
when :result
|
|
56
|
-
Result.failure([{message: e.message, type: "mapping_error"}])
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
end
|
|
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
|
|
60
62
|
end
|
|
61
63
|
|
|
62
64
|
# Returns the names of the filters the version declares
|
|
@@ -69,9 +69,13 @@ module Verquest
|
|
|
69
69
|
# @!attribute [rw] reject_other_version_keys
|
|
70
70
|
# Controls whether `process` reports root keys that only another version of the request declares, when it validates params
|
|
71
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
|
|
72
76
|
attr_accessor :validate_params, :json_schema_version, :validation_error_handling,
|
|
73
77
|
:remove_extra_root_keys, :insert_property_defaults, :default_additional_properties,
|
|
74
|
-
:query_array_explode, :reject_other_version_keys
|
|
78
|
+
:query_array_explode, :reject_other_version_keys, :lenient_through
|
|
75
79
|
|
|
76
80
|
# @!attribute [r] current_version
|
|
77
81
|
# A callable object that returns the current API version to use when not explicitly specified
|
|
@@ -100,6 +104,7 @@ module Verquest
|
|
|
100
104
|
@default_additional_properties = false
|
|
101
105
|
@query_array_explode = true
|
|
102
106
|
@reject_other_version_keys = false
|
|
107
|
+
@lenient_through = nil
|
|
103
108
|
end
|
|
104
109
|
|
|
105
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
|
data/lib/verquest/version.rb
CHANGED
|
@@ -268,9 +268,26 @@ module Verquest
|
|
|
268
268
|
# Converts query or path strings to the types the validation schema expects
|
|
269
269
|
#
|
|
270
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
|
|
271
273
|
# @return [Hash] Converted params for a query or path version, the same params otherwise
|
|
272
|
-
def coerce_params(params)
|
|
273
|
-
|
|
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)
|
|
274
291
|
end
|
|
275
292
|
|
|
276
293
|
# Returns OpenAPI parameter objects for this query or path version, in OpenAPI 3.0 forms under :openapi30
|
|
@@ -329,9 +346,24 @@ module Verquest
|
|
|
329
346
|
|
|
330
347
|
private
|
|
331
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
|
+
|
|
332
361
|
# @return [Verquest::Query::Coercer, nil] The coercer of a query or path version
|
|
333
362
|
attr_reader :query_coercer
|
|
334
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
|
+
|
|
335
367
|
# @return [Hash{Array<String> => Hash}, nil] The parameter options of each property path of a query or path version
|
|
336
368
|
attr_reader :query_options
|
|
337
369
|
|
|
@@ -345,8 +377,8 @@ module Verquest
|
|
|
345
377
|
@query_options = all_properties.each_value.with_object({}) do |property, options|
|
|
346
378
|
options.merge!(property.query_options(prefix: [], version: name))
|
|
347
379
|
end.freeze
|
|
348
|
-
|
|
349
|
-
@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)
|
|
350
382
|
end
|
|
351
383
|
|
|
352
384
|
# Returns the compiled validation schema for the current configuration
|
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
|