verquest 0.9.0 → 0.11.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 +14 -0
- data/README.md +39 -0
- data/docs/rails.md +6 -1
- data/lib/verquest/base/private_class_methods.rb +120 -19
- 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/properties/array.rb +2 -2
- data/lib/verquest/properties/base.rb +8 -4
- data/lib/verquest/query/coercer.rb +37 -15
- data/lib/verquest/query/definition_check.rb +38 -3
- data/lib/verquest/query/lenient_coercer.rb +139 -0
- data/lib/verquest/query/parameters.rb +1 -1
- data/lib/verquest/version.rb +43 -6
- 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: 7419f824e6f2966c2301b9f9c2ea9a3df48c6dd7559e7adbbd3ab297476726df
|
|
4
|
+
data.tar.gz: ece88f624641894745c5b6d0a5a3dc818839ceb79deef5548657a8e9bf7553c2
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d541f01f0945d3003d21edf031eb1dca54256d5ed702b7bae7d08ab99a32407873822a787f509d0512edc93cf8cd09e4220cf7cf2b83bdf2d84c407459902524
|
|
7
|
+
data.tar.gz: 43cbf7621f6a84d7f396835bf75358d842b835f92209abc3748bdd83e3287723a0a3895abb452a092e1ded7c9ac9470a6d6987424cfe8201fc13a4f237cceb0c
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
## [Unreleased]
|
|
2
2
|
|
|
3
|
+
## [0.11.0] - 2026-11-10
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
- `case_insensitive: true` on `field`, `enum` and `array` (and `filter`) in query and path requests reads a value matching a declared string enum value case-insensitively as that value, in every version, without a lenient correction. The schema is unchanged. It raises `Verquest::DefinitionError` in a body request, with `with_options`, with a value other than `true`, `false` or `nil`, without string enum values (also on `const`, `object` and `collection`), or with values that differ only by case.
|
|
7
|
+
|
|
8
|
+
## [0.10.0] - 2026-11-09
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
- `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.
|
|
12
|
+
- `property_names(version:)` lists the root property names a version declares, inherited ones included, in declaration order.
|
|
13
|
+
|
|
14
|
+
### Changed
|
|
15
|
+
- 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.
|
|
16
|
+
|
|
3
17
|
## [0.9.0] - 2026-11-09
|
|
4
18
|
|
|
5
19
|
### Added
|
data/README.md
CHANGED
|
@@ -861,8 +861,43 @@ array :status, type: :string, explode: false, item_schema_options: {enum: %w[ope
|
|
|
861
861
|
# => {name: "status", ..., examples: {single: {value: ["open"]}, multiple: {value: ["open", "won"]}}, schema: {...}}
|
|
862
862
|
```
|
|
863
863
|
|
|
864
|
+
`case_insensitive: true` on a `field` or `enum`, or an `array` of enum items, reads a value matching a declared string value case-insensitively as that value, in every version and without a [lenient](#lenient-requests) correction. The schema lists only the declared values, so mention it in `description:` if clients should know:
|
|
865
|
+
|
|
866
|
+
```ruby
|
|
867
|
+
field :sort_direction, type: :string, enum: %w[ASC DESC], case_insensitive: true, description: "`ASC` or `DESC`, case-insensitive"
|
|
868
|
+
# ?sort_direction=desc => {"sort_direction" => "DESC"}
|
|
869
|
+
```
|
|
870
|
+
|
|
871
|
+
It raises `Verquest::DefinitionError` in a body request, with `with_options`, with a value other than `true`, `false` or `nil`, on a property without string enum values (such as a `const` or an `object`), or when two values differ only by case. A `reference` to the property keeps the option in a query or path class; a body class referencing it matches exactly.
|
|
872
|
+
|
|
864
873
|
A property reference to another query class reuses its definition, e.g. a shared `expand` parameter: `reference :expand, from: ExpandQuery, property: :expand`.
|
|
865
874
|
|
|
875
|
+
#### Lenient requests
|
|
876
|
+
|
|
877
|
+
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.
|
|
878
|
+
|
|
879
|
+
```ruby
|
|
880
|
+
Verquest.configure do |config|
|
|
881
|
+
config.lenient_through = "2025-06"
|
|
882
|
+
end
|
|
883
|
+
```
|
|
884
|
+
|
|
885
|
+
| Rule | Value sent | Correction |
|
|
886
|
+
|------|------------|------------|
|
|
887
|
+
| `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) |
|
|
888
|
+
| `enum_case` | `sort_direction=desc` | `"DESC"`, when exactly one enum value matches case-insensitively (properties with `case_insensitive: true` match without a correction) |
|
|
889
|
+
| `maximum` | `limit=200` | Capped at the `maximum`, `100` |
|
|
890
|
+
| `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`) |
|
|
891
|
+
| `invalid` | `page=0` | Removed when it still fails validation, except a filter or a required property, which is still rejected |
|
|
892
|
+
|
|
893
|
+
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`:
|
|
894
|
+
|
|
895
|
+
```ruby
|
|
896
|
+
params = ListQuery.process(request.query_parameters) do |correction|
|
|
897
|
+
logger.info(correction.to_h.except(:from)) # `from` is what the client sent
|
|
898
|
+
end
|
|
899
|
+
```
|
|
900
|
+
|
|
866
901
|
### Filters
|
|
867
902
|
|
|
868
903
|
`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 +906,7 @@ A property reference to another query class reuses its definition, e.g. a shared
|
|
|
871
906
|
params = DealsIndexQuery.process(request.query_parameters, version: "2025-08")
|
|
872
907
|
DealsIndexQuery.apply_filters(Deal.all, params, version: "2025-08")
|
|
873
908
|
DealsIndexQuery.filter_names(version: "2025-08") # => ["status", "site_id"]
|
|
909
|
+
DealsIndexQuery.property_names(version: "2025-08") # => ["page", "archived", "sort_direction", "status", "created", "site_id"]
|
|
874
910
|
```
|
|
875
911
|
|
|
876
912
|
- 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 +996,9 @@ Verquest.configure do |config|
|
|
|
960
996
|
# Default explode value of query arrays (true: status[]=a&status[]=b, false: status=a,b)
|
|
961
997
|
config.query_array_explode = true # default
|
|
962
998
|
|
|
999
|
+
# Last version whose query and path requests are corrected instead of rejected, see Lenient requests
|
|
1000
|
+
config.lenient_through = nil # default
|
|
1001
|
+
|
|
963
1002
|
# Reusable field types, see Custom Field Types
|
|
964
1003
|
config.custom_field_types = {} # default
|
|
965
1004
|
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
|
|
|
@@ -168,7 +227,10 @@ module Verquest
|
|
|
168
227
|
# @param options [Hash] The options to set during the block execution
|
|
169
228
|
# @yield Block to be executed with the temporary options
|
|
170
229
|
# @return [void]
|
|
230
|
+
# @raise [Verquest::DefinitionError] If options include case_insensitive
|
|
171
231
|
def with_options(**options, &block)
|
|
232
|
+
raise DefinitionError, "case_insensitive can't be set with with_options, set it on each property" if options.key?(:case_insensitive)
|
|
233
|
+
|
|
172
234
|
camelize(options)
|
|
173
235
|
|
|
174
236
|
original_options = default_options
|
|
@@ -188,8 +250,9 @@ module Verquest
|
|
|
188
250
|
# @param nullable [Boolean] Whether the field can be null
|
|
189
251
|
# @param schema_options [Hash] Additional schema options for the field
|
|
190
252
|
# @param parameter_examples [Hash, nil] Named OpenAPI Example Objects for the query parameter (query requests only)
|
|
253
|
+
# @param case_insensitive [Boolean, nil] Whether a query value matches the enum case-insensitively (query and path requests only)
|
|
191
254
|
# @return [void]
|
|
192
|
-
def field(name, type: nil, map: nil, required: nil, nullable: nil, parameter_examples: nil, **schema_options)
|
|
255
|
+
def field(name, type: nil, map: nil, required: nil, nullable: nil, parameter_examples: nil, case_insensitive: nil, **schema_options)
|
|
193
256
|
camelize(schema_options)
|
|
194
257
|
|
|
195
258
|
type = default_options.fetch(:type, type)
|
|
@@ -198,7 +261,7 @@ module Verquest
|
|
|
198
261
|
schema_options = default_options.except(:type, :required, :nullable).merge(schema_options)
|
|
199
262
|
|
|
200
263
|
field = Properties::Field.new(name:, type:, map:, required:, nullable:, **schema_options)
|
|
201
|
-
add_property(field, parameter_examples)
|
|
264
|
+
add_property(field, parameter_examples, case_insensitive)
|
|
202
265
|
end
|
|
203
266
|
|
|
204
267
|
# Defines a new enum property for the current version scope
|
|
@@ -210,8 +273,9 @@ module Verquest
|
|
|
210
273
|
# @param nullable [Boolean] Whether the enum can be null
|
|
211
274
|
# @param schema_options [Hash] Additional schema options for the enum
|
|
212
275
|
# @param parameter_examples [Hash, nil] Named OpenAPI Example Objects for the query parameter (query requests only)
|
|
276
|
+
# @param case_insensitive [Boolean, nil] Whether a query value matches the enum case-insensitively (query and path requests only)
|
|
213
277
|
# @return [void]
|
|
214
|
-
def enum(name, values:, map: nil, required: nil, nullable: nil, parameter_examples: nil, **schema_options)
|
|
278
|
+
def enum(name, values:, map: nil, required: nil, nullable: nil, parameter_examples: nil, case_insensitive: nil, **schema_options)
|
|
215
279
|
camelize(schema_options)
|
|
216
280
|
|
|
217
281
|
required = default_options.fetch(:required, false) if required.nil?
|
|
@@ -219,7 +283,7 @@ module Verquest
|
|
|
219
283
|
schema_options = default_options.except(:type, :required, :nullable).merge(schema_options)
|
|
220
284
|
|
|
221
285
|
enum_property = Properties::Enum.new(name:, values:, map:, required:, nullable:, **schema_options)
|
|
222
|
-
add_property(enum_property, parameter_examples)
|
|
286
|
+
add_property(enum_property, parameter_examples, case_insensitive)
|
|
223
287
|
end
|
|
224
288
|
|
|
225
289
|
# Defines a new constant property for the current version scope
|
|
@@ -231,14 +295,15 @@ module Verquest
|
|
|
231
295
|
# @param nullable [Boolean] Whether the constant can be null
|
|
232
296
|
# @param schema_options [Hash] Additional schema options for the constant
|
|
233
297
|
# @param parameter_examples [Hash, nil] Named OpenAPI Example Objects for the query parameter (query requests only)
|
|
298
|
+
# @param case_insensitive [Boolean, nil] Not supported (a constant has no enum); `true` raises Verquest::DefinitionError
|
|
234
299
|
# @return [void]
|
|
235
|
-
def const(name, value:, map: nil, required: nil, nullable: nil, parameter_examples: nil, **schema_options)
|
|
300
|
+
def const(name, value:, map: nil, required: nil, nullable: nil, parameter_examples: nil, case_insensitive: nil, **schema_options)
|
|
236
301
|
camelize(schema_options)
|
|
237
302
|
required = default_options.fetch(:required, false) if required.nil?
|
|
238
303
|
nullable = default_options.fetch(:nullable, false) if nullable.nil?
|
|
239
304
|
|
|
240
305
|
const = Properties::Const.new(name:, value:, map:, required:, nullable:, **schema_options)
|
|
241
|
-
add_property(const, parameter_examples)
|
|
306
|
+
add_property(const, parameter_examples, case_insensitive)
|
|
242
307
|
end
|
|
243
308
|
|
|
244
309
|
# Defines a new object for the current version scope
|
|
@@ -249,9 +314,10 @@ module Verquest
|
|
|
249
314
|
# @param nullable [Boolean] Whether the object can be null
|
|
250
315
|
# @param schema_options [Hash] Additional schema options for the object
|
|
251
316
|
# @param parameter_examples [Hash, nil] Named OpenAPI Example Objects for the query parameter (query requests only)
|
|
317
|
+
# @param case_insensitive [Boolean, nil] Not supported (an object has no enum); `true` raises Verquest::DefinitionError
|
|
252
318
|
# @yield Block executed in the context of the new object definition
|
|
253
319
|
# @return [void]
|
|
254
|
-
def object(name, map: nil, required: nil, nullable: nil, parameter_examples: nil, **schema_options, &block)
|
|
320
|
+
def object(name, map: nil, required: nil, nullable: nil, parameter_examples: nil, case_insensitive: nil, **schema_options, &block)
|
|
255
321
|
unless block_given?
|
|
256
322
|
raise ArgumentError, "a block must be given to define the object"
|
|
257
323
|
end
|
|
@@ -262,7 +328,7 @@ module Verquest
|
|
|
262
328
|
schema_options = default_options.except(:type, :required, :nullable).merge(schema_options)
|
|
263
329
|
|
|
264
330
|
object = Properties::Object.new(name:, map:, required:, nullable:, **schema_options)
|
|
265
|
-
add_property(object, parameter_examples)
|
|
331
|
+
add_property(object, parameter_examples, case_insensitive)
|
|
266
332
|
|
|
267
333
|
if block_given?
|
|
268
334
|
previous_scope = current_scope
|
|
@@ -282,10 +348,11 @@ module Verquest
|
|
|
282
348
|
# @param nullable [Boolean] Whether the collection can be null
|
|
283
349
|
# @param map [String, nil] An optional mapping to another collection
|
|
284
350
|
# @param schema_options [Hash] Additional schema options for the collection
|
|
351
|
+
# @param case_insensitive [Boolean, nil] Not supported (a collection has no enum); `true` raises Verquest::DefinitionError
|
|
285
352
|
# @yield Block executed in the context of the new collection definition
|
|
286
353
|
# @return [void]
|
|
287
354
|
# @raise [ArgumentError] without item: or a block, or when item does not respond to to_ref
|
|
288
|
-
def collection(name, item: nil, required: nil, nullable: nil, map: nil, **schema_options, &block)
|
|
355
|
+
def collection(name, item: nil, required: nil, nullable: nil, map: nil, case_insensitive: nil, **schema_options, &block)
|
|
289
356
|
if item.nil? && !block_given?
|
|
290
357
|
raise ArgumentError, "item must be provided or a block must be given to define the collection"
|
|
291
358
|
elsif !item.nil? && !item.respond_to?(:to_ref)
|
|
@@ -298,7 +365,7 @@ module Verquest
|
|
|
298
365
|
schema_options = default_options.except(:required, :nullable).merge(schema_options)
|
|
299
366
|
|
|
300
367
|
collection = Properties::Collection.new(name:, item:, required:, nullable:, map:, **schema_options)
|
|
301
|
-
|
|
368
|
+
add_property(collection, nil, case_insensitive)
|
|
302
369
|
|
|
303
370
|
if block_given?
|
|
304
371
|
previous_scope = current_scope
|
|
@@ -339,8 +406,9 @@ module Verquest
|
|
|
339
406
|
# @param item_schema_options [Hash] Additional JSON schema options for the array items (merged with custom type options)
|
|
340
407
|
# @param schema_options [Hash] Additional schema options for the array property
|
|
341
408
|
# @param parameter_examples [Hash, nil] Named OpenAPI Example Objects for the query parameter (query requests only)
|
|
409
|
+
# @param case_insensitive [Boolean, nil] Whether a query item matches the items' enum case-insensitively (query and path requests only)
|
|
342
410
|
# @return [void]
|
|
343
|
-
def array(name, type:, required: nil, nullable: nil, map: nil, explode: nil, parameter_examples: nil, item_schema_options: {}, **schema_options)
|
|
411
|
+
def array(name, type:, required: nil, nullable: nil, map: nil, explode: nil, parameter_examples: nil, case_insensitive: nil, item_schema_options: {}, **schema_options)
|
|
344
412
|
camelize(schema_options)
|
|
345
413
|
|
|
346
414
|
type = default_options.fetch(:type, type)
|
|
@@ -349,7 +417,7 @@ module Verquest
|
|
|
349
417
|
schema_options = default_options.except(:type, :required, :nullable).merge(schema_options)
|
|
350
418
|
|
|
351
419
|
array = Properties::Array.new(name:, type:, required:, nullable:, map:, explode:, item_schema_options:, **schema_options)
|
|
352
|
-
add_property(array, parameter_examples)
|
|
420
|
+
add_property(array, parameter_examples, case_insensitive)
|
|
353
421
|
end
|
|
354
422
|
|
|
355
423
|
# Defines a property and the block that filters by its processed value
|
|
@@ -443,13 +511,46 @@ module Verquest
|
|
|
443
511
|
end
|
|
444
512
|
end
|
|
445
513
|
|
|
514
|
+
# Maps valid params according to the validation_error_handling setting
|
|
515
|
+
#
|
|
516
|
+
# @param params [Hash] The validated params
|
|
517
|
+
# @param version [Verquest::Version] The resolved version
|
|
518
|
+
# @return [Verquest::Result, Hash] A Result when validation_error_handling is :result, the mapped params otherwise
|
|
519
|
+
# @raise [Verquest::MappingError] When mapping fails and validation_error_handling is :raise
|
|
520
|
+
def mapped_result(params, version)
|
|
521
|
+
mapped_params = version.map_params(params)
|
|
522
|
+
|
|
523
|
+
case Verquest.configuration.validation_error_handling
|
|
524
|
+
when :raise
|
|
525
|
+
mapped_params
|
|
526
|
+
when :result
|
|
527
|
+
Result.success(mapped_params)
|
|
528
|
+
end
|
|
529
|
+
rescue MappingError => e
|
|
530
|
+
case Verquest.configuration.validation_error_handling
|
|
531
|
+
when :raise
|
|
532
|
+
raise
|
|
533
|
+
when :result
|
|
534
|
+
Result.failure([{message: e.message, type: "mapping_error"}])
|
|
535
|
+
end
|
|
536
|
+
end
|
|
537
|
+
|
|
446
538
|
# Adds a property to the current scope
|
|
447
539
|
#
|
|
448
540
|
# @param property [Verquest::Properties::Base] The property to add
|
|
449
541
|
# @param parameter_examples [Hash, nil] Named OpenAPI Example Objects for the query parameter
|
|
542
|
+
# @param case_insensitive [Boolean, nil] Whether a query value matches the enum case-insensitively
|
|
450
543
|
# @return [void]
|
|
451
|
-
|
|
544
|
+
# @raise [Verquest::DefinitionError] If case_insensitive isn't true, false or nil, or is set in a body request
|
|
545
|
+
def add_property(property, parameter_examples, case_insensitive = nil)
|
|
546
|
+
label = "Property '#{property.name}' in version '#{@defining_version&.name}'"
|
|
547
|
+
unless [true, false, nil].include?(case_insensitive)
|
|
548
|
+
raise DefinitionError, "#{label} has case_insensitive: #{case_insensitive.inspect}, which must be true, false or nil"
|
|
549
|
+
end
|
|
550
|
+
raise DefinitionError, "#{label} is case_insensitive, which only query and path parameters support" if case_insensitive && location == :body
|
|
551
|
+
|
|
452
552
|
property.parameter_examples = parameter_examples
|
|
553
|
+
property.case_insensitive = case_insensitive
|
|
453
554
|
current_scope.add(property)
|
|
454
555
|
end
|
|
455
556
|
|
|
@@ -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
|
@@ -83,13 +83,13 @@ module Verquest
|
|
|
83
83
|
mapping[(key_prefix + [name]).join("/")] = mapping_value_key(value_prefix:)
|
|
84
84
|
end
|
|
85
85
|
|
|
86
|
-
# Returns this array's explode value
|
|
86
|
+
# Returns this array's explode value, parameter examples and case_insensitive
|
|
87
87
|
#
|
|
88
88
|
# @param prefix [Array<String>] Path of the parent property
|
|
89
89
|
# @param version [String] The version to resolve references with
|
|
90
90
|
# @return [Hash{Array<String> => Hash}] The options of this array, by path
|
|
91
91
|
def query_options(prefix:, version:)
|
|
92
|
-
{prefix + [name] => {explode
|
|
92
|
+
{prefix + [name] => {explode:}.compact.merge(super.fetch(prefix + [name], {}))}
|
|
93
93
|
end
|
|
94
94
|
|
|
95
95
|
private
|
|
@@ -23,7 +23,9 @@ module Verquest
|
|
|
23
23
|
# @return [String, nil] The mapping path for this property
|
|
24
24
|
# @!attribute [rw] parameter_examples
|
|
25
25
|
# @return [Hash, nil] Named OpenAPI Example Objects for this property's query parameter, kept out of the JSON schemas
|
|
26
|
-
|
|
26
|
+
# @!attribute [rw] case_insensitive
|
|
27
|
+
# @return [Boolean, nil] Whether a query value matches the enum case-insensitively
|
|
28
|
+
attr_accessor :name, :required, :map, :parameter_examples, :case_insensitive
|
|
27
29
|
|
|
28
30
|
# Adds a child property to this property
|
|
29
31
|
# @abstract
|
|
@@ -76,14 +78,16 @@ module Verquest
|
|
|
76
78
|
|
|
77
79
|
# Returns the query parameter options under this property, by path
|
|
78
80
|
#
|
|
79
|
-
# Options are `explode:` for arrays
|
|
80
|
-
# parameter examples
|
|
81
|
+
# Options are `explode:` for arrays, `examples:` for properties with
|
|
82
|
+
# parameter examples, and `case_insensitive:` for properties matching
|
|
83
|
+
# their enum case-insensitively. Paths without options are left out.
|
|
81
84
|
#
|
|
82
85
|
# @param prefix [Array<String>] Path of the parent property
|
|
83
86
|
# @param version [String] The version to resolve references with
|
|
84
87
|
# @return [Hash{Array<String> => Hash}] The options of each property path
|
|
85
88
|
def query_options(prefix:, version:)
|
|
86
|
-
|
|
89
|
+
options = {examples: parameter_examples, case_insensitive: (true if case_insensitive)}.compact
|
|
90
|
+
options.empty? ? {} : {prefix + [name] => options}
|
|
87
91
|
end
|
|
88
92
|
|
|
89
93
|
# Returns a copy of this property under another name and map:
|
|
@@ -38,9 +38,11 @@ module Verquest
|
|
|
38
38
|
|
|
39
39
|
# @param schema [Hash] The version's validation schema
|
|
40
40
|
# @param explodes [Hash{Array<String> => Boolean}] The explode value of each array property, by path
|
|
41
|
-
|
|
41
|
+
# @param case_insensitive [Set<Array<String>>] Paths of properties matching their enum case-insensitively
|
|
42
|
+
def initialize(schema:, explodes:, case_insensitive: Set.new)
|
|
42
43
|
@schema = schema
|
|
43
44
|
@explodes = explodes
|
|
45
|
+
@case_insensitive = case_insensitive
|
|
44
46
|
end
|
|
45
47
|
|
|
46
48
|
# Converts the params
|
|
@@ -53,7 +55,7 @@ module Verquest
|
|
|
53
55
|
|
|
54
56
|
private
|
|
55
57
|
|
|
56
|
-
attr_reader :schema, :explodes
|
|
58
|
+
attr_reader :schema, :explodes, :case_insensitive
|
|
57
59
|
|
|
58
60
|
# @return [Hash] A copy of the hash with each known property converted
|
|
59
61
|
def coerce_properties(hash, object_schema, path)
|
|
@@ -78,7 +80,7 @@ module Verquest
|
|
|
78
80
|
return coerced unless coerced.equal?(NO_MATCH)
|
|
79
81
|
end
|
|
80
82
|
|
|
81
|
-
value_schema
|
|
83
|
+
variants(value_schema).each do |variant|
|
|
82
84
|
coerced = coerce(value, variant, path)
|
|
83
85
|
return coerced unless coerced.equal?(NO_MATCH)
|
|
84
86
|
end
|
|
@@ -86,6 +88,11 @@ module Verquest
|
|
|
86
88
|
NO_MATCH
|
|
87
89
|
end
|
|
88
90
|
|
|
91
|
+
# @return [Array<Hash>] The schema's anyOf and oneOf variants
|
|
92
|
+
def variants(value_schema)
|
|
93
|
+
value_schema.values_at("anyOf", "oneOf").compact.flatten
|
|
94
|
+
end
|
|
95
|
+
|
|
89
96
|
# @return [Array<String>] The schema's types, inferred from enum and const values without a type
|
|
90
97
|
def types(value_schema)
|
|
91
98
|
if value_schema.key?("type")
|
|
@@ -102,11 +109,11 @@ module Verquest
|
|
|
102
109
|
# @return [Object] The value converted to the type or NO_MATCH
|
|
103
110
|
def coerce_type(type, value, value_schema, path)
|
|
104
111
|
case type
|
|
105
|
-
when "integer" then coerce_integer(value)
|
|
106
|
-
when "number" then coerce_number(value)
|
|
112
|
+
when "integer" then coerce_integer(value, value_schema, path)
|
|
113
|
+
when "number" then coerce_number(value, value_schema, path)
|
|
107
114
|
when "boolean" then coerce_boolean(value)
|
|
108
115
|
when "null" then (value.nil? || value == "") ? nil : NO_MATCH
|
|
109
|
-
when "string" then coerce_string(value, value_schema)
|
|
116
|
+
when "string" then coerce_string(value, value_schema, path)
|
|
110
117
|
when "array" then coerce_array(value, value_schema, path)
|
|
111
118
|
when "object" then value.is_a?(::Hash) ? coerce_properties(value, value_schema, path) : NO_MATCH
|
|
112
119
|
else NO_MATCH
|
|
@@ -114,7 +121,7 @@ module Verquest
|
|
|
114
121
|
end
|
|
115
122
|
|
|
116
123
|
# @return [Integer, Object] The integer or NO_MATCH
|
|
117
|
-
def coerce_integer(value)
|
|
124
|
+
def coerce_integer(value, _value_schema, _path)
|
|
118
125
|
return value if value.is_a?(::Integer)
|
|
119
126
|
return NO_MATCH unless value.is_a?(::String) && INTEGER_PATTERN.match?(value)
|
|
120
127
|
|
|
@@ -122,7 +129,7 @@ module Verquest
|
|
|
122
129
|
end
|
|
123
130
|
|
|
124
131
|
# @return [Numeric, Object] The number or NO_MATCH
|
|
125
|
-
def coerce_number(value)
|
|
132
|
+
def coerce_number(value, _value_schema, _path)
|
|
126
133
|
return value if value.is_a?(::Numeric)
|
|
127
134
|
return NO_MATCH unless value.is_a?(::String) && NUMBER_PATTERN.match?(value)
|
|
128
135
|
|
|
@@ -136,14 +143,22 @@ module Verquest
|
|
|
136
143
|
BOOLEANS.fetch(value, NO_MATCH)
|
|
137
144
|
end
|
|
138
145
|
|
|
139
|
-
# A string outside the schema's enum tries the next type, so "" can still map to nil
|
|
146
|
+
# A string outside the schema's enum tries the next type, so "" can still map to nil.
|
|
147
|
+
# On a case-insensitive path, a string matching an enum value case-insensitively is that value.
|
|
140
148
|
#
|
|
141
|
-
# @return [String, Object] The string or NO_MATCH
|
|
142
|
-
def coerce_string(value, value_schema)
|
|
149
|
+
# @return [String, Object] The string, the declared value or NO_MATCH
|
|
150
|
+
def coerce_string(value, value_schema, path)
|
|
143
151
|
return NO_MATCH unless value.is_a?(::String)
|
|
144
|
-
return
|
|
152
|
+
return value unless value_schema.key?("enum")
|
|
153
|
+
return value if value_schema["enum"].include?(value)
|
|
154
|
+
return NO_MATCH unless case_insensitive?(path)
|
|
155
|
+
|
|
156
|
+
enum_matches(value, value_schema).first || NO_MATCH
|
|
157
|
+
end
|
|
145
158
|
|
|
146
|
-
|
|
159
|
+
# @return [Array<String>] The schema's string enum values matching the value case-insensitively
|
|
160
|
+
def enum_matches(value, value_schema)
|
|
161
|
+
value_schema["enum"].select { |declared| declared.is_a?(::String) && declared.casecmp?(value) }
|
|
147
162
|
end
|
|
148
163
|
|
|
149
164
|
# Splits a string by the array's explode value and converts each item;
|
|
@@ -152,8 +167,8 @@ module Verquest
|
|
|
152
167
|
items = split(value, path)
|
|
153
168
|
return items if items.equal?(NO_MATCH) || !array_schema.key?("items")
|
|
154
169
|
|
|
155
|
-
items.map do |item|
|
|
156
|
-
coerced = coerce(item, array_schema["items"], path)
|
|
170
|
+
items.each_with_index.map do |item, index|
|
|
171
|
+
coerced = coerce(item, array_schema["items"], path + [index.to_s])
|
|
157
172
|
coerced.equal?(NO_MATCH) ? item : coerced
|
|
158
173
|
end
|
|
159
174
|
end
|
|
@@ -170,6 +185,13 @@ module Verquest
|
|
|
170
185
|
def explode?(path)
|
|
171
186
|
explodes.fetch(path) { Verquest.configuration.query_array_explode }
|
|
172
187
|
end
|
|
188
|
+
|
|
189
|
+
# The path of an array item ends with its index, so an item is case-insensitive when its array is
|
|
190
|
+
#
|
|
191
|
+
# @return [Boolean] Whether the value at the path matches its enum case-insensitively
|
|
192
|
+
def case_insensitive?(path)
|
|
193
|
+
case_insensitive.include?(path) || case_insensitive.include?(path[...-1])
|
|
194
|
+
end
|
|
173
195
|
end
|
|
174
196
|
end
|
|
175
197
|
end
|
|
@@ -7,11 +7,14 @@ module Verquest
|
|
|
7
7
|
# Query parameters are scalars, arrays of scalars, or one level of objects
|
|
8
8
|
# sent as `deepObject` (`filter[status]=open`). Path parameters are required
|
|
9
9
|
# scalars. Anything else, and oneOf properties, raise when the version is
|
|
10
|
-
# prepared
|
|
10
|
+
# prepared, as does `case_insensitive` on a property without string enum
|
|
11
|
+
# values, or with values equal case-insensitively.
|
|
11
12
|
class DefinitionCheck
|
|
12
13
|
# @param version [Verquest::Version] The query or path version to check
|
|
13
|
-
|
|
14
|
+
# @param query_options [Hash{Array<String> => Hash}] The version's query options, by path
|
|
15
|
+
def initialize(version, query_options: {})
|
|
14
16
|
@version = version
|
|
17
|
+
@query_options = query_options
|
|
15
18
|
end
|
|
16
19
|
|
|
17
20
|
# @return [void]
|
|
@@ -26,11 +29,13 @@ module Verquest
|
|
|
26
29
|
version.validation_schema.fetch("properties", {}).each do |name, property_schema|
|
|
27
30
|
(version.location == :path) ? check_path(property_schema, name) : check(property_schema, [name], nested: false)
|
|
28
31
|
end
|
|
32
|
+
|
|
33
|
+
query_options.each { |path, options| check_case_insensitive(path) if options[:case_insensitive] }
|
|
29
34
|
end
|
|
30
35
|
|
|
31
36
|
private
|
|
32
37
|
|
|
33
|
-
attr_reader :version
|
|
38
|
+
attr_reader :version, :query_options
|
|
34
39
|
|
|
35
40
|
# Rejects path parameters that aren't required scalars
|
|
36
41
|
#
|
|
@@ -77,6 +82,36 @@ module Verquest
|
|
|
77
82
|
reject(path, "an array of objects or arrays") if item_types.intersect?(%w[object array])
|
|
78
83
|
end
|
|
79
84
|
|
|
85
|
+
# Rejects case_insensitive on a property without string enum values (for an array, its items'),
|
|
86
|
+
# or with two values equal case-insensitively
|
|
87
|
+
#
|
|
88
|
+
# @return [void]
|
|
89
|
+
# @raise [Verquest::DefinitionError] If the property can't match case-insensitively
|
|
90
|
+
def check_case_insensitive(path)
|
|
91
|
+
label = "Property '#{path.join("/")}' in version '#{version.name}' is case_insensitive"
|
|
92
|
+
strings = enum_strings(property_schemas(path))
|
|
93
|
+
raise DefinitionError, "#{label}, which needs string enum values" if strings.empty?
|
|
94
|
+
|
|
95
|
+
first, second = strings.group_by { |value| value.downcase(:fold) }.values.find { |same| same.size > 1 }
|
|
96
|
+
raise DefinitionError, "#{label}, but its enum values #{first.inspect} and #{second.inspect} differ only by case" if first
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# @return [Array<Hash>] The schemas at the path, through anyOf and oneOf variants, with an array's items in place of the array
|
|
100
|
+
def property_schemas(path)
|
|
101
|
+
schemas = path.reduce([version.validation_schema]) do |current, name|
|
|
102
|
+
current.flat_map { |schema| variants(schema) }.filter_map { |schema| schema.dig("properties", name) }
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
schemas.flat_map { |schema| variants(schema) }.map do |schema|
|
|
106
|
+
(Array(schema["type"]).include?("array") && schema.key?("items")) ? schema["items"] : schema
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# @return [Array<String>] The distinct string enum values of the schemas and their variants
|
|
111
|
+
def enum_strings(schemas)
|
|
112
|
+
schemas.flat_map { |schema| variants(schema) }.flat_map { |schema| Array(schema["enum"]) }.grep(::String).uniq
|
|
113
|
+
end
|
|
114
|
+
|
|
80
115
|
# @return [Array<Hash>] The schema and its anyOf and oneOf variants
|
|
81
116
|
def variants(schema)
|
|
82
117
|
[schema, *schema.values_at("anyOf", "oneOf").compact.flatten]
|
|
@@ -0,0 +1,139 @@
|
|
|
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
|
+
# @param case_insensitive [Set<Array<String>>] Paths of properties matching their enum case-insensitively
|
|
22
|
+
def initialize(schema:, explodes:, required:, on_correction:, case_insensitive: Set.new)
|
|
23
|
+
super(schema:, explodes:, case_insensitive:)
|
|
24
|
+
@required = required
|
|
25
|
+
@on_correction = on_correction
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
attr_reader :required, :on_correction
|
|
31
|
+
|
|
32
|
+
# Removes blank root values before converting the rest; a required property is kept, so it
|
|
33
|
+
# fails validation with its own error
|
|
34
|
+
#
|
|
35
|
+
# @return [Hash] A copy of the hash without blank root values, with each known property converted
|
|
36
|
+
def coerce_properties(hash, object_schema, path)
|
|
37
|
+
return super unless path.empty?
|
|
38
|
+
|
|
39
|
+
properties = object_schema.fetch("properties", {})
|
|
40
|
+
blank = hash.keys.select { |key| removable_blank?(key.to_s, hash[key], properties[key.to_s]) }
|
|
41
|
+
blank.each { |key| correct(path + [key.to_s], :blank, hash[key], nil) }
|
|
42
|
+
|
|
43
|
+
super(hash.except(*blank), object_schema, path)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# @return [Boolean] Whether the value is a blank string of a property that isn't required and rejects it
|
|
47
|
+
def removable_blank?(name, value, property_schema)
|
|
48
|
+
!required.include?(name) && blank_property?(value, property_schema)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# @return [Boolean] Whether the value is a blank string that the known property rejects
|
|
52
|
+
def blank_property?(value, property_schema)
|
|
53
|
+
!property_schema.nil? && value.is_a?(::String) && value.strip.empty? && !accepts_blank?(property_schema)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Strict validation accepts a blank value for an array (it becomes []), a property that
|
|
57
|
+
# accepts null, and a string without constraints, also as an anyOf or oneOf variant,
|
|
58
|
+
# so correcting it would only add noise
|
|
59
|
+
#
|
|
60
|
+
# @return [Boolean] Whether the schema accepts a blank string as it is
|
|
61
|
+
def accepts_blank?(value_schema)
|
|
62
|
+
types = types(value_schema)
|
|
63
|
+
|
|
64
|
+
types.include?("array") || types.include?("null") || unconstrained_string?(value_schema, types) ||
|
|
65
|
+
variants(value_schema).any? { |variant| accepts_blank?(variant) }
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# @return [Boolean] Whether the schema is a string without constraints a blank string fails
|
|
69
|
+
def unconstrained_string?(value_schema, types)
|
|
70
|
+
types == ["string"] && (value_schema.keys & %w[enum format pattern minLength const]).empty?
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# @return [Integer, Object] The integer, capped at the schema's maximum, or NO_MATCH
|
|
74
|
+
def coerce_integer(value, value_schema, path)
|
|
75
|
+
capped(super, value, value_schema, path)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# @return [Numeric, Object] The number, capped at the schema's maximum, or NO_MATCH
|
|
79
|
+
def coerce_number(value, value_schema, path)
|
|
80
|
+
capped(super, value, value_schema, path)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# @return [Numeric, Object] The schema's maximum when the coerced number is above it, else the coerced value
|
|
84
|
+
def capped(coerced, value, value_schema, path)
|
|
85
|
+
maximum = value_schema["maximum"]
|
|
86
|
+
return coerced if coerced.equal?(NO_MATCH) || maximum.nil? || coerced <= maximum
|
|
87
|
+
|
|
88
|
+
correct(path, :maximum, value, maximum)
|
|
89
|
+
maximum
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# Matches an enum value case-insensitively when exactly one declared value matches,
|
|
93
|
+
# and gives a date-time an offset
|
|
94
|
+
#
|
|
95
|
+
# @return [String, Object] The string, the declared value, the date-time with an offset or NO_MATCH
|
|
96
|
+
def coerce_string(value, value_schema, path)
|
|
97
|
+
coerced = super
|
|
98
|
+
return coerced unless value.is_a?(::String)
|
|
99
|
+
return enum_case(value, value_schema, path) if coerced.equal?(NO_MATCH) && value_schema.key?("enum")
|
|
100
|
+
return with_offset(coerced, path) if value_schema["format"] == "date-time"
|
|
101
|
+
|
|
102
|
+
coerced
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# @return [String, Object] The only declared value matching case-insensitively or NO_MATCH
|
|
106
|
+
def enum_case(value, value_schema, path)
|
|
107
|
+
matches = enum_matches(value, value_schema)
|
|
108
|
+
return NO_MATCH unless matches.one?
|
|
109
|
+
|
|
110
|
+
correct(path, :enum_case, value, matches.first)
|
|
111
|
+
matches.first
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# Adds "Z" to a date-time without an offset, and a colon to a "+HHMM" offset, with "T"
|
|
115
|
+
# between the date and the time
|
|
116
|
+
#
|
|
117
|
+
# @return [String] The date-time, corrected when it matches DATE_TIME_TO_FIX
|
|
118
|
+
def with_offset(value, path)
|
|
119
|
+
match = DATE_TIME_TO_FIX.match(value)
|
|
120
|
+
return value unless match
|
|
121
|
+
|
|
122
|
+
corrected = "#{match[:date]}T#{match[:time]}#{offset(match)}"
|
|
123
|
+
correct(path, :datetime_offset, value, corrected)
|
|
124
|
+
corrected
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# @return [String] "+HH:MM" from a "+HHMM" offset, or "Z" without an offset
|
|
128
|
+
def offset(match) = match[:hours] ? "#{match[:hours]}:#{match[:minutes]}" : "Z"
|
|
129
|
+
|
|
130
|
+
# Reports a correction at the corrected value's JSON pointer
|
|
131
|
+
#
|
|
132
|
+
# @param path [Array<String>] The value's path, with array indexes
|
|
133
|
+
# @return [void]
|
|
134
|
+
def correct(path, rule, from, to)
|
|
135
|
+
on_correction&.call(Correction.new(path: Correction.pointer(path), rule:, from:, to:))
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
end
|
|
@@ -17,7 +17,7 @@ module Verquest
|
|
|
17
17
|
DOCUMENTATION_KEYS = %w[description deprecated].freeze
|
|
18
18
|
|
|
19
19
|
# @param schema [Hash] The version's validation schema
|
|
20
|
-
# @param options [Hash{Array<String> => Hash}] The `explode:` and `examples:` of each property path
|
|
20
|
+
# @param options [Hash{Array<String> => Hash}] The `explode:` and `examples:` of each property path; other options, such as `case_insensitive:`, are ignored
|
|
21
21
|
# @param location [Symbol] `:query` or `:path`, written as `in:`
|
|
22
22
|
def initialize(schema:, options:, location: :query)
|
|
23
23
|
@schema = schema
|
data/lib/verquest/version.rb
CHANGED
|
@@ -268,9 +268,27 @@ 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,
|
|
279
|
+
on_correction:, case_insensitive: query_case_insensitive).call(params)
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
# Returns the root properties the validation schema requires, always (`required`), when another
|
|
283
|
+
# property is present (`dependentRequired`) or in a condition's branch (`then`/`else`, also in `allOf`,
|
|
284
|
+
# and any alternative of `anyOf` or `oneOf`)
|
|
285
|
+
#
|
|
286
|
+
# The list is conservative: a name stays in it even when its trigger is absent or its condition
|
|
287
|
+
# doesn't apply, so a lenient request rejects `{"per" => "0"}` instead of removing `per`.
|
|
288
|
+
#
|
|
289
|
+
# @return [Array<String>] The property names
|
|
290
|
+
def required_property_names
|
|
291
|
+
required_names(validation_schema)
|
|
274
292
|
end
|
|
275
293
|
|
|
276
294
|
# Returns OpenAPI parameter objects for this query or path version, in OpenAPI 3.0 forms under :openapi30
|
|
@@ -329,9 +347,27 @@ module Verquest
|
|
|
329
347
|
|
|
330
348
|
private
|
|
331
349
|
|
|
350
|
+
# Collects names from `then`/`else` branches and `allOf`, `anyOf` and `oneOf` entries, conservatively
|
|
351
|
+
# (a name any alternative requires counts); `if` and `not` don't require anything
|
|
352
|
+
#
|
|
353
|
+
# @param schema [Hash] An object schema, a condition's branch or an entry of one of them
|
|
354
|
+
# @return [Array<String>] The names the schema, its branches and its entries require
|
|
355
|
+
def required_names(schema)
|
|
356
|
+
names = schema.fetch("required", []) | schema.fetch("dependentRequired", {}).values.flatten
|
|
357
|
+
branches = schema.values_at("then", "else").compact + schema.values_at("allOf", "anyOf", "oneOf").compact.flatten
|
|
358
|
+
|
|
359
|
+
branches.reduce(names) { |all, branch| all | required_names(branch) }
|
|
360
|
+
end
|
|
361
|
+
|
|
332
362
|
# @return [Verquest::Query::Coercer, nil] The coercer of a query or path version
|
|
333
363
|
attr_reader :query_coercer
|
|
334
364
|
|
|
365
|
+
# @return [Hash{Array<String> => Boolean}, nil] The explode value of each array property of a query or path version, by path
|
|
366
|
+
attr_reader :query_explodes
|
|
367
|
+
|
|
368
|
+
# @return [Set<Array<String>>, nil] Paths of query or path properties matching their enum case-insensitively
|
|
369
|
+
attr_reader :query_case_insensitive
|
|
370
|
+
|
|
335
371
|
# @return [Hash{Array<String> => Hash}, nil] The parameter options of each property path of a query or path version
|
|
336
372
|
attr_reader :query_options
|
|
337
373
|
|
|
@@ -340,13 +376,14 @@ module Verquest
|
|
|
340
376
|
# @return [void]
|
|
341
377
|
# @raise [Verquest::DefinitionError] If the version uses a shape its location can't express
|
|
342
378
|
def prepare_parameters
|
|
343
|
-
Query::DefinitionCheck.new(self).call
|
|
344
|
-
|
|
345
379
|
@query_options = all_properties.each_value.with_object({}) do |property, options|
|
|
346
380
|
options.merge!(property.query_options(prefix: [], version: name))
|
|
347
381
|
end.freeze
|
|
348
|
-
|
|
349
|
-
|
|
382
|
+
Query::DefinitionCheck.new(self, query_options: @query_options).call
|
|
383
|
+
|
|
384
|
+
@query_explodes = query_options.filter_map { |path, options| [path, options[:explode]] if options.key?(:explode) }.to_h.freeze
|
|
385
|
+
@query_case_insensitive = query_options.filter_map { |path, options| path if options[:case_insensitive] }.to_set.freeze
|
|
386
|
+
@query_coercer = Query::Coercer.new(schema: validation_schema, explodes: @query_explodes, case_insensitive: @query_case_insensitive)
|
|
350
387
|
end
|
|
351
388
|
|
|
352
389
|
# 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.11.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
|