verquest 0.12.0 → 0.13.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3e0fcd7f6d608af2e076189ca2484861a076d2e24e19fb61a76795924a53b22c
4
- data.tar.gz: 59fd91a6e47c36499a212bffca49bd71fa3375bf333b91d0b162a5e54a9564d7
3
+ metadata.gz: 25a0c3e261ec4a315a1acd735469afe54c6e6b0e2e2cc03e5033ee2ccd63be1d
4
+ data.tar.gz: 74e8bc5a752eb9d284be7474c52e15769ac486f77998c9932fb14d8edd293acf
5
5
  SHA512:
6
- metadata.gz: d81a27ce31546a76107dac399cbc7baf57e02dad44eef25b00482c9b9023440ce88921e830d74ce9b991ea78d566e31a75258d1f5def238ae71a8151b34fc4e9
7
- data.tar.gz: bd463d27e048cd7d513106c6b22352bad04f5c0ef333398a38cbf26c748826a7c64778096e238f10f4b1840fbb60ccd3330e39534e6c91a439fe6abab49e5a02
6
+ metadata.gz: c7f1bd79c153e32820e441331b2038a27db8ae7ecd2350966fee8312853217e390302d78195359debfdfccc2506053e8a7921934e9e284a43315244ccfd4b80c
7
+ data.tar.gz: c32d2717ec9b72db818f34088bb0ba2e1d373e89d60bd104176e085dfecbd469c33bcd2e3e906f3c49b475392a6cb3a609c8a66a2ce5f58b8b1cc8eba809e633
data/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.13.0] - 2026-11-28
4
+
5
+ ### Added
6
+ - `process(..., reject_unknown_keys: true)` (or `config.reject_unknown_keys`) reports root keys that the version doesn't declare as validation errors of type `unknown`, when params are validated. The message lists the names the version accepts (or ends with "It accepts none."), and `details` has them as `accepted`. The keys are removed before coercion and validation, and their errors come first, in one failure with the other validation errors. Keys that `reject_other_version_keys` reports keep their `other_version` error. Keys the schema accepts (`additionalProperties` not `false`, `unevaluatedProperties` not `false` without `additionalProperties`, or matching `patternProperties`) aren't unknown, and `remove_extra_root_keys` still drops them. Symbol keys are unknown. Pointers escape `~` and `/`. The option takes precedence over `remove_extra_root_keys`, but `config.reject_unknown_keys` doesn't apply to a call that passes `remove_extra_root_keys: true`. It applies to lenient versions. A version with a root `one_of` accepts the keys of any variant.
7
+
8
+ ### Changed
9
+ - `property_names` of a version with a root `one_of` returns the names its variants declare, instead of `[nil]`, and `reject_other_version_keys` recognizes them.
10
+ - With `reject_other_version_keys`, `process` no longer returns early with only the `other_version` errors. It removes those keys and validates the rest, so the `other_version` errors come first in one failure with the unknown-key and other validation errors. Code that expects exactly the `other_version` errors may now get more.
11
+
3
12
  ## [0.12.0] - 2026-11-17
4
13
 
5
14
  ### Added
data/README.md CHANGED
@@ -133,6 +133,8 @@ error.errors
133
133
 
134
134
  Set `config.current_version` to drop the `version:` argument, or `config.validation_error_handling = :result` to get a `Verquest::Result` instead of an exception (see [Configuration](#configuration)). For query and path parameters in controllers, see [Rails and rswag](docs/rails.md).
135
135
 
136
+ With [`reject_unknown_keys`](#reject-unknown-keys) on, `params` also carries `controller`, `action`, `format` and route keys, so every request fails with an `unknown` error. Pass `request.request_parameters` or `request.query_parameters` instead, and turn off `wrap_parameters` in those controllers, as ParamsWrapper merges its wrapper key into `request_parameters`.
137
+
136
138
  ### JSON schema for OpenAPI
137
139
 
138
140
  `to_schema` returns the JSON Schema of a version for your API documentation. References stay `$ref`s:
@@ -928,7 +930,34 @@ DealsIndexQuery.process({"site_id" => "site_1"}, version: "2025-06", reject_othe
928
930
  # [{pointer: "/site_id", type: "other_version", message: "`site_id` isn't available in version 2025-06", details: nil}]
929
931
  ```
930
932
 
931
- `remove_extra_root_keys` still drops keys that no version declares, such as `utm_source`. The check works for body requests too, and runs only when `process` validates the params. `config.reject_other_version_keys` sets the default (`false`).
933
+ `process` removes those keys and still validates the rest, so the `other_version` errors come first in one failure with the other validation errors. `remove_extra_root_keys` still drops keys that no version declares, such as `utm_source`. The check works for body requests too, and runs only when `process` validates the params. `config.reject_other_version_keys` sets the default (`false`).
934
+
935
+ #### Reject unknown keys
936
+
937
+ With `reject_unknown_keys: true`, `process` reports every root key that the version doesn't accept as a validation error, and lists the names it declares:
938
+
939
+ ```ruby
940
+ DealsIndexQuery.process({"page" => "0", "colour" => "red"}, version: "2025-06", reject_unknown_keys: true)
941
+ # raises Verquest::InvalidParamsError with
942
+ # [{pointer: "/colour", type: "unknown",
943
+ # message: "`colour` isn't a parameter of version 2025-06. It accepts: page, archived, sort_direction, status, created.",
944
+ # details: {accepted: ["page", "archived", "sort_direction", "status", "created"]}},
945
+ # {pointer: "/page", ...}]
946
+ ```
947
+
948
+ - `process` removes the unknown keys before it coerces and validates the params, so the schema doesn't also report them as additional properties. The `other_version` errors come first, then the unknown-key errors, then the other validation errors, in one failure.
949
+ - A version that declares no properties ends the message with "It accepts none." and has `accepted: []`.
950
+ - With `reject_other_version_keys: true`, a key that only another version declares gets its `other_version` error instead. Without it, that key is unknown.
951
+ - A key the schema accepts isn't unknown: any key when `additionalProperties` isn't `false` (see `config.default_additional_properties`), or, without `additionalProperties`, when `unevaluatedProperties` isn't `false`, and keys that match `patternProperties`. `remove_extra_root_keys` still drops them; without it, `process` keeps and validates them. Other keywords that restrict keys, like `propertyNames`, are left to the schema's errors.
952
+ - The option takes precedence over `remove_extra_root_keys`: unknown keys are reported, not dropped. `config.reject_unknown_keys` doesn't apply to a call that passes `remove_extra_root_keys: true`, so that call still drops extra keys.
953
+ - The pointer escapes `~` and `/` in the key, as in `/a~1b` for `a/b`.
954
+ - A lenient version reports unknown keys too; it never removes them as a correction. Corrections to other params are still yielded.
955
+ - A version whose root is a `one_of` accepts the keys of any variant, and `accepted` and `property_names` list them all. A key of another variant than the one sent is left to the `oneOf` errors.
956
+ - Corrections can be yielded even when `process` then fails, for example with an unknown key.
957
+ - `process` reads String keys, as `ActionController::Parameters` gives them. A Symbol key is unknown; a declared one gets the message "`name` isn't a String key; process reads String keys only".
958
+ - In Rails, pass `request.request_parameters` or `request.query_parameters`, not `params`, and turn off `wrap_parameters` (see [Process params in a Rails controller](#process-params-in-a-rails-controller)).
959
+
960
+ The check works for body, query and path requests, and runs only when `process` validates the params. `config.reject_unknown_keys` sets the default (`false`).
932
961
 
933
962
  ### Path parameters
934
963
 
@@ -987,6 +1016,9 @@ Verquest.configure do |config|
987
1016
 
988
1017
  # Report root keys that only another version declares, see Filters
989
1018
  config.reject_other_version_keys = false # default
1019
+
1020
+ # Report root keys that the version doesn't declare, instead of removing them, see Filters
1021
+ config.reject_unknown_keys = false # default
990
1022
 
991
1023
  # Set custom version resolver
992
1024
  config.version_resolver = CustomVersionResolver # default is `Verquest::VersionResolver`
data/docs/rails.md CHANGED
@@ -66,6 +66,8 @@ module VerquestParams
66
66
  @path_params = by_location.fetch(:path, []).to_h { |declaration| process_path_param(declaration) }
67
67
  # Unknown query keys (cache busters, tracking) are dropped instead of rejected. Add
68
68
  # `reject_other_version_keys: true` to reject keys from another version, such as a newer filter.
69
+ # `reject_unknown_keys: true` rejects every undeclared key; with it, turn off `wrap_parameters`,
70
+ # which adds its wrapper key to `request_parameters`.
69
71
  # A lenient request (config.lenient_through) yields each value it corrected instead of rejecting.
70
72
  @query_params = by_location[:query]&.first&.dig(:class)&.process(request.query_parameters, remove_extra_root_keys: true) do |correction|
71
73
  Rails.logger.info("Corrected query param: #{correction.to_h.except(:from)}")
@@ -515,20 +515,65 @@ module Verquest
515
515
  "(#{schema_options.keys.join(", ")}), give it a name: or #{alternative}"
516
516
  end
517
517
 
518
- # Errors for root keys that only another version declares
518
+ # Removes the root keys that only another version declares, with an error for each
519
519
  #
520
520
  # @param params [Hash] The params being processed
521
521
  # @param version [Verquest::Version] The resolved version
522
- # @return [Array<Hash>] One error per key, in the format of validation errors
523
- def other_version_key_errors(params, version)
524
- other_names = versions.property_names - version.all_properties.keys
522
+ # @return [Array(Hash, Array<Hash>)] The params without those keys, and one error per key
523
+ def without_other_version_keys(params, version)
524
+ other_names = versions.property_names - version.root_property_names
525
+ found = params.keys.select { |key| other_names.include?(key.to_s) }
526
+ return [params, []] if found.empty?
527
+
528
+ errors = found.map { |key| root_key_error(key.to_s, "other_version", "`#{key}` isn't available in version #{version.name}") }
529
+ [params.except(*found), errors]
530
+ end
525
531
 
526
- params.keys.filter_map do |key|
527
- key = key.to_s
528
- next unless other_names.include?(key)
532
+ # Removes the root keys the version doesn't accept, with an error for each
533
+ #
534
+ # A key is accepted when it's a declared String key, or when the schema accepts it
535
+ # (additionalProperties isn't false, or it matches patternProperties). Keys only another
536
+ # version declares stay unknown here; with reject_other_version_keys,
537
+ # {#without_other_version_keys} removes and reports them before this runs.
538
+ #
539
+ # @param params [Hash] The params being processed
540
+ # @param version [Verquest::Version] The resolved version
541
+ # @param remove_accepted [Boolean] Whether to also remove undeclared keys the schema accepts, as remove_extra_root_keys does
542
+ # @return [Array(Hash, Array<Hash>)] The params without the removed keys, and one error per unknown key
543
+ def without_unknown_keys(params, version, remove_accepted:)
544
+ undeclared = params.keys.reject { |key| key.is_a?(String) && version.declares_root_key?(key) }
545
+ return [params, []] if undeclared.empty?
546
+
547
+ unknown = undeclared.reject { |key| key.is_a?(String) && version.accepts_root_key?(key) }
548
+ [params.except(*(remove_accepted ? undeclared : unknown)), unknown.map { |key| unknown_key_error(key, version) }]
549
+ end
529
550
 
530
- {pointer: "/#{key}", type: "other_version", message: "`#{key}` isn't available in version #{version.name}", details: nil}
551
+ # The error for a root key the version doesn't accept
552
+ #
553
+ # @param key [String, Symbol, Object] The unknown key
554
+ # @param version [Verquest::Version] The resolved version
555
+ # @return [Hash] The error, in the format of validation errors
556
+ def unknown_key_error(key, version)
557
+ accepted = version.root_property_names
558
+ name = key.to_s
559
+ message = if !key.is_a?(String) && accepted.include?(name)
560
+ "`#{name}` isn't a String key; process reads String keys only"
561
+ else
562
+ "`#{name}` isn't a parameter of version #{version.name}. #{accepted.empty? ? "It accepts none." : "It accepts: #{accepted.join(", ")}."}"
531
563
  end
564
+
565
+ root_key_error(name, "unknown", message, {accepted:})
566
+ end
567
+
568
+ # Builds the validation error of a root key
569
+ #
570
+ # @param name [String] The root key
571
+ # @param type [String] The error type
572
+ # @param message [String] The error message
573
+ # @param details [Hash, nil] The error details
574
+ # @return [Hash] The error, with an escaped JSON pointer
575
+ def root_key_error(name, type, message, details = nil)
576
+ {pointer: Correction.pointer([name]), type:, message:, details:}
532
577
  end
533
578
 
534
579
  # Reports failed params according to the validation_error_handling setting
@@ -16,13 +16,17 @@ 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 reject_unknown_keys [Boolean, nil] Whether to report root keys the version doesn't accept as validation errors (only when validating;
20
+ # for a root one_of, keys no variant accepts), instead of removing them, defaults to configuration setting unless remove_extra_root_keys is true
19
21
  # @param lenient [Boolean, nil] Whether a query or path request corrects values instead of rejecting them, defaults to
20
22
  # comparing the requested version with the configuration's lenient_through
21
23
  # @yieldparam correction [Verquest::Correction] Each correction a lenient request made
22
24
  # @return [Verquest::Result, Hash] When validation_error_handling is :result, returns a Success result with mapped params or Failure result with validation errors.
23
25
  # When validation_error_handling is :raise, returns mapped params directly or raises InvalidParamsError with validation errors.
24
- def process(params, version: nil, validate: nil, remove_extra_root_keys: nil, reject_other_version_keys: nil, lenient: nil, &on_correction)
26
+ def process(params, version: nil, validate: nil, remove_extra_root_keys: nil, reject_other_version_keys: nil, reject_unknown_keys: nil, lenient: nil, &on_correction)
25
27
  validate = Verquest.configuration.validate_params if validate.nil?
28
+ # An explicit remove_extra_root_keys: true wins over config.reject_unknown_keys
29
+ reject_unknown_keys = remove_extra_root_keys != true && Verquest.configuration.reject_unknown_keys if reject_unknown_keys.nil?
26
30
  remove_extra_root_keys = Verquest.configuration.remove_extra_root_keys if remove_extra_root_keys.nil?
27
31
  reject_other_version_keys = Verquest.configuration.reject_other_version_keys if reject_other_version_keys.nil?
28
32
 
@@ -33,11 +37,17 @@ module Verquest
33
37
  params = params.dup
34
38
  params = params.to_unsafe_h if params.respond_to?(:to_unsafe_h)
35
39
 
36
- if validate && reject_other_version_keys && (other_version_errors = other_version_key_errors(params, version_class)).any?
37
- return validation_failure(other_version_errors)
40
+ key_errors = []
41
+ if validate && reject_other_version_keys
42
+ params, key_errors = without_other_version_keys(params, version_class)
38
43
  end
39
44
 
40
- params = params.slice(*version_class.all_properties.keys) if remove_extra_root_keys && !version_class.combination?
45
+ if validate && reject_unknown_keys
46
+ params, unknown_errors = without_unknown_keys(params, version_class, remove_accepted: remove_extra_root_keys && !version_class.combination?)
47
+ key_errors += unknown_errors
48
+ elsif remove_extra_root_keys && !version_class.combination?
49
+ params = params.slice(*version_class.all_properties.keys)
50
+ end
41
51
  sent = params
42
52
  corrections = []
43
53
  params = version_class.coerce_params(params, lenient:) { |correction| corrections << correction }
@@ -45,6 +55,7 @@ module Verquest
45
55
 
46
56
  params, errors, invalid = validate ? validated_params(params, version_class, lenient:) : [params, [], []]
47
57
  with_invalid_corrections(corrections, invalid, sent).each(&on_correction) if on_correction
58
+ errors = key_errors + errors
48
59
  return validation_failure(errors) if errors.any?
49
60
 
50
61
  mapped_result(params, version_class)
@@ -56,9 +67,10 @@ module Verquest
56
67
  # DealsIndexQuery.property_names(version: "2025-06") # => ["page", "archived", "sort_direction", "status", "created"]
57
68
  #
58
69
  # @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
70
+ # @return [Array<String>] Root property names in declaration order, inherited ones included; for a root one_of,
71
+ # the names of its variants
60
72
  def property_names(version: nil)
61
- resolve(version).all_properties.keys
73
+ resolve(version).root_property_names
62
74
  end
63
75
 
64
76
  # Returns the names of the filters the version declares
@@ -70,12 +70,18 @@ module Verquest
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
72
  #
73
+ # @!attribute [rw] reject_unknown_keys
74
+ # Controls whether `process` reports root keys that the resolved version doesn't accept, when it validates params.
75
+ # It takes precedence over remove_extra_root_keys, except in a call that passes remove_extra_root_keys: true.
76
+ # A version with a root one_of accepts the keys of any of its variants
77
+ # @return [Boolean] true to report them as validation errors, false to leave them to remove_extra_root_keys (default)
78
+ #
73
79
  # @!attribute [rw] lenient_through
74
80
  # The last version whose query and path requests are corrected instead of rejected (see {Verquest::Correction})
75
81
  # @return [String, nil] A version name; nil (default) makes every request strict
76
82
  attr_accessor :validate_params, :json_schema_version, :validation_error_handling,
77
83
  :remove_extra_root_keys, :insert_property_defaults, :default_additional_properties,
78
- :query_array_explode, :reject_other_version_keys, :lenient_through
84
+ :query_array_explode, :reject_other_version_keys, :reject_unknown_keys, :lenient_through
79
85
 
80
86
  # @!attribute [r] current_version
81
87
  # A callable object that returns the current API version to use when not explicitly specified
@@ -104,6 +110,7 @@ module Verquest
104
110
  @default_additional_properties = false
105
111
  @query_array_explode = true
106
112
  @reject_other_version_keys = false
113
+ @reject_unknown_keys = false
107
114
  @lenient_through = nil
108
115
  end
109
116
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Verquest
4
- GEM_VERSION = "0.12.0"
4
+ GEM_VERSION = "0.13.0"
5
5
  end
@@ -125,6 +125,36 @@ module Verquest
125
125
  @all_properties || super
126
126
  end
127
127
 
128
+ # Returns the names of the root properties, as clients send them
129
+ #
130
+ # For a root one_of, the names any variant declares.
131
+ #
132
+ # @return [Array<String>] Frozen root property names in declaration order
133
+ def root_property_names
134
+ @root_property_names || all_properties.keys.freeze
135
+ end
136
+
137
+ # Checks whether the schema accepts a root key, declared or not
138
+ #
139
+ # A key the version doesn't declare is accepted when additionalProperties
140
+ # isn't false (or, without it, unevaluatedProperties isn't false) or the key
141
+ # matches a patternProperties pattern. For a root
142
+ # one_of, a key is accepted when any variant accepts it.
143
+ #
144
+ # @param key [String] A root key from the params
145
+ # @return [Boolean] true when the schema accepts the key
146
+ def accepts_root_key?(key)
147
+ declares_root_key?(key) || @open_root || @root_key_patterns.any? { |pattern| pattern.match?(key) }
148
+ end
149
+
150
+ # Checks whether the version declares a root key
151
+ #
152
+ # @param key [String] A root key from the params
153
+ # @return [Boolean] true when the key is one of {#root_property_names}
154
+ def declares_root_key?(key)
155
+ @root_property_set.include?(key)
156
+ end
157
+
128
158
  # Copy properties and conditions from another version
129
159
  #
130
160
  # @param version [Verquest::Version] The version to copy properties from
@@ -169,6 +199,7 @@ module Verquest
169
199
  prepare_external_mapping
170
200
  end
171
201
 
202
+ prepare_root_keys
172
203
  prepare_parameters if parameters?
173
204
  @property_defaults = property_defaults?(validation_schema)
174
205
  @transformer = Transformer.new(layout: transformer_layout)
@@ -347,6 +378,61 @@ module Verquest
347
378
 
348
379
  private
349
380
 
381
+ # Stores which root keys the schema accepts, for {#accepts_root_key?}
382
+ #
383
+ # A root one_of takes the union of its variants, read from the validation schema:
384
+ # their property names and patterns, and open when any variant is open or isn't
385
+ # an object schema with properties.
386
+ #
387
+ # @return [void]
388
+ def prepare_root_keys
389
+ roots = combination? ? root_variant_schemas : [schema_options]
390
+ names = combination? ? roots.flat_map { |root| root.fetch("properties", {}).keys }.uniq : all_properties.keys
391
+
392
+ @root_property_names = names.freeze
393
+ @root_property_set = names.to_set.freeze
394
+ @open_root = roots.any? { |root| open_object_schema?(root) || (combination? && !root.key?("properties")) }
395
+ @root_key_patterns = roots.flat_map { |root| root.fetch("patternProperties", {}).keys }.uniq
396
+ .map { |pattern| Regexp.new(pattern.to_s) }.freeze
397
+ end
398
+
399
+ # Returns the object schemas of a root one_of's variants
400
+ #
401
+ # Follows local $refs (a discriminated one_of keeps its variants in definitions)
402
+ # and unwraps the anyOf of a nullable variant, leaving out its null schema.
403
+ #
404
+ # @return [Array<Hash>] The variants' object schemas
405
+ def root_variant_schemas
406
+ validation_schema.fetch("oneOf", []).flat_map { |variant| object_schemas(variant) }
407
+ end
408
+
409
+ # @param schema [Hash] A variant schema, or an entry of its anyOf
410
+ # @return [Array<Hash>] The object schemas it stands for
411
+ def object_schemas(schema)
412
+ if (ref = schema["$ref"])&.start_with?("#/")
413
+ object_schemas(validation_schema.dig(*ref.delete_prefix("#/").split("/")) || {})
414
+ elsif schema.key?("anyOf") && !schema.key?("properties")
415
+ schema["anyOf"].flat_map { |entry| object_schemas(entry) }
416
+ elsif schema["type"] == "null"
417
+ []
418
+ else
419
+ [schema]
420
+ end
421
+ end
422
+
423
+ # Checks whether an object schema accepts keys it doesn't declare
424
+ #
425
+ # additionalProperties decides when it's set, as it evaluates every key;
426
+ # without it, unevaluatedProperties does.
427
+ #
428
+ # @param schema [Hash] An object schema
429
+ # @return [Boolean] true when undeclared keys are accepted
430
+ def open_object_schema?(schema)
431
+ return schema["additionalProperties"] != false if schema.key?("additionalProperties")
432
+
433
+ schema["unevaluatedProperties"] != false
434
+ end
435
+
350
436
  # Collects names from `then`/`else` branches and `allOf`, `anyOf` and `oneOf` entries, conservatively
351
437
  # (a name any alternative requires counts); `if` and `not` don't require anything
352
438
  #
@@ -66,11 +66,13 @@ module Verquest
66
66
 
67
67
  # Returns the root property names declared by any version
68
68
  #
69
+ # A version with a root one_of contributes the names of its variants.
70
+ #
69
71
  # Cached until the next version is added.
70
72
  #
71
73
  # @return [Array<String>] The property names
72
74
  def property_names
73
- @property_names ||= versions.values.flat_map { |version| version.all_properties.keys }.uniq.freeze
75
+ @property_names ||= versions.values.flat_map(&:root_property_names).uniq.freeze
74
76
  end
75
77
 
76
78
  private
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.12.0
4
+ version: 0.13.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Petr Hlavicka