verquest 0.7.0 → 0.7.1
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 +27 -1
- data/lib/verquest/base/private_class_methods.rb +53 -6
- data/lib/verquest/conditions/condition.rb +10 -15
- data/lib/verquest/conditions/rule_set.rb +16 -0
- data/lib/verquest/conditions/rules/absent.rb +16 -0
- data/lib/verquest/conditions/rules/any.rb +16 -0
- data/lib/verquest/conditions/rules/none.rb +16 -0
- data/lib/verquest/conditions/rules/present.rb +16 -0
- data/lib/verquest/conditions/rules/property_rule.rb +18 -0
- data/lib/verquest/conditions/variant_shape.rb +39 -0
- data/lib/verquest/configuration.rb +5 -5
- data/lib/verquest/gem_version.rb +1 -1
- data/lib/verquest/properties/collection.rb +12 -1
- data/lib/verquest/properties/one_of.rb +4 -1
- data/lib/verquest/properties/reference.rb +13 -0
- data/lib/verquest/supported_versions.rb +73 -0
- data/lib/verquest/versions.rb +7 -0
- data/lib/verquest.rb +2 -1
- 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: a8cd5618a10d4cd2445bfaa83544c67b1b2875aaf4c2a81f4f379c58b542fa48
|
|
4
|
+
data.tar.gz: d6f5d4e461d21ea2ddbe00d0a5e1ecae6846807359b39d4af0deb17b53c353a5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 47f7a89e151551c01db83182d160af65ce0087eac9c9b98968f166c0bd6583fbd04308b8cd12dbf0a7fa037802a2858e464763002b74d12ae7675ec47ab928fd
|
|
7
|
+
data.tar.gz: c9fea3cbebb730cd85757067b9f5b1ca7aa31893c61e617e05d6e1757dea57cd226a686cc8c43949c7bb5ca49babb6ee2b6546875a82e1286859fe6466da5bda
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
## [Unreleased]
|
|
2
2
|
|
|
3
|
+
## [0.7.1] - 2026-11-06
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
- `reference from:` and `collection item:` accept classes that only implement `to_ref` and `to_validation_schema` (such as versponse responses). Their schema is referenced and their value passes through unchanged. They can't be `one_of` variants.
|
|
7
|
+
- `supported_versions min:, max:` limits the versions a request serves (both inclusive, both optional; `min` defaults to the first declared version). Resolving a version outside the range raises `Verquest::VersionNotFoundError`, so `process`, schemas, mappings and references respect it. Only the requested version is checked, so `min: "preview"` can be served by an earlier block. `min` after `max`, or a `max` before the first declared version, raises `Verquest::DefinitionError`. Versions are compared with `config.version_resolver`, and subclasses inherit the range. Known limit: references are checked against the referencing class's declared version, not the requested one, so a parent must declare a later block, or its own range, to stop serving a retired reference.
|
|
8
|
+
|
|
9
|
+
### Fixed
|
|
10
|
+
- `collection item:` validation referenced an undefined `type` variable.
|
|
11
|
+
- `version ..., inherit: "<version>"` copied the previous version instead of the named one.
|
|
12
|
+
- `config.custom_field_types=` no longer modifies the Hash it is given.
|
|
13
|
+
|
|
14
|
+
### Changed
|
|
15
|
+
- `document_as: :one_of` leaves the `given` rules out of a variant when the variant's required and forbidden properties already decide them, including `absent`, `any` and `none`. Before, only `present` (in the `then` variant) and a required property the `else` variant forbids were left out, so `given { absent :percentage }` added `"percentage": false` and a `not` to the variants. The documented schema accepts the same payloads as before.
|
|
16
|
+
|
|
3
17
|
## [0.7.0] - 2026-11-05
|
|
4
18
|
|
|
5
19
|
### Added
|
data/README.md
CHANGED
|
@@ -502,7 +502,7 @@ end
|
|
|
502
502
|
}
|
|
503
503
|
```
|
|
504
504
|
|
|
505
|
-
- Each variant lists the version's properties without the ones only the other branch declares, and adds its branch's rules. The `then` variant also holds the `given` rules, and the `else` variant `not` the `given` rules
|
|
505
|
+
- Each variant lists the version's properties without the ones only the other branch declares, and adds its branch's rules. The `then` variant also holds the `given` rules, and the `else` variant `not` the `given` rules. A variant leaves them out when its required and forbidden properties already decide them, so `given { absent :percentage }` or `given { any { present :percentages; absent :percentage } }` documents as cleanly as `given { present :percentages }` when the branches require those properties. Values aren't compared, so `equals`, `value_in` and `satisfies` stay in the `then` variant. Both schemas accept the same payloads. When a rule clashes with a property's own schema (for example `given { absent :note }` against a declared `note`, or `satisfies :age, minimum: 18` against `minimum: 0`), the variant keeps both by adding the rule under `allOf`.
|
|
506
506
|
- Validation still uses the `if` / `then` / `else` schema, so validation errors point at those rules, not at a `oneOf` variant.
|
|
507
507
|
- When `additionalProperties` isn't `false`, the other branch's properties are forbidden with `false` in each variant.
|
|
508
508
|
- Only for a condition in a version, and it must be the only condition in that version. The version's `description` stays at the root of the schema.
|
|
@@ -685,6 +685,32 @@ This is used across all referenced requests, so if you have a `UserRequest` that
|
|
|
685
685
|
|
|
686
686
|
The goal here is to avoid redefining the same request structure in multiple versions when there are no changes, and to facilitate the easy evolution of API requests over time. When a new API version is created and there are no changes to the requests, you don't need to update anything.
|
|
687
687
|
|
|
688
|
+
#### Supported versions
|
|
689
|
+
|
|
690
|
+
`supported_versions` limits the versions a request serves. Both bounds are inclusive and optional:
|
|
691
|
+
|
|
692
|
+
```ruby
|
|
693
|
+
class OverlockRequest < Verquest::Base
|
|
694
|
+
supported_versions min: "2025-09", max: "2026-03"
|
|
695
|
+
|
|
696
|
+
version "2025-09" do
|
|
697
|
+
field :reason, type: :string, required: true
|
|
698
|
+
end
|
|
699
|
+
end
|
|
700
|
+
|
|
701
|
+
OverlockRequest.process(params, version: "2025-12") # => uses the defined version "2025-09"
|
|
702
|
+
OverlockRequest.process(params, version: "2026-06") # => raises Verquest::VersionNotFoundError
|
|
703
|
+
```
|
|
704
|
+
|
|
705
|
+
- `min` defaults to the first declared version. Set it when the request should appear later than its first `version` block. The range applies to the requested version, not to the blocks: with `supported_versions min: "preview"` and a single `version "2025-09"` block, `"preview"` is served by the 2025-09 block and `"2025-09"` raises.
|
|
706
|
+
- `max` defaults to no upper bound.
|
|
707
|
+
- A version outside the range raises `Verquest::VersionNotFoundError`, the same error as a version older than every declared one. `process`, `to_schema`, `to_validation_schema`, `mapping` and references from other requests all respect it. A request that references `OverlockRequest` from a version outside its range raises when that version is defined.
|
|
708
|
+
- Known limit: references are checked against the referencing class's declared version, not the requested one. If `ParentRequest` declares only `version "2025-06"` with `reference :overlock, from: OverlockRequest`, then `ParentRequest.process(params, version: "2026-06")` resolves to the 2025-06 block and still serves the reference. To stop serving a retired reference, the parent must declare a later block without it, or its own `supported_versions`.
|
|
709
|
+
- Versions are compared with `config.version_resolver`. With the default resolver they compare as strings, so `"preview"` sorts after dated versions.
|
|
710
|
+
- `min` after `max` raises `Verquest::DefinitionError` when declared. A `max` before the first declared version would leave nothing to serve, so it raises `Verquest::DefinitionError` the first time the request resolves a version.
|
|
711
|
+
- Declare it before the first `version` block. Subclasses inherit the range and can declare their own. `supported_versions` with no arguments removes an inherited range.
|
|
712
|
+
- `inherit: "<version>"` copies any declared version, even one outside the range.
|
|
713
|
+
|
|
688
714
|
### Mapping request structure
|
|
689
715
|
|
|
690
716
|
Verquest's mapping system allows transforming external API request structures into your internal application data structures.
|
|
@@ -14,6 +14,8 @@ module Verquest
|
|
|
14
14
|
# @param version [String, nil] The specific version to resolve
|
|
15
15
|
# @return [Verquest::Version] The resolved version object
|
|
16
16
|
# @raise [ArgumentError] If no version is provided and no current_version is configured
|
|
17
|
+
# @raise [Verquest::VersionNotFoundError] If no version matches, or supported_versions leaves the version out
|
|
18
|
+
# @raise [Verquest::DefinitionError] If supported_versions max sorts before every declared version
|
|
17
19
|
def resolve(version)
|
|
18
20
|
if version.nil? && Verquest.configuration.current_version
|
|
19
21
|
version = instance_exec(&Verquest.configuration.current_version)
|
|
@@ -21,9 +23,19 @@ module Verquest
|
|
|
21
23
|
raise ArgumentError, "Version must be provided or set by Verquest.configuration.current_version"
|
|
22
24
|
end
|
|
23
25
|
|
|
26
|
+
check_supported_version!(version)
|
|
24
27
|
versions.resolve(version)
|
|
25
28
|
end
|
|
26
29
|
|
|
30
|
+
# Returns the range declared with supported_versions on this class or its closest superclass
|
|
31
|
+
#
|
|
32
|
+
# @return [Verquest::SupportedVersions, nil] The range, or nil when the request serves every declared version
|
|
33
|
+
def supported_version_range
|
|
34
|
+
return @supported_versions if defined?(@supported_versions)
|
|
35
|
+
|
|
36
|
+
superclass.supported_version_range if superclass.respond_to?(:supported_version_range)
|
|
37
|
+
end
|
|
38
|
+
|
|
27
39
|
private
|
|
28
40
|
|
|
29
41
|
# @return [Verquest::Version, Verquest::Properties::Object] The current scope being defined
|
|
@@ -40,6 +52,41 @@ module Verquest
|
|
|
40
52
|
@versions ||= Versions.new
|
|
41
53
|
end
|
|
42
54
|
|
|
55
|
+
# Limits the versions this request serves
|
|
56
|
+
#
|
|
57
|
+
# Resolving a version outside the range raises {Verquest::VersionNotFoundError}. Subclasses
|
|
58
|
+
# inherit the range; calling it without arguments removes an inherited one.
|
|
59
|
+
#
|
|
60
|
+
# @example
|
|
61
|
+
# supported_versions min: "2025-09", max: "2026-03"
|
|
62
|
+
#
|
|
63
|
+
# @param min [String, nil] The first supported version (inclusive), defaults to the first declared version
|
|
64
|
+
# @param max [String, nil] The last supported version (inclusive), defaults to no upper bound
|
|
65
|
+
# @return [void]
|
|
66
|
+
# @raise [Verquest::DefinitionError] If min sorts after max, or a version is already defined
|
|
67
|
+
def supported_versions(min: nil, max: nil)
|
|
68
|
+
raise DefinitionError, "supported_versions must be set before the first version" if current_scope
|
|
69
|
+
|
|
70
|
+
@supported_versions = (min.nil? && max.nil?) ? nil : SupportedVersions.new(min:, max:)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Raises unless supported_versions covers the requested version (declared blocks may lie outside it)
|
|
74
|
+
#
|
|
75
|
+
# @param version [String] The requested version
|
|
76
|
+
# @return [void]
|
|
77
|
+
# @raise [Verquest::VersionNotFoundError] If the version is outside the range
|
|
78
|
+
# @raise [Verquest::DefinitionError] If max sorts before every declared version
|
|
79
|
+
def check_supported_version!(version)
|
|
80
|
+
range = supported_version_range
|
|
81
|
+
return unless range
|
|
82
|
+
|
|
83
|
+
declared = versions.names
|
|
84
|
+
range.check!(declared, name || inspect)
|
|
85
|
+
return if declared.empty? || range.covers?(version.to_s)
|
|
86
|
+
|
|
87
|
+
raise VersionNotFoundError, "Version '#{version}' is outside supported_versions (#{range}) of #{name || inspect}"
|
|
88
|
+
end
|
|
89
|
+
|
|
43
90
|
# Defines a new version with optional inheritance from another version
|
|
44
91
|
#
|
|
45
92
|
# @param name [String] The name/identifier of the version
|
|
@@ -51,11 +98,10 @@ module Verquest
|
|
|
51
98
|
version = Version.new(name:, location:)
|
|
52
99
|
versions.add(version)
|
|
53
100
|
|
|
54
|
-
if inherit
|
|
101
|
+
if inherit.is_a?(String)
|
|
102
|
+
version.copy_from(versions.resolve(inherit), exclude_properties:)
|
|
103
|
+
elsif inherit && @current_scope
|
|
55
104
|
version.copy_from(@current_scope, exclude_properties:)
|
|
56
|
-
elsif inherit.is_a?(String)
|
|
57
|
-
inherit_version = versions.resolve(inherit)
|
|
58
|
-
version.copy_from(inherit_version, exclude_properties:)
|
|
59
105
|
end
|
|
60
106
|
|
|
61
107
|
@default_options = {}
|
|
@@ -222,11 +268,12 @@ module Verquest
|
|
|
222
268
|
# @param schema_options [Hash] Additional schema options for the collection
|
|
223
269
|
# @yield Block executed in the context of the new collection definition
|
|
224
270
|
# @return [void]
|
|
271
|
+
# @raise [ArgumentError] without item: or a block, or when item does not respond to to_ref
|
|
225
272
|
def collection(name, item: nil, required: nil, nullable: nil, map: nil, **schema_options, &block)
|
|
226
273
|
if item.nil? && !block_given?
|
|
227
274
|
raise ArgumentError, "item must be provided or a block must be given to define the collection"
|
|
228
|
-
elsif !item.nil? && !
|
|
229
|
-
raise ArgumentError, "item must be a
|
|
275
|
+
elsif !item.nil? && !item.respond_to?(:to_ref)
|
|
276
|
+
raise ArgumentError, "item must be a Verquest::Base subclass or respond to to_ref and to_validation_schema"
|
|
230
277
|
end
|
|
231
278
|
|
|
232
279
|
camelize(schema_options)
|
|
@@ -108,9 +108,10 @@ module Verquest
|
|
|
108
108
|
#
|
|
109
109
|
# Each variant holds the scope's properties without the ones only the
|
|
110
110
|
# other branch declares, plus its branch's rules. The `then` variant also
|
|
111
|
-
# holds the `given` rules,
|
|
112
|
-
#
|
|
113
|
-
#
|
|
111
|
+
# holds the `given` rules, unless its required and forbidden properties
|
|
112
|
+
# already satisfy them, and the `else` variant their negation, unless
|
|
113
|
+
# they already rule `given` out. So exactly one variant matches every
|
|
114
|
+
# payload the `if` / `then` / `else` schema accepts.
|
|
114
115
|
#
|
|
115
116
|
# @param scope_schema [Hash] The scope schema with every property listed
|
|
116
117
|
# @return [Hash] The scope description and the `oneOf` variants
|
|
@@ -155,8 +156,12 @@ module Verquest
|
|
|
155
156
|
base = {"type" => scope_schema["type"]}.merge(branch(part).documentation, scope_schema.except("description"))
|
|
156
157
|
base["properties"] = variant_properties(scope_schema, excluded)
|
|
157
158
|
|
|
158
|
-
Conjunction.call([base, branch(part).to_schema
|
|
159
|
-
|
|
159
|
+
variant = Conjunction.call([base, branch(part).to_schema])
|
|
160
|
+
|
|
161
|
+
if part == :then
|
|
162
|
+
given.holds_in?(variant) ? variant : Conjunction.conjoin(variant, given.to_schema(in_given: true))
|
|
163
|
+
else
|
|
164
|
+
given.fails_in?(variant) ? variant : variant.merge("not" => given.to_schema(in_given: true))
|
|
160
165
|
end
|
|
161
166
|
end
|
|
162
167
|
|
|
@@ -175,16 +180,6 @@ module Verquest
|
|
|
175
180
|
properties.merge(excluded.to_h { |property_name| [property_name, false] })
|
|
176
181
|
end
|
|
177
182
|
|
|
178
|
-
# Checks whether a payload matching the variant could still match `given`
|
|
179
|
-
#
|
|
180
|
-
# @param variant [Hash] The variant schema
|
|
181
|
-
# @return [Boolean] false when the variant forbids a name `given` requires
|
|
182
|
-
def given_possible?(variant)
|
|
183
|
-
given.to_schema(in_given: true).fetch("required", []).none? do |property_name|
|
|
184
|
-
variant["properties"][property_name] == false || (!variant["properties"].key?(property_name) && variant["additionalProperties"] == false)
|
|
185
|
-
end
|
|
186
|
-
end
|
|
187
|
-
|
|
188
183
|
# @return [String] The condition name for error messages
|
|
189
184
|
def label
|
|
190
185
|
name ? "Condition '#{name}'" : "Condition"
|
|
@@ -53,6 +53,22 @@ module Verquest
|
|
|
53
53
|
def to_schema(in_given: false)
|
|
54
54
|
FragmentMerge.call(rules.map { |rule| rule.to_schema(in_given:) })
|
|
55
55
|
end
|
|
56
|
+
|
|
57
|
+
# Checks whether every payload of the variant matches all the rules
|
|
58
|
+
#
|
|
59
|
+
# @param variant [Hash] The documented oneOf variant
|
|
60
|
+
# @return [Boolean] true when every rule holds in the variant
|
|
61
|
+
def holds_in?(variant)
|
|
62
|
+
rules.all? { |rule| rule.holds_in?(variant) }
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Checks whether no payload of the variant matches all the rules
|
|
66
|
+
#
|
|
67
|
+
# @param variant [Hash] The documented oneOf variant
|
|
68
|
+
# @return [Boolean] true when a rule fails in the variant
|
|
69
|
+
def fails_in?(variant)
|
|
70
|
+
rules.any? { |rule| rule.fails_in?(variant) }
|
|
71
|
+
end
|
|
56
72
|
end
|
|
57
73
|
end
|
|
58
74
|
end
|
|
@@ -19,6 +19,22 @@ module Verquest
|
|
|
19
19
|
def to_schema(in_given: false)
|
|
20
20
|
{"properties" => property_names.to_h { |name| [name, false] }}
|
|
21
21
|
end
|
|
22
|
+
|
|
23
|
+
# Checks whether every payload of the variant matches this rule
|
|
24
|
+
#
|
|
25
|
+
# @param variant [Hash] The documented oneOf variant
|
|
26
|
+
# @return [Boolean] true when the variant forbids every named property
|
|
27
|
+
def holds_in?(variant)
|
|
28
|
+
property_names.all? { |property_name| VariantShape.forbidden?(variant, property_name) }
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Checks whether no payload of the variant matches this rule
|
|
32
|
+
#
|
|
33
|
+
# @param variant [Hash] The documented oneOf variant
|
|
34
|
+
# @return [Boolean] true when the variant requires a named property
|
|
35
|
+
def fails_in?(variant)
|
|
36
|
+
property_names.any? { |property_name| VariantShape.required?(variant, property_name) }
|
|
37
|
+
end
|
|
22
38
|
end
|
|
23
39
|
end
|
|
24
40
|
end
|
|
@@ -21,6 +21,22 @@ module Verquest
|
|
|
21
21
|
def to_schema(in_given: false)
|
|
22
22
|
{"anyOf" => alternatives(in_given:)}
|
|
23
23
|
end
|
|
24
|
+
|
|
25
|
+
# Checks whether every payload of the variant matches one of the rules
|
|
26
|
+
#
|
|
27
|
+
# @param variant [Hash] The documented oneOf variant
|
|
28
|
+
# @return [Boolean] true when a rule holds in the variant
|
|
29
|
+
def holds_in?(variant)
|
|
30
|
+
rules.any? { |rule| rule.holds_in?(variant) }
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Checks whether no payload of the variant matches any of the rules
|
|
34
|
+
#
|
|
35
|
+
# @param variant [Hash] The documented oneOf variant
|
|
36
|
+
# @return [Boolean] true when every rule fails in the variant
|
|
37
|
+
def fails_in?(variant)
|
|
38
|
+
rules.all? { |rule| rule.fails_in?(variant) }
|
|
39
|
+
end
|
|
24
40
|
end
|
|
25
41
|
end
|
|
26
42
|
end
|
|
@@ -18,6 +18,22 @@ module Verquest
|
|
|
18
18
|
def to_schema(in_given: false)
|
|
19
19
|
{"not" => {"anyOf" => alternatives(in_given:)}}
|
|
20
20
|
end
|
|
21
|
+
|
|
22
|
+
# Checks whether no payload of the variant matches any of the rules
|
|
23
|
+
#
|
|
24
|
+
# @param variant [Hash] The documented oneOf variant
|
|
25
|
+
# @return [Boolean] true when every rule fails in the variant
|
|
26
|
+
def holds_in?(variant)
|
|
27
|
+
rules.all? { |rule| rule.fails_in?(variant) }
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Checks whether every payload of the variant matches one of the rules
|
|
31
|
+
#
|
|
32
|
+
# @param variant [Hash] The documented oneOf variant
|
|
33
|
+
# @return [Boolean] true when a rule holds in the variant
|
|
34
|
+
def fails_in?(variant)
|
|
35
|
+
rules.any? { |rule| rule.holds_in?(variant) }
|
|
36
|
+
end
|
|
21
37
|
end
|
|
22
38
|
end
|
|
23
39
|
end
|
|
@@ -16,6 +16,22 @@ module Verquest
|
|
|
16
16
|
def to_schema(in_given: false)
|
|
17
17
|
{"required" => property_names}
|
|
18
18
|
end
|
|
19
|
+
|
|
20
|
+
# Checks whether every payload of the variant matches this rule
|
|
21
|
+
#
|
|
22
|
+
# @param variant [Hash] The documented oneOf variant
|
|
23
|
+
# @return [Boolean] true when the variant requires every named property
|
|
24
|
+
def holds_in?(variant)
|
|
25
|
+
property_names.all? { |property_name| VariantShape.required?(variant, property_name) }
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Checks whether no payload of the variant matches this rule
|
|
29
|
+
#
|
|
30
|
+
# @param variant [Hash] The documented oneOf variant
|
|
31
|
+
# @return [Boolean] true when the variant forbids a named property
|
|
32
|
+
def fails_in?(variant)
|
|
33
|
+
property_names.any? { |property_name| VariantShape.forbidden?(variant, property_name) }
|
|
34
|
+
end
|
|
19
35
|
end
|
|
20
36
|
end
|
|
21
37
|
end
|
|
@@ -35,6 +35,24 @@ module Verquest
|
|
|
35
35
|
in_given ? {"required" => [name]}.merge(fragment) : fragment
|
|
36
36
|
end
|
|
37
37
|
|
|
38
|
+
# Checks whether every payload of the variant matches this rule
|
|
39
|
+
#
|
|
40
|
+
# The property's value isn't compared, so this is never known.
|
|
41
|
+
#
|
|
42
|
+
# @param _variant [Hash] The documented oneOf variant
|
|
43
|
+
# @return [Boolean] Always false
|
|
44
|
+
def holds_in?(_variant)
|
|
45
|
+
false
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Checks whether no payload of the variant matches this rule (as used in `given`)
|
|
49
|
+
#
|
|
50
|
+
# @param variant [Hash] The documented oneOf variant
|
|
51
|
+
# @return [Boolean] true when the variant forbids the property, which `given` requires
|
|
52
|
+
def fails_in?(variant)
|
|
53
|
+
VariantShape.forbidden?(variant, name)
|
|
54
|
+
end
|
|
55
|
+
|
|
38
56
|
private
|
|
39
57
|
|
|
40
58
|
# @!attribute [r] name
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Verquest
|
|
4
|
+
module Conditions
|
|
5
|
+
# Reads what a documented oneOf variant decides about its properties
|
|
6
|
+
#
|
|
7
|
+
# Only the variant's top-level `required`, `properties` and
|
|
8
|
+
# `additionalProperties` are read, so an answer of true holds for every
|
|
9
|
+
# payload the variant accepts. Rules use it to tell whether `given` is
|
|
10
|
+
# already decided by a variant (see {Condition#to_one_of_schema}).
|
|
11
|
+
#
|
|
12
|
+
# @example
|
|
13
|
+
# variant = {"required" => ["a"], "properties" => {"a" => {}}, "additionalProperties" => false}
|
|
14
|
+
# VariantShape.required?(variant, "a") # => true
|
|
15
|
+
# VariantShape.forbidden?(variant, "b") # => true
|
|
16
|
+
module VariantShape
|
|
17
|
+
module_function
|
|
18
|
+
|
|
19
|
+
# Checks whether every payload of the variant has the property
|
|
20
|
+
#
|
|
21
|
+
# @param variant [Hash] The variant schema
|
|
22
|
+
# @param property_name [String] The property name
|
|
23
|
+
# @return [Boolean]
|
|
24
|
+
def required?(variant, property_name)
|
|
25
|
+
variant.fetch("required", []).include?(property_name)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Checks whether no payload of the variant has the property
|
|
29
|
+
#
|
|
30
|
+
# @param variant [Hash] The variant schema
|
|
31
|
+
# @param property_name [String] The property name
|
|
32
|
+
# @return [Boolean]
|
|
33
|
+
def forbidden?(variant, property_name)
|
|
34
|
+
properties = variant.fetch("properties", {})
|
|
35
|
+
properties[property_name] == false || (!properties.key?(property_name) && variant["additionalProperties"] == false)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -144,12 +144,12 @@ module Verquest
|
|
|
144
144
|
def custom_field_types=(custom_field_types)
|
|
145
145
|
raise ArgumentError, "Custom field types must be a Hash" unless custom_field_types.is_a?(Hash)
|
|
146
146
|
|
|
147
|
-
custom_field_types.
|
|
148
|
-
|
|
149
|
-
value[:schema_options] = camelize(value[:schema_options]) if value[:schema_options]
|
|
150
|
-
end
|
|
147
|
+
@custom_field_types = custom_field_types.each_with_object({}) do |(name, definition), types|
|
|
148
|
+
next if Properties::Field::DEFAULT_TYPES.include?(name.to_s)
|
|
151
149
|
|
|
152
|
-
|
|
150
|
+
definition = definition.merge(schema_options: camelize(definition[:schema_options].dup)) if definition[:schema_options]
|
|
151
|
+
types[name.to_sym] = definition
|
|
152
|
+
end
|
|
153
153
|
end
|
|
154
154
|
|
|
155
155
|
# Gets the JSON Schema class based on the configured version
|
data/lib/verquest/gem_version.rb
CHANGED
|
@@ -194,7 +194,9 @@ module Verquest
|
|
|
194
194
|
# @param version [String, nil] The version to create mapping for
|
|
195
195
|
# @return [void]
|
|
196
196
|
def mapping(key_prefix:, value_prefix:, mapping:, version:)
|
|
197
|
-
if
|
|
197
|
+
if schema_only_item?
|
|
198
|
+
mapping[(key_prefix + [name]).join("/")] = mapping_value_key(value_prefix:)
|
|
199
|
+
elsif has_item?
|
|
198
200
|
add_request_mapping(item, key_prefix: key_prefix + ["#{name}[]"], value_prefix: mapping_value_prefix(value_prefix: value_prefix, collection: true), mapping:, version:)
|
|
199
201
|
else
|
|
200
202
|
children = has_one_of? ? [one_of_property] : all_properties.values
|
|
@@ -215,6 +217,8 @@ module Verquest
|
|
|
215
217
|
# @param version [String, nil] The version to create the layout for
|
|
216
218
|
# @return [void]
|
|
217
219
|
def layout(key_prefix:, value_prefix:, layout:, version:)
|
|
220
|
+
return super if schema_only_item?
|
|
221
|
+
|
|
218
222
|
item_key_prefix = key_prefix + ["#{name}[]"]
|
|
219
223
|
item_value_prefix = mapping_value_prefix(value_prefix:, collection: true)
|
|
220
224
|
layout.add_collection(item_key_prefix.join("/"), item_value_prefix.join("/"))
|
|
@@ -228,6 +232,13 @@ module Verquest
|
|
|
228
232
|
private
|
|
229
233
|
|
|
230
234
|
attr_reader :item, :schema_options, :properties, :type
|
|
235
|
+
|
|
236
|
+
# Whether item only provides a schema (not a Verquest::Base subclass), so items pass through unchanged
|
|
237
|
+
#
|
|
238
|
+
# @return [Boolean]
|
|
239
|
+
def schema_only_item?
|
|
240
|
+
has_item? && !(item.is_a?(Class) && item <= Verquest::Base)
|
|
241
|
+
end
|
|
231
242
|
end
|
|
232
243
|
end
|
|
233
244
|
end
|
|
@@ -60,12 +60,15 @@ module Verquest
|
|
|
60
60
|
# Object properties define inline schemas directly within the oneOf.
|
|
61
61
|
#
|
|
62
62
|
# @param schema [Verquest::Properties::Reference, Verquest::Properties::Object] The schema to add
|
|
63
|
-
# @raise [ArgumentError] If schema is neither a Reference nor an Object
|
|
63
|
+
# @raise [ArgumentError] If schema is neither a Reference nor an Object, or references a schema-only class
|
|
64
64
|
# @return [Verquest::Properties::Base] The added schema
|
|
65
65
|
def add(schema)
|
|
66
66
|
unless schema.is_a?(Verquest::Properties::Reference) || schema.is_a?(Verquest::Properties::Object)
|
|
67
67
|
raise ArgumentError, "Must be a Reference or Object property"
|
|
68
68
|
end
|
|
69
|
+
if schema.is_a?(Verquest::Properties::Reference) && schema.schema_only?
|
|
70
|
+
raise ArgumentError, "one_of variants must reference Verquest::Base subclasses"
|
|
71
|
+
end
|
|
69
72
|
|
|
70
73
|
schemas[schema.name] = schema
|
|
71
74
|
end
|
|
@@ -68,6 +68,7 @@ module Verquest
|
|
|
68
68
|
# @return [void]
|
|
69
69
|
# @raise [Verquest::PropertyNotFoundError] If the referenced property doesn't exist
|
|
70
70
|
def mapping(key_prefix:, value_prefix:, mapping:, version:)
|
|
71
|
+
return mapping[(key_prefix + [name]).join("/")] = mapping_value_key(value_prefix:) if schema_only?
|
|
71
72
|
return add_request_mapping(from, key_prefix: key_prefix + [name], value_prefix: mapping_value_prefix(value_prefix:), mapping:, version:) unless property
|
|
72
73
|
|
|
73
74
|
referenced = from.resolve(version)
|
|
@@ -83,6 +84,7 @@ module Verquest
|
|
|
83
84
|
# @return [void]
|
|
84
85
|
# @raise [Verquest::PropertyNotFoundError] If the referenced property doesn't exist
|
|
85
86
|
def layout(key_prefix:, value_prefix:, layout:, version:)
|
|
87
|
+
return super if schema_only?
|
|
86
88
|
return add_request_layout(from, key_prefix: key_prefix + [name], value_prefix: mapping_value_prefix(value_prefix:), layout:, version:) unless property
|
|
87
89
|
|
|
88
90
|
referenced = from.resolve(version)
|
|
@@ -99,6 +101,8 @@ module Verquest
|
|
|
99
101
|
# @return [Hash{Array<String> => Hash}] The options of each property path
|
|
100
102
|
# @raise [Verquest::PropertyNotFoundError] If the referenced property doesn't exist
|
|
101
103
|
def query_options(prefix:, version:)
|
|
104
|
+
return super if schema_only?
|
|
105
|
+
|
|
102
106
|
referenced = from.resolve(version)
|
|
103
107
|
return referenced_property_options(referenced, prefix:) if property
|
|
104
108
|
|
|
@@ -107,6 +111,15 @@ module Verquest
|
|
|
107
111
|
end
|
|
108
112
|
end
|
|
109
113
|
|
|
114
|
+
# Whether the referenced class only provides a schema (not a Verquest::Base subclass),
|
|
115
|
+
# so its value passes through unchanged
|
|
116
|
+
#
|
|
117
|
+
# @return [Boolean]
|
|
118
|
+
# @api private
|
|
119
|
+
def schema_only?
|
|
120
|
+
!(from.is_a?(Class) && from <= Verquest::Base)
|
|
121
|
+
end
|
|
122
|
+
|
|
110
123
|
private
|
|
111
124
|
|
|
112
125
|
attr_reader :from, :property
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Verquest
|
|
4
|
+
# Inclusive range of versions a request serves, declared with `supported_versions`
|
|
5
|
+
#
|
|
6
|
+
# Versions are compared with the configured version resolver, so the range uses the same
|
|
7
|
+
# order as version resolution. With the default resolver that is string order, where
|
|
8
|
+
# "preview" sorts after dated versions.
|
|
9
|
+
#
|
|
10
|
+
# @example
|
|
11
|
+
# range = Verquest::SupportedVersions.new(min: "2025-09", max: "2026-03")
|
|
12
|
+
# range.covers?("2025-12") # => true
|
|
13
|
+
# range.covers?("2026-06") # => false
|
|
14
|
+
class SupportedVersions
|
|
15
|
+
# @return [String, nil] The first supported version, nil to start at the first declared version
|
|
16
|
+
attr_reader :min
|
|
17
|
+
|
|
18
|
+
# @return [String, nil] The last supported version, nil for no upper bound
|
|
19
|
+
attr_reader :max
|
|
20
|
+
|
|
21
|
+
# Returns whether one version sorts at or before another
|
|
22
|
+
#
|
|
23
|
+
# The resolver picks `earlier` for a request of `later` only when `earlier` comes at or before it.
|
|
24
|
+
#
|
|
25
|
+
# @param earlier [String] The version expected first
|
|
26
|
+
# @param later [String] The version expected second
|
|
27
|
+
# @return [Boolean] true when earlier sorts at or before later
|
|
28
|
+
def self.at_or_before?(earlier, later)
|
|
29
|
+
Verquest.configuration.version_resolver.call(later, [earlier]) == earlier
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# @param min [String, nil] The first supported version (inclusive)
|
|
33
|
+
# @param max [String, nil] The last supported version (inclusive)
|
|
34
|
+
# @raise [Verquest::DefinitionError] If min sorts after max
|
|
35
|
+
def initialize(min:, max:)
|
|
36
|
+
@min = min&.to_s
|
|
37
|
+
@max = max&.to_s
|
|
38
|
+
raise DefinitionError, "supported_versions min: #{@min} is after max: #{@max}" if @min && @max && !self.class.at_or_before?(@min, @max)
|
|
39
|
+
|
|
40
|
+
freeze
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Returns whether the range includes a version
|
|
44
|
+
#
|
|
45
|
+
# Without min, earlier versions are left to the resolver, which finds nothing before the first declared version.
|
|
46
|
+
#
|
|
47
|
+
# @param version [String] The version name
|
|
48
|
+
# @return [Boolean] true when the version is within the range
|
|
49
|
+
def covers?(version)
|
|
50
|
+
(min.nil? || self.class.at_or_before?(min, version)) && (max.nil? || self.class.at_or_before?(version, max))
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Raises when max sorts before every declared version, so no request could resolve
|
|
54
|
+
#
|
|
55
|
+
# Only max is checked: declared blocks don't have to be inside the range, so
|
|
56
|
+
# `min: "preview"` with a single `version "2025-09"` block serves "preview" from 2025-09.
|
|
57
|
+
#
|
|
58
|
+
# @param declared [Array<String>] The declared version names
|
|
59
|
+
# @param owner [String] The class name, for the error message
|
|
60
|
+
# @return [void]
|
|
61
|
+
# @raise [Verquest::DefinitionError] If max sorts before every declared version
|
|
62
|
+
def check!(declared, owner)
|
|
63
|
+
return if max.nil? || declared.empty? || declared.any? { |version| self.class.at_or_before?(version, max) }
|
|
64
|
+
|
|
65
|
+
raise DefinitionError, "#{owner}: supported_versions max: #{max} is before every declared version (#{declared.join(", ")})"
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# @return [String] The range for error messages, like "min: 2025-09, max: none"
|
|
69
|
+
def to_s
|
|
70
|
+
"min: #{min || "first declared"}, max: #{max || "none"}"
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
data/lib/verquest/versions.rb
CHANGED
|
@@ -56,6 +56,13 @@ module Verquest
|
|
|
56
56
|
versions[resolved_version_name] || raise(Verquest::VersionNotFoundError, "Version '#{version_name}' not found")
|
|
57
57
|
end
|
|
58
58
|
|
|
59
|
+
# Returns the declared version names in string order
|
|
60
|
+
#
|
|
61
|
+
# @return [Array<String>] The version names, sorted as strings
|
|
62
|
+
def names
|
|
63
|
+
versions.keys.sort
|
|
64
|
+
end
|
|
65
|
+
|
|
59
66
|
private
|
|
60
67
|
|
|
61
68
|
# @!attribute [r] versions
|
data/lib/verquest.rb
CHANGED
|
@@ -68,7 +68,8 @@ module Verquest
|
|
|
68
68
|
# @api public
|
|
69
69
|
MappingError = Class.new(Verquest::Error)
|
|
70
70
|
|
|
71
|
-
# Error raised when a request definition
|
|
71
|
+
# Error raised when a request definition is invalid, such as a shape its location can't
|
|
72
|
+
# express or a supported_versions max before the first declared version
|
|
72
73
|
# @api public
|
|
73
74
|
DefinitionError = Class.new(Verquest::Error)
|
|
74
75
|
|
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.7.
|
|
4
|
+
version: 0.7.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Petr Hlavicka
|
|
@@ -79,6 +79,7 @@ files:
|
|
|
79
79
|
- lib/verquest/conditions/rules/property_rule.rb
|
|
80
80
|
- lib/verquest/conditions/rules/satisfies.rb
|
|
81
81
|
- lib/verquest/conditions/rules/value_in.rb
|
|
82
|
+
- lib/verquest/conditions/variant_shape.rb
|
|
82
83
|
- lib/verquest/configuration.rb
|
|
83
84
|
- lib/verquest/gem_version.rb
|
|
84
85
|
- lib/verquest/helper_methods/conditional_properties.rb
|
|
@@ -100,6 +101,7 @@ files:
|
|
|
100
101
|
- lib/verquest/query/definition_check.rb
|
|
101
102
|
- lib/verquest/query/parameters.rb
|
|
102
103
|
- lib/verquest/result.rb
|
|
104
|
+
- lib/verquest/supported_versions.rb
|
|
103
105
|
- lib/verquest/transformer.rb
|
|
104
106
|
- lib/verquest/transformer/collection.rb
|
|
105
107
|
- lib/verquest/transformer/const_as_enum.rb
|