verquest 0.7.0 → 0.8.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: 8bc88f55be50f80f099c989c617954b1600ed5d61adfcf35fccd430fbbce21cc
4
- data.tar.gz: 8741893b6b9c3d5e91db1b6d6c3e3bf72e84ac43babd34f62a04859ee5bf6a7e
3
+ metadata.gz: 4445e1ed3a032827a00b4805bb1eb3bcb57eb11cf3a261e334d4bb5362df953d
4
+ data.tar.gz: 14a3b0eb7fc630f6d227f4efc439eabfde307ed0e8d3c133a68b96120e6b588e
5
5
  SHA512:
6
- metadata.gz: d77a640d8afade1d2578899ef72a39550d870452f16e81bfef9c2aa61506c1b09aea07e7f23b3ef35ccc38218295c620389c1cda3e6eb9cdf1bb3227638358b1
7
- data.tar.gz: 308cdf6806df22ec89108911cfe9a6e351e9b9c3c7b925a8a15dd31c108adc19f51d8f361528ecd566e057042b1382603ec6b3652e4f9604a5e45c711e7d3d81
6
+ metadata.gz: 6605a7c2d01d8a1d89388340592f5f9ac7e61220f705ce06eecdc50b39b0b01771c70afa0311f4ca4ebd21f72c0ca61732ca2ab27e9fc332d5761f357f909e4a
7
+ data.tar.gz: c8dacd48fec778f398971dcf1f797fb639edd771069070c64c2f5ee3a9eb3c1389483f5dbb6348eb4607c579624ba3fb74a94fe64c05f9a7bd7cbe79112d9fd8
data/CHANGELOG.md CHANGED
@@ -1,5 +1,39 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.8.0] - 2026-11-06
4
+
5
+ ### Changed
6
+ - **Breaking:** `enum` writes a `type` next to its values and checks the values when the request is defined. Without `type:`, the type is inferred: Strings are `string`, Integers `integer`, other Numerics `number`, `true`/`false` `boolean`, Hashes `object`; Integers next to other Numerics are `number`, and other mixes a type array. These now raise `ArgumentError`:
7
+ - a value of another class, such as a Symbol (Symbols never matched JSON values) or an Array;
8
+ - `nil` in the values without `nullable: true`;
9
+ - a `type:` other than the five above, or one a value doesn't fit;
10
+ - values equal in JSON, such as `1` and `1.0` (also inside Hashes);
11
+ - NaN or Infinity;
12
+ - `values:` that isn't an Array;
13
+ - a Hash value with a key that isn't a String, or with something inside that isn't JSON (such as a Symbol).
14
+
15
+ `nullable: true` adds `"null"` to the type, inferred or given. `with_options(type:)` no longer applies to enums.
16
+ - With `json_schema_version = :openapi30`, documents (`to_schema` and `to_parameters`) use OpenAPI 3.0 forms: `nullable: true` instead of a `"null"` type, an `anyOf` of single types (each nullable) instead of a type array, a nullable constant as an `enum` with `null`, an `allOf` around a nullable `$ref` or discriminated `one_of`, and `x-dependentRequired` instead of `dependentRequired`. Validation schemas keep the JSON Schema forms validators honour.
17
+ - Under `:openapi30`, `valid_schema?` and `validate_schema` check the document (`to_schema`) instead of the validation schema, whose JSON Schema forms made the check fail for any nullable request.
18
+ - Schemas leave out `required` when nothing is required, instead of an empty array, which draft 4 and OpenAPI 3.0 forbid. Validation is unchanged. `present` and `absent` without a property name raise `ArgumentError`.
19
+
20
+ ### Fixed
21
+ - Query params convert `""` to `nil` in nullable string and mixed enums, and in nullable fields with `enum:`.
22
+
23
+ ## [0.7.1] - 2026-11-06
24
+
25
+ ### Added
26
+ - `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.
27
+ - `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.
28
+
29
+ ### Fixed
30
+ - `collection item:` validation referenced an undefined `type` variable.
31
+ - `version ..., inherit: "<version>"` copied the previous version instead of the named one.
32
+ - `config.custom_field_types=` no longer modifies the Hash it is given.
33
+
34
+ ### Changed
35
+ - `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.
36
+
3
37
  ## [0.7.0] - 2026-11-05
4
38
 
5
39
  ### Added
data/README.md CHANGED
@@ -166,19 +166,18 @@ Output:
166
166
  "description" => "Permissions associated with the user"
167
167
  },
168
168
  "role" => {
169
+ "type" => "string",
169
170
  "enum" => ["member", "manager"],
170
171
  "default" => "member",
171
172
  "description" => "Role of the user"
172
173
  },
173
174
  "profile_details" => {
174
175
  "type" => "object",
175
- "required" => [],
176
176
  "properties" => {
177
177
  "bio" => {"type" => "string", "description" => "Short biography of the user"},
178
178
  "hobbies" => {"type" => "array", "items" => {"type" => "string"}, "description" => "Tags associated with the user"},
179
179
  "social_links" => {
180
180
  "type" => "object",
181
- "required" => [],
182
181
  "properties" => {
183
182
  "github" => {"type" => "string", "format" => "uri", "description" => "GitHub profile URL"},
184
183
  "mastodon" => {"type" => "string", "format" => "uri", "description" => "Mastodon profile URL"}
@@ -237,18 +236,17 @@ Output:
237
236
  "description" => "Permissions associated with the user"
238
237
  },
239
238
  "role" => {
239
+ "type" => "string",
240
240
  "enum" => ["member", "manager"],
241
241
  "default" => "member",
242
242
  "description" => "Role of the user"
243
243
  },
244
244
  "profile_details" => {"type" => "object",
245
- "required" => [],
246
245
  "properties" => {
247
246
  "bio" => {"type" => "string", "description" => "Short biography of the user"},
248
247
  "hobbies" => {"type" => "array", "items" => {"type" => "string"}, "description" => "Tags associated with the user"},
249
248
  "social_links" => {
250
249
  "type" => "object",
251
- "required" => [],
252
250
  "properties" => {
253
251
  "github" => {"type" => "string", "format" => "uri", "description" => "GitHub profile URL"},
254
252
  "mastodon" => {"type" => "string", "format" => "uri", "description" => "Mastodon profile URL"}
@@ -262,7 +260,7 @@ Output:
262
260
  }
263
261
  ```
264
262
 
265
- You can also validate it to ensure it meets the JSON Schema standards:
263
+ You can also validate it to ensure it meets the JSON Schema standards. Under `:openapi30` this checks the document from `to_schema` against the OpenAPI 3.0 meta-schema, since validation schemas keep JSON Schema forms OpenAPI 3.0 forbids:
266
264
 
267
265
  ```ruby
268
266
  UserCreateRequest.valid_schema?(version: "2025-06") # => true/false
@@ -280,7 +278,7 @@ The JSON schema can be used for both validation of incoming parameters and for g
280
278
  #### Component types
281
279
 
282
280
  - `field`: a scalar value (string, integer, boolean, etc.).
283
- - `enum`: a value from a fixed list.
281
+ - `enum`: a value from a fixed list. Its `type` is inferred from the values (`string`, `integer`, `number`, `boolean`, `object`, or a type array for mixed values) unless you pass `type:`, and every value must fit it. A value of another class (such as a Symbol), `nil` without `nullable: true`, or values equal in JSON (`1` and `1.0`) raise `ArgumentError`.
284
282
  - `object`: a JSON object with its own properties.
285
283
  - `array`: a JSON array of scalar items.
286
284
  - `collection`: a JSON array of objects, defined inline or with `item:` pointing at another request.
@@ -333,17 +331,29 @@ Will produce this validation schema:
333
331
 
334
332
  You can define nullable properties in your request schema by setting the `nullable` option to `true`. This feature is based on the latest JSON Schema specification, which is also used in OpenAPI 3.1.
335
333
 
336
- Nullable enums include Ruby `nil` in their allowed values, which exports as JSON `null`, not the string `"null"`. This also applies to fields with an `enum:` constraint (including custom field types). The supplied values array is not modified.
334
+ Nullable enums include Ruby `nil` in their allowed values, which exports as JSON `null`, not the string `"null"`, and add `"null"` to the type, inferred or given. An enum that lists `nil` itself needs `nullable: true`. This also applies to fields with an `enum:` constraint (including custom field types). The supplied values array is not modified.
337
335
 
338
336
  ```ruby
339
337
  enum :role, values: %w[member admin], nullable: true
340
- # => {"role" => {"enum" => ["member", "admin", nil]}}
338
+ # => {"role" => {"type" => ["string", "null"], "enum" => ["member", "admin", nil]}}
339
+ # under :openapi30: {"type" => "string", "nullable" => true, "enum" => ["member", "admin", nil]}
341
340
 
342
341
  const :kind, value: "user", nullable: true
343
342
  # => {"kind" => {"anyOf" => [{"const" => "user"}, {"type" => "null"}]}}
344
343
  ```
345
344
 
346
- Nullable constants and references use `anyOf` with a null alternative in both exported and validation schemas. Nullable `one_of` uses an outer `anyOf` around the original `oneOf` and the null alternative. This allows null even when a referenced schema or multiple variants already accept it, while preserving all restrictions on non-null values. A discriminator, when present, stays alongside the inner `oneOf`.
345
+ Nullable constants and references use `anyOf` with a null alternative in both exported and validation schemas (except `:openapi30` documents, see below). Nullable `one_of` uses an outer `anyOf` around the original `oneOf` and the null alternative. This allows null even when a referenced schema or multiple variants already accept it, while preserving all restrictions on non-null values. A discriminator, when present, stays alongside the inner `oneOf`.
346
+
347
+ OpenAPI 3.0 has no `null` type, so with `json_schema_version = :openapi30` documents (`to_schema` and `to_parameters`) write `nullable: true` instead:
348
+ - next to the type;
349
+ - merged into a nullable constant (with `null` in its `enum`) or a `one_of` without a discriminator;
350
+ - next to an `allOf` around a nullable reference (`{"allOf" => [{"$ref" => "..."}], "nullable" => true}`) or a nullable `one_of` with a discriminator, which keeps null outside the discriminated `oneOf`, as versponse does.
351
+
352
+ OpenAPI 3.0 also has no `dependentRequired` (from `required: [:other]`), so documents write it as the `x-dependentRequired` extension. Validation still enforces it.
353
+
354
+ Mixed types become an `anyOf` of single types, each nullable when the property is. Strict OpenAPI 3.0.3 tools honour `nullable` only next to `type`, so they may still treat a nullable reference or `one_of` as not nullable. Validation schemas keep the forms above, which validators honour.
355
+
356
+ Enum values must be JSON values: Strings, Numerics, booleans or Hashes, given as an Array. Hash values need String keys, and everything inside them must be JSON too (Strings, finite numbers, booleans, `nil`, Arrays and Hashes). A BigDecimal or Rational value is a `number`, but Ruby's JSON generator writes it as a string, so prefer Integers and Floats in documented enums.
347
357
 
348
358
  `required: true` still requires the key to be present; `nullable: true` only permits its value to be null. Defaults stay on the outer nullable schema so missing properties still receive them when default insertion is enabled. Explicit null values are not replaced by defaults.
349
359
 
@@ -378,15 +388,13 @@ Will produce this validation schema:
378
388
  {
379
389
  "type" => "object",
380
390
  "description" => "This is a simple request with nullable properties for testing purposes.",
381
- "required" => [],
382
391
  "properties" => {
383
392
  "array" => {"type" => %w[array null], "items" => {"type" => "string"}},
384
393
  "collection_with_item" => {"type" => %w[array null], "items" => {"type" => "object", "description" => "This is an another example for testing purposes.", "required" => %w[simple_field nested], "properties" => {"simple_field" => {"type" => "string", "description" => "The simple field"}, "nested" => {"type" => "object", "required" => %w[nested_field_1 nested_field_2], "properties" => {"nested_field_1" => {"type" => "string", "description" => "This is a nested field"}, "nested_field_2" => {"type" => "string", "description" => "This is another nested field"}}, "additionalProperties" => false}}, "additionalProperties" => false}},
385
- "collection_with_object" => {"type" => %w[array null], "items" => {"type" => "object", "required" => [], "properties" => {"field" => {"type" => "string"}}, "additionalProperties" => false}},
394
+ "collection_with_object" => {"type" => %w[array null], "items" => {"type" => "object", "properties" => {"field" => {"type" => "string"}}, "additionalProperties" => false}},
386
395
  "field" => {"type" => %w[string null]},
387
396
  "object" => {
388
397
  "type" => %w[object null],
389
- "required" => [],
390
398
  "properties" => {
391
399
  "field" => {"type" => "string"}
392
400
  },
@@ -502,7 +510,7 @@ end
502
510
  }
503
511
  ```
504
512
 
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, unless it already forbids a property `given` requires. 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`.
513
+ - 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
514
  - Validation still uses the `if` / `then` / `else` schema, so validation errors point at those rules, not at a `oneOf` variant.
507
515
  - When `additionalProperties` isn't `false`, the other branch's properties are forbidden with `false` in each variant.
508
516
  - 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 +693,32 @@ This is used across all referenced requests, so if you have a `UserRequest` that
685
693
 
686
694
  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
695
 
696
+ #### Supported versions
697
+
698
+ `supported_versions` limits the versions a request serves. Both bounds are inclusive and optional:
699
+
700
+ ```ruby
701
+ class OverlockRequest < Verquest::Base
702
+ supported_versions min: "2025-09", max: "2026-03"
703
+
704
+ version "2025-09" do
705
+ field :reason, type: :string, required: true
706
+ end
707
+ end
708
+
709
+ OverlockRequest.process(params, version: "2025-12") # => uses the defined version "2025-09"
710
+ OverlockRequest.process(params, version: "2026-06") # => raises Verquest::VersionNotFoundError
711
+ ```
712
+
713
+ - `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.
714
+ - `max` defaults to no upper bound.
715
+ - 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.
716
+ - 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`.
717
+ - Versions are compared with `config.version_resolver`. With the default resolver they compare as strings, so `"preview"` sorts after dated versions.
718
+ - `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.
719
+ - 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.
720
+ - `inherit: "<version>"` copies any declared version, even one outside the range.
721
+
688
722
  ### Mapping request structure
689
723
 
690
724
  Verquest's mapping system allows transforming external API request structures into your internal application data structures.
@@ -787,7 +821,7 @@ DealsIndexQuery.process(request.query_parameters, version: "2025-06")
787
821
  ```
788
822
 
789
823
  Conversion follows the validation schema:
790
- - `integer` and `number` from digits (`"2"`, `"-1.5"`), `boolean` from `"true"` and `"false"`, `null` from an empty string when the schema allows it;
824
+ - `integer` and `number` from digits (`"2"`, `"-1.5"`), `boolean` from `"true"` and `"false"`, `null` from an empty string when the schema allows it (a string outside an enum's values tries the next type, so `""` is `nil` in a nullable string enum);
791
825
  - arrays split on commas with `explode: false` (`status=open,won`), or come from repeated keys with `explode: true` (`status[]=open&status[]=won`; Rails keeps only the last value of a plain repeated key). `config.query_array_explode` sets the default (`true`);
792
826
  - objects (`deepObject`, one level deep) convert each property.
793
827
 
@@ -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 && @current_scope
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 = {}
@@ -154,7 +200,7 @@ module Verquest
154
200
 
155
201
  required = default_options.fetch(:required, false) if required.nil?
156
202
  nullable = default_options.fetch(:nullable, false) if nullable.nil?
157
- schema_options = default_options.except(:required, :nullable).merge(schema_options)
203
+ schema_options = default_options.except(:type, :required, :nullable).merge(schema_options)
158
204
 
159
205
  enum_property = Properties::Enum.new(name:, values:, map:, required:, nullable:, **schema_options)
160
206
  add_property(enum_property, parameter_examples)
@@ -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? && !block_given? && !(item <= Verquest::Base)
229
- raise ArgumentError, "item must be a child of Verquest::Base class or nil" unless type.is_a?(Verquest::Properties::Base)
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)
@@ -127,6 +127,8 @@ module Verquest
127
127
 
128
128
  # Validates the generated JSON schema structure
129
129
  #
130
+ # Under `:openapi30` it checks the document from `to_schema`, otherwise the validation schema.
131
+ #
130
132
  # @param version [String, nil] Specific version to use, defaults to configuration setting
131
133
  # @return [Boolean] True if schema is valid
132
134
  def valid_schema?(version: nil)
@@ -137,7 +139,8 @@ module Verquest
137
139
  #
138
140
  # This method validates the schema against the configured JSON Schema metaschema
139
141
  # and returns detailed validation errors if any are found. It's useful for debugging
140
- # schema issues during development and testing.
142
+ # schema issues during development and testing. Under `:openapi30` it checks the document from `to_schema`, so
143
+ # the pointers refer to it.
141
144
  #
142
145
  # @param version [String, nil] Specific version to use, defaults to configuration setting
143
146
  # @return [Array<Hash>] An array of validation error details, empty if schema is valid
@@ -80,8 +80,7 @@ module Verquest
80
80
  #
81
81
  # @return [Hash] The `required` and `dependentRequired` fragment
82
82
  def required_schema
83
- {}.tap do |schema|
84
- schema["required"] = required_properties if required_properties.any?
83
+ required_keyword.tap do |schema|
85
84
  schema["dependentRequired"] = dependent_required_properties if dependent_required_properties.any?
86
85
  end
87
86
  end
@@ -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, and the `else` variant their negation, unless
112
- # the variant already rules `given` out. So exactly one variant matches
113
- # every payload the `if` / `then` / `else` schema accepts.
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, (part == :then) ? given.to_schema(in_given: true) : {}]).tap do |variant|
159
- variant["not"] = given.to_schema(in_given: true) if part == :else && given_possible?(variant)
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
@@ -14,7 +14,10 @@ module Verquest
14
14
  # Initialize a new rule
15
15
  #
16
16
  # @param names [Array<Symbol, String>] The properties the rule names
17
+ # @raise [ArgumentError] If no property is named
17
18
  def initialize(names)
19
+ raise ArgumentError, "Name at least one property" if names.empty?
20
+
18
21
  @property_names = names.map(&:to_s)
19
22
  end
20
23
  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.delete_if { |k, _| Properties::Field::DEFAULT_TYPES.include?(k.to_s) }
148
- custom_field_types.each do |_, value|
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
- @custom_field_types = custom_field_types.transform_keys(&:to_sym)
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
@@ -169,6 +169,15 @@ module Verquest
169
169
  !VERSIONS_WITHOUT_CONST.include?(json_schema_version)
170
170
  end
171
171
 
172
+ # Checks whether documents write nullable as a keyword
173
+ #
174
+ # OpenAPI 3.0 has no "null" type, so its documents use `nullable: true` (see {Verquest::Openapi30}).
175
+ #
176
+ # @return [Boolean] True for OpenAPI 3.0
177
+ def nullable_keyword?
178
+ json_schema_version == :openapi30
179
+ end
180
+
172
181
  # Gets the JSON Schema URI for the configured schema version
173
182
  #
174
183
  # @return [String] The base URI for the configured JSON Schema version
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Verquest
4
- GEM_VERSION = "0.7.0"
4
+ GEM_VERSION = "0.8.0"
5
5
  end