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 +4 -4
- data/CHANGELOG.md +34 -0
- data/README.md +48 -14
- data/lib/verquest/base/private_class_methods.rb +54 -7
- data/lib/verquest/base/public_class_methods.rb +4 -1
- data/lib/verquest/conditions/branch.rb +1 -2
- 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/names_rule.rb +3 -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 +14 -5
- data/lib/verquest/gem_version.rb +1 -1
- data/lib/verquest/helper_methods/required_properties.rb +9 -0
- data/lib/verquest/openapi30.rb +128 -0
- data/lib/verquest/properties/collection.rb +14 -3
- data/lib/verquest/properties/enum.rb +143 -10
- data/lib/verquest/properties/object.rb +2 -2
- data/lib/verquest/properties/one_of.rb +4 -1
- data/lib/verquest/properties/reference.rb +13 -0
- data/lib/verquest/query/coercer.rb +11 -1
- data/lib/verquest/query/parameters.rb +1 -1
- data/lib/verquest/supported_versions.rb +73 -0
- data/lib/verquest/version.rb +27 -9
- data/lib/verquest/versions.rb +7 -0
- data/lib/verquest.rb +2 -1
- metadata +4 -1
|
@@ -25,6 +25,15 @@ module Verquest
|
|
|
25
25
|
@_required_properties ||= properties.values.select { _1.required == true }.map(&:name)
|
|
26
26
|
end
|
|
27
27
|
|
|
28
|
+
# Returns the `required` keyword for a schema, left out when nothing is required
|
|
29
|
+
#
|
|
30
|
+
# Draft 4 and OpenAPI 3.0 forbid an empty `required` array, and later drafts treat it like no `required`.
|
|
31
|
+
#
|
|
32
|
+
# @return [Hash] `{"required" => names}`, or an empty Hash when no property is required
|
|
33
|
+
def required_keyword
|
|
34
|
+
required_properties.any? ? {"required" => required_properties} : {}
|
|
35
|
+
end
|
|
36
|
+
|
|
28
37
|
# Returns properties that are conditionally required based on other properties
|
|
29
38
|
#
|
|
30
39
|
# This method identifies properties that are required only when certain other
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Verquest
|
|
4
|
+
# Rewrites a JSON Schema into OpenAPI 3.0 forms
|
|
5
|
+
#
|
|
6
|
+
# OpenAPI 3.0 has no "null" type and no type arrays. Properties write the JSON Schema forms, which validators
|
|
7
|
+
# honour, so only documents are converted, when `json_schema_version` is `:openapi30`:
|
|
8
|
+
# - "null" in a type array becomes `nullable: true`
|
|
9
|
+
# - several types become an `anyOf` of single types, each nullable when the schema is
|
|
10
|
+
# - an `anyOf` with a single null alternative becomes `nullable: true`, with an `allOf` around a `$ref`
|
|
11
|
+
# (OpenAPI 3.0 ignores keywords next to `$ref`) or a discriminated `oneOf` (which would reject null),
|
|
12
|
+
# and `null` added to an `enum`
|
|
13
|
+
# - `dependentRequired` becomes the `x-dependentRequired` extension
|
|
14
|
+
#
|
|
15
|
+
# Only schema positions are visited, so a property named "type" is never read as the keyword.
|
|
16
|
+
#
|
|
17
|
+
# @example
|
|
18
|
+
# Verquest::Openapi30.document({"type" => ["string", "null"]})
|
|
19
|
+
# # => {"type" => "string", "nullable" => true}
|
|
20
|
+
module Openapi30
|
|
21
|
+
# The null alternative of a nullable anyOf
|
|
22
|
+
NULL_SCHEMA = {"type" => "null"}.freeze
|
|
23
|
+
|
|
24
|
+
# Keywords whose value is a map from names to schemas
|
|
25
|
+
SCHEMA_MAPS = %w[properties patternProperties dependentSchemas dependencies $defs definitions].freeze
|
|
26
|
+
|
|
27
|
+
# Keywords whose value is a list of schemas
|
|
28
|
+
SCHEMA_LISTS = %w[prefixItems anyOf oneOf allOf].freeze
|
|
29
|
+
|
|
30
|
+
# Keywords whose value is a schema (`items` can also be a list)
|
|
31
|
+
SCHEMAS = %w[
|
|
32
|
+
items additionalProperties not if then else
|
|
33
|
+
contains propertyNames unevaluatedProperties unevaluatedItems additionalItems
|
|
34
|
+
].freeze
|
|
35
|
+
|
|
36
|
+
# Keywords OpenAPI 3.0 lacks, written as extensions
|
|
37
|
+
RENAMED = {"dependentRequired" => "x-dependentRequired"}.freeze
|
|
38
|
+
|
|
39
|
+
module_function
|
|
40
|
+
|
|
41
|
+
# @param schema [Object] A schema; other values, such as `additionalProperties: false`, are returned as they are
|
|
42
|
+
# @return [Object] A converted copy of the schema
|
|
43
|
+
def document(schema)
|
|
44
|
+
return schema unless schema.is_a?(::Hash)
|
|
45
|
+
|
|
46
|
+
# The null alternative goes first, so a merged alternative's type array meets nullable in type_union
|
|
47
|
+
type_union(nullable_any_of(schema).to_h { |keyword, value| [RENAMED.fetch(keyword, keyword), child(keyword, value)] })
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# @param keyword [String] The schema keyword
|
|
51
|
+
# @param value [Object] Its value
|
|
52
|
+
# @return [Object] The value, with the schemas it holds converted
|
|
53
|
+
def child(keyword, value)
|
|
54
|
+
if SCHEMA_MAPS.include?(keyword) && value.is_a?(::Hash)
|
|
55
|
+
value.transform_values { |schema| document(schema) }
|
|
56
|
+
elsif (SCHEMA_LISTS.include?(keyword) || SCHEMAS.include?(keyword)) && value.is_a?(::Array)
|
|
57
|
+
value.map { |schema| document(schema) }
|
|
58
|
+
elsif SCHEMAS.include?(keyword)
|
|
59
|
+
document(value)
|
|
60
|
+
else
|
|
61
|
+
value
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# OpenAPI 3.0 honours nullable only next to type, so each branch of a union carries it, also when a merged
|
|
66
|
+
# nullable alternative has the type array
|
|
67
|
+
#
|
|
68
|
+
# @param schema [Hash] A schema with converted children
|
|
69
|
+
# @return [Hash] The schema without a type array
|
|
70
|
+
def type_union(schema)
|
|
71
|
+
types = schema["type"]
|
|
72
|
+
return schema unless types.is_a?(::Array)
|
|
73
|
+
|
|
74
|
+
others = types - ["null"]
|
|
75
|
+
return schema if others.empty?
|
|
76
|
+
|
|
77
|
+
nullable = (types.include?("null") || schema["nullable"]) ? {"nullable" => true} : {}
|
|
78
|
+
return schema.merge("type" => others.first).merge(nullable) if others.one?
|
|
79
|
+
|
|
80
|
+
any_of(schema.except("type"), others.map { |type| {"type" => type}.merge(nullable) })
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# @param schema [Hash] A schema without its type
|
|
84
|
+
# @param variants [Array<Hash>] The single-type schemas
|
|
85
|
+
# @return [Hash] The schema with the variants as an anyOf, next to an existing anyOf through allOf
|
|
86
|
+
def any_of(schema, variants)
|
|
87
|
+
return schema.merge("anyOf" => variants) unless schema.key?("anyOf")
|
|
88
|
+
|
|
89
|
+
schema.except("anyOf").merge("allOf" => [*schema["allOf"], {"anyOf" => variants}, {"anyOf" => schema["anyOf"]}])
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# @param schema [Hash] A schema whose children are not converted yet
|
|
93
|
+
# @return [Hash] The schema without a null alternative in its anyOf
|
|
94
|
+
def nullable_any_of(schema)
|
|
95
|
+
variants = schema["anyOf"]
|
|
96
|
+
return schema unless variants.is_a?(::Array) && variants.include?(NULL_SCHEMA)
|
|
97
|
+
|
|
98
|
+
others = variants - [NULL_SCHEMA]
|
|
99
|
+
return schema if others.empty?
|
|
100
|
+
|
|
101
|
+
siblings = schema.except("anyOf").merge("nullable" => true)
|
|
102
|
+
others.one? ? nullable_alternative(others.first, siblings) : siblings.merge("anyOf" => others)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# OpenAPI 3.0 ignores keywords next to $ref, so a nullable $ref goes into an allOf. So does a discriminated
|
|
106
|
+
# oneOf, keeping null outside it as versponse does.
|
|
107
|
+
#
|
|
108
|
+
# @param alternative [Object] The only non-null alternative
|
|
109
|
+
# @param siblings [Hash] The other keywords of the schema, with nullable
|
|
110
|
+
# @return [Hash] The nullable schema
|
|
111
|
+
def nullable_alternative(alternative, siblings)
|
|
112
|
+
return siblings.merge("anyOf" => [alternative]) unless alternative.is_a?(::Hash)
|
|
113
|
+
return {"allOf" => [alternative]}.merge(siblings) if alternative.key?("$ref") || alternative.key?("discriminator")
|
|
114
|
+
|
|
115
|
+
nullable_enum(alternative).merge(siblings)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# OpenAPI 3.0 needs null listed in an enum next to nullable
|
|
119
|
+
#
|
|
120
|
+
# @param schema [Hash] The only non-null alternative
|
|
121
|
+
# @return [Hash] The alternative, with nil in its enum
|
|
122
|
+
def nullable_enum(schema)
|
|
123
|
+
schema.key?("enum") ? schema.merge("enum" => schema["enum"] | [nil]) : schema
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
private_class_method :child, :type_union, :any_of, :nullable_any_of, :nullable_alternative, :nullable_enum
|
|
127
|
+
end
|
|
128
|
+
end
|
|
@@ -122,7 +122,7 @@ module Verquest
|
|
|
122
122
|
"type" => type,
|
|
123
123
|
"items" => {
|
|
124
124
|
"type" => "object",
|
|
125
|
-
|
|
125
|
+
**required_keyword,
|
|
126
126
|
"properties" => all_properties.transform_values { |property| property.to_schema[property.name] },
|
|
127
127
|
"additionalProperties" => Verquest.configuration.default_additional_properties
|
|
128
128
|
}.tap do |schema|
|
|
@@ -160,7 +160,7 @@ module Verquest
|
|
|
160
160
|
"type" => type,
|
|
161
161
|
"items" => {
|
|
162
162
|
"type" => "object",
|
|
163
|
-
|
|
163
|
+
**required_keyword,
|
|
164
164
|
"properties" => all_properties.transform_values { |property| property.to_validation_schema(version: version)[property.name] },
|
|
165
165
|
"additionalProperties" => Verquest.configuration.default_additional_properties
|
|
166
166
|
}.tap do |schema|
|
|
@@ -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
|
|
@@ -4,9 +4,27 @@ module Verquest
|
|
|
4
4
|
module Properties
|
|
5
5
|
# The Enum class represents a enum property with a list of possible values in a JSON schema.
|
|
6
6
|
#
|
|
7
|
+
# The type is inferred from the values unless `type:` is given, and every value must fit it.
|
|
8
|
+
#
|
|
7
9
|
# @example
|
|
8
10
|
# enum = Enum.new(name: "type", values: ["member", "admin"])
|
|
11
|
+
# enum.to_schema # => {"type" => {"type" => "string", "enum" => ["member", "admin"]}}
|
|
9
12
|
class Enum < Base
|
|
13
|
+
# Ruby classes the values of each JSON type can have
|
|
14
|
+
VALUE_CLASSES = {
|
|
15
|
+
"string" => [::String],
|
|
16
|
+
"integer" => [::Integer],
|
|
17
|
+
"number" => [::Numeric],
|
|
18
|
+
"boolean" => [::TrueClass, ::FalseClass],
|
|
19
|
+
"object" => [::Hash]
|
|
20
|
+
}.freeze
|
|
21
|
+
|
|
22
|
+
# Classes of the JSON values that need no further check inside a Hash
|
|
23
|
+
JSON_SCALARS = [::String, ::TrueClass, ::FalseClass, ::NilClass].freeze
|
|
24
|
+
|
|
25
|
+
# Added to errors about Symbols
|
|
26
|
+
SYMBOL_HINT = " (Symbols never match JSON values, use Strings)"
|
|
27
|
+
|
|
10
28
|
# Initialize a new Enum property
|
|
11
29
|
#
|
|
12
30
|
# @param name [String, Symbol] The name of the property
|
|
@@ -14,31 +32,35 @@ module Verquest
|
|
|
14
32
|
# @param required [Boolean, Array<Symbol>] Whether this property is required, or array of dependency names
|
|
15
33
|
# @param nullable [Boolean] Whether this property can be null
|
|
16
34
|
# @param map [String, nil] The mapping path for this property
|
|
35
|
+
# @param type [String, Symbol, Array<String, Symbol>, nil] The JSON type of the values, inferred from them when nil
|
|
17
36
|
# @param schema_options [Hash] Additional JSON schema options for this property
|
|
18
37
|
# @raise [ArgumentError] If attempting to map an enum to root without a name
|
|
19
|
-
# @raise [ArgumentError] If values is empty
|
|
20
|
-
# @raise [ArgumentError] If values are not unique
|
|
38
|
+
# @raise [ArgumentError] If values is not an Array, or is empty
|
|
39
|
+
# @raise [ArgumentError] If values are not unique, comparing them as JSON (1 and 1.0 are the same)
|
|
21
40
|
# @raise [ArgumentError] If only one value is provided (should use const instead)
|
|
22
|
-
|
|
41
|
+
# @raise [ArgumentError] If values include nil without nullable, or a number that isn't finite
|
|
42
|
+
# @raise [ArgumentError] If a value has no JSON type, the type is unknown, or a value isn't of the type
|
|
43
|
+
# @raise [ArgumentError] If a Hash value has a key that isn't a String, or contains a value that isn't JSON
|
|
44
|
+
def initialize(name:, values:, required: false, nullable: false, map: nil, type: nil, **schema_options)
|
|
23
45
|
raise ArgumentError, "You can not map enums to the root without a name" if map == "/"
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
raise ArgumentError, "Use const for a single value" if values.length == 1
|
|
46
|
+
|
|
47
|
+
check_values!(values, nullable)
|
|
27
48
|
|
|
28
49
|
@name = name.to_s
|
|
29
50
|
@values = nullable ? values | [nil] : values
|
|
51
|
+
@types = type.nil? ? inferred_types : checked_types(type)
|
|
30
52
|
@required = required
|
|
31
53
|
@nullable = nullable
|
|
32
54
|
@map = map
|
|
33
|
-
@schema_options = schema_options
|
|
55
|
+
@schema_options = schema_options.transform_keys(&:to_s).except("type")
|
|
34
56
|
end
|
|
35
57
|
|
|
36
|
-
# Generate JSON schema definition for this enum
|
|
58
|
+
# Generate JSON schema definition for this enum. Nullable adds "null" to the type.
|
|
37
59
|
#
|
|
38
60
|
# @return [Hash] The schema definition for this enum
|
|
39
61
|
def to_schema
|
|
40
62
|
{
|
|
41
|
-
name => {"enum" => values}.merge(schema_options)
|
|
63
|
+
name => {"type" => schema_type, "enum" => values}.merge(schema_options)
|
|
42
64
|
}
|
|
43
65
|
end
|
|
44
66
|
|
|
@@ -55,7 +77,118 @@ module Verquest
|
|
|
55
77
|
|
|
56
78
|
private
|
|
57
79
|
|
|
58
|
-
attr_reader :values, :schema_options
|
|
80
|
+
attr_reader :values, :types, :schema_options
|
|
81
|
+
|
|
82
|
+
# @param values [Array] The given values
|
|
83
|
+
# @param nullable [Boolean] Whether the enum can be null
|
|
84
|
+
# @raise [ArgumentError] If the values are not an Array, empty, not JSON, not unique, a single value, or include nil
|
|
85
|
+
# without nullable
|
|
86
|
+
def check_values!(values, nullable)
|
|
87
|
+
raise ArgumentError, "Values must be an Array" unless values.is_a?(::Array)
|
|
88
|
+
raise ArgumentError, "Values must not be empty" if values.empty?
|
|
89
|
+
|
|
90
|
+
values.each { |value| check_json!(value, value) }
|
|
91
|
+
raise ArgumentError, "Values must be unique" if values.uniq { |value| json_value(value) }.length != values.length
|
|
92
|
+
raise ArgumentError, "Use const for a single value" if values.length == 1
|
|
93
|
+
raise ArgumentError, "Values include nil, which needs nullable: true" if values.include?(nil) && !nullable
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Checks numbers and walks Hashes; top-level Arrays and other classes are left to the type check
|
|
97
|
+
#
|
|
98
|
+
# @param value [Object] An enum value, or a part of a Hash value
|
|
99
|
+
# @param enum_value [Object] The enum value, for errors
|
|
100
|
+
# @param nested [Boolean] Whether the value is inside a Hash
|
|
101
|
+
# @raise [ArgumentError] If a number isn't finite, or a Hash value has a key or part that isn't JSON
|
|
102
|
+
def check_json!(value, enum_value, nested: false)
|
|
103
|
+
case value
|
|
104
|
+
when ::Numeric then raise ArgumentError, "Enum values must be finite numbers" unless finite?(value)
|
|
105
|
+
when ::Hash then value.each { |key, item| check_key!(key, enum_value) || check_json!(item, enum_value, nested: true) }
|
|
106
|
+
when ::Array then value.each { |item| check_json!(item, enum_value, nested: true) } if nested
|
|
107
|
+
when *JSON_SCALARS then nil
|
|
108
|
+
else
|
|
109
|
+
raise ArgumentError, "Enum value #{enum_value.inspect} contains #{value.inspect}, which is not a JSON value#{symbol_hint(value)}" if nested
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# @param key [Object] A key of a Hash value
|
|
114
|
+
# @param enum_value [Object] The enum value, for errors
|
|
115
|
+
# @return [nil]
|
|
116
|
+
# @raise [ArgumentError] If the key isn't a String
|
|
117
|
+
def check_key!(key, enum_value)
|
|
118
|
+
return if key.is_a?(::String)
|
|
119
|
+
|
|
120
|
+
raise ArgumentError, "Enum value #{enum_value.inspect} has key #{key.inspect}, which is not a String#{symbol_hint(key)}"
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# @param value [Numeric] A number
|
|
124
|
+
# @return [Boolean] Whether JSON can represent the number
|
|
125
|
+
def finite?(value)
|
|
126
|
+
value.real? && value.finite?
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# @param value [Object] The value an error is about
|
|
130
|
+
# @return [String, nil] The hint for a Symbol
|
|
131
|
+
def symbol_hint(value)
|
|
132
|
+
SYMBOL_HINT if value.is_a?(::Symbol)
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# @return [String, Array<String>] The type, with "null" when nullable
|
|
136
|
+
def schema_type
|
|
137
|
+
all = nullable ? types | ["null"] : types
|
|
138
|
+
(all.size == 1) ? all.first : all
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# @return [Array<String>] The types of the non-nil values; integers next to other numerics are numbers
|
|
142
|
+
def inferred_types
|
|
143
|
+
found = values.compact.map { |value| json_type(value) }.uniq
|
|
144
|
+
found.delete("integer") if found.include?("number")
|
|
145
|
+
found
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# VALUE_CLASSES lists "integer" before "number", so an Integer is an integer
|
|
149
|
+
#
|
|
150
|
+
# @param value [Object] A non-nil enum value
|
|
151
|
+
# @return [String] The JSON type of the value
|
|
152
|
+
# @raise [ArgumentError] If the value has no JSON type
|
|
153
|
+
def json_type(value)
|
|
154
|
+
type = VALUE_CLASSES.keys.find { |name| of_type?(value, name) }
|
|
155
|
+
return type if type
|
|
156
|
+
|
|
157
|
+
raise ArgumentError, "Enum value #{value.inspect} is not a String, Numeric, boolean or Hash#{symbol_hint(value)}"
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# @param type [String, Symbol, Array<String, Symbol>] The given type
|
|
161
|
+
# @return [Array<String>] The given types as strings
|
|
162
|
+
# @raise [ArgumentError] If a type is unknown or a value isn't of the types
|
|
163
|
+
def checked_types(type)
|
|
164
|
+
given = Array(type).map(&:to_s).uniq
|
|
165
|
+
if given.empty? || !given.all? { |name| VALUE_CLASSES.key?(name) }
|
|
166
|
+
raise ArgumentError, "Unknown enum type #{type.inspect}, use one of #{VALUE_CLASSES.keys.join(", ")}"
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
invalid = values.compact.reject { |value| given.any? { |name| of_type?(value, name) } }
|
|
170
|
+
raise ArgumentError, "Enum value #{invalid.first.inspect} is not of type #{given.join(" or ")}" unless invalid.empty?
|
|
171
|
+
|
|
172
|
+
given
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
# @param value [Object] A non-nil enum value
|
|
176
|
+
# @param name [String] A JSON type in VALUE_CLASSES
|
|
177
|
+
# @return [Boolean] Whether the value is of the type
|
|
178
|
+
def of_type?(value, name)
|
|
179
|
+
VALUE_CLASSES.fetch(name).any? { |klass| value.is_a?(klass) }
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
# @param value [Object] An enum value, or a part of a Hash value
|
|
183
|
+
# @return [Object] The value as JSON compares it: whole numbers as Integers, also inside Hashes and Arrays
|
|
184
|
+
def json_value(value)
|
|
185
|
+
case value
|
|
186
|
+
when ::Hash then value.transform_values { |item| json_value(item) }
|
|
187
|
+
when ::Array then value.map { |item| json_value(item) }
|
|
188
|
+
when ::Numeric then (finite?(value) && value == value.to_i) ? value.to_i : value
|
|
189
|
+
else value
|
|
190
|
+
end
|
|
191
|
+
end
|
|
59
192
|
end
|
|
60
193
|
end
|
|
61
194
|
end
|
|
@@ -59,7 +59,7 @@ module Verquest
|
|
|
59
59
|
{
|
|
60
60
|
name => {
|
|
61
61
|
"type" => type,
|
|
62
|
-
|
|
62
|
+
**required_keyword,
|
|
63
63
|
"properties" => all_properties.transform_values { |property| property.to_schema[property.name] }
|
|
64
64
|
}.merge(schema_options).tap do |schema|
|
|
65
65
|
schema["dependentRequired"] = dependent_required_properties if dependent_required_properties.any?
|
|
@@ -76,7 +76,7 @@ module Verquest
|
|
|
76
76
|
{
|
|
77
77
|
name => {
|
|
78
78
|
"type" => type,
|
|
79
|
-
|
|
79
|
+
**required_keyword,
|
|
80
80
|
"properties" => all_properties.transform_values { |property| property.to_validation_schema(version: version)[property.name] }
|
|
81
81
|
}.merge(schema_options).tap do |schema|
|
|
82
82
|
schema["dependentRequired"] = dependent_required_properties if dependent_required_properties.any?
|
|
@@ -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
|
|
@@ -106,7 +106,7 @@ module Verquest
|
|
|
106
106
|
when "number" then coerce_number(value)
|
|
107
107
|
when "boolean" then coerce_boolean(value)
|
|
108
108
|
when "null" then (value.nil? || value == "") ? nil : NO_MATCH
|
|
109
|
-
when "string" then
|
|
109
|
+
when "string" then coerce_string(value, value_schema)
|
|
110
110
|
when "array" then coerce_array(value, value_schema, path)
|
|
111
111
|
when "object" then value.is_a?(::Hash) ? coerce_properties(value, value_schema, path) : NO_MATCH
|
|
112
112
|
else NO_MATCH
|
|
@@ -136,6 +136,16 @@ module Verquest
|
|
|
136
136
|
BOOLEANS.fetch(value, NO_MATCH)
|
|
137
137
|
end
|
|
138
138
|
|
|
139
|
+
# A string outside the schema's enum tries the next type, so "" can still map to nil
|
|
140
|
+
#
|
|
141
|
+
# @return [String, Object] The string or NO_MATCH
|
|
142
|
+
def coerce_string(value, value_schema)
|
|
143
|
+
return NO_MATCH unless value.is_a?(::String)
|
|
144
|
+
return NO_MATCH if value_schema.key?("enum") && !value_schema["enum"].include?(value)
|
|
145
|
+
|
|
146
|
+
value
|
|
147
|
+
end
|
|
148
|
+
|
|
139
149
|
# Splits a string by the array's explode value and converts each item;
|
|
140
150
|
# an item no rule converts is kept as it is
|
|
141
151
|
def coerce_array(value, array_schema, path)
|
|
@@ -9,7 +9,7 @@ module Verquest
|
|
|
9
9
|
# (`param[:in]`, `param[:style]`, `param[:schema][:type]`).
|
|
10
10
|
#
|
|
11
11
|
# @example
|
|
12
|
-
# schema = {"type" => "object", "
|
|
12
|
+
# schema = {"type" => "object", "properties" => {"page" => {"type" => "integer"}}}
|
|
13
13
|
# Verquest::Query::Parameters.new(schema:, options: {}).call
|
|
14
14
|
# # => [{name: "page", in: :query, required: false, schema: {type: "integer"}}]
|
|
15
15
|
class Parameters
|
|
@@ -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/version.rb
CHANGED
|
@@ -157,10 +157,13 @@ module Verquest
|
|
|
157
157
|
|
|
158
158
|
# Validate the schema against the metaschema
|
|
159
159
|
#
|
|
160
|
+
# Under `:openapi30` the document (`schema`) is checked, since the validation schema keeps JSON Schema forms
|
|
161
|
+
# OpenAPI 3.0 forbids, such as "null" types.
|
|
162
|
+
#
|
|
160
163
|
# @return [Boolean] true if the schema is valid, false otherwise
|
|
161
164
|
def valid_schema?
|
|
162
165
|
JSONSchemer.valid_schema?(
|
|
163
|
-
|
|
166
|
+
checked_schema,
|
|
164
167
|
meta_schema: Verquest.configuration.json_schema_uri
|
|
165
168
|
)
|
|
166
169
|
end
|
|
@@ -169,13 +172,14 @@ module Verquest
|
|
|
169
172
|
#
|
|
170
173
|
# This method validates the schema against the configured JSON Schema metaschema
|
|
171
174
|
# and returns detailed validation errors if any are found. It uses the JSONSchemer
|
|
172
|
-
# library with the schema version specified in the configuration.
|
|
175
|
+
# library with the schema version specified in the configuration. Under `:openapi30` it checks the document,
|
|
176
|
+
# so the pointers refer to `schema`.
|
|
173
177
|
#
|
|
174
178
|
# @return [Array<Hash>] An array of validation error details, empty if schema is valid
|
|
175
179
|
# @see #valid_schema?
|
|
176
180
|
def validate_schema
|
|
177
181
|
JSONSchemer.validate_schema(
|
|
178
|
-
|
|
182
|
+
checked_schema,
|
|
179
183
|
meta_schema: Verquest.configuration.json_schema_uri
|
|
180
184
|
).map do |error|
|
|
181
185
|
{
|
|
@@ -239,11 +243,11 @@ module Verquest
|
|
|
239
243
|
parameters? ? query_coercer.call(params) : params
|
|
240
244
|
end
|
|
241
245
|
|
|
242
|
-
# Returns OpenAPI parameter objects for this query or path version
|
|
246
|
+
# Returns OpenAPI parameter objects for this query or path version, in OpenAPI 3.0 forms under :openapi30
|
|
243
247
|
#
|
|
244
248
|
# @return [Array<Hash>] A new array of parameter objects on each call
|
|
245
249
|
def parameters
|
|
246
|
-
Query::Parameters.new(schema: validation_schema, options: query_options, location:).call
|
|
250
|
+
Query::Parameters.new(schema: document_schema(validation_schema), options: query_options, location:).call
|
|
247
251
|
end
|
|
248
252
|
|
|
249
253
|
# Map request parameters to internal representation using the transformer
|
|
@@ -341,13 +345,14 @@ module Verquest
|
|
|
341
345
|
def prepare_schema
|
|
342
346
|
@schema = {
|
|
343
347
|
"type" => "object",
|
|
344
|
-
|
|
348
|
+
**required_keyword,
|
|
345
349
|
"properties" => all_properties.transform_values { |property| property.to_schema[property.name] }
|
|
346
350
|
}.merge(schema_options).tap do |schema|
|
|
347
351
|
schema["dependentRequired"] = dependent_required_properties if dependent_required_properties.any?
|
|
348
352
|
schema["description"] = description if description
|
|
349
353
|
add_conditions(schema, documentation: true)
|
|
350
|
-
end
|
|
354
|
+
end
|
|
355
|
+
@schema = document_schema(@schema).freeze
|
|
351
356
|
end
|
|
352
357
|
|
|
353
358
|
# Generates the JSON schema for combination schemas (oneOf at root level)
|
|
@@ -360,7 +365,20 @@ module Verquest
|
|
|
360
365
|
def prepare_combination_schema
|
|
361
366
|
raise ArgumentError, "Conditions can not be declared next to a root-level one_of" if conditions.any?
|
|
362
367
|
|
|
363
|
-
@schema = properties.values.first.to_schema
|
|
368
|
+
@schema = document_schema(properties.values.first.to_schema)
|
|
369
|
+
end
|
|
370
|
+
|
|
371
|
+
# @return [Hash] The schema the meta-schema checks: the document under OpenAPI 3.0, the validation schema otherwise
|
|
372
|
+
def checked_schema
|
|
373
|
+
Verquest.configuration.nullable_keyword? ? schema : validation_schema
|
|
374
|
+
end
|
|
375
|
+
|
|
376
|
+
# Converts a document to OpenAPI 3.0 forms when the configured version needs them
|
|
377
|
+
#
|
|
378
|
+
# @param schema [Hash] The schema in JSON Schema forms
|
|
379
|
+
# @return [Hash] The schema for documents
|
|
380
|
+
def document_schema(schema)
|
|
381
|
+
Verquest.configuration.nullable_keyword? ? Openapi30.document(schema) : schema
|
|
364
382
|
end
|
|
365
383
|
|
|
366
384
|
# Generates the validation schema for this version
|
|
@@ -372,7 +390,7 @@ module Verquest
|
|
|
372
390
|
def prepare_validation_schema
|
|
373
391
|
@validation_schema = {
|
|
374
392
|
"type" => "object",
|
|
375
|
-
|
|
393
|
+
**required_keyword,
|
|
376
394
|
"properties" => all_properties.transform_values { |property| property.to_validation_schema(version: name)[property.name] }
|
|
377
395
|
}.merge(schema_options).tap do |schema|
|
|
378
396
|
schema["dependentRequired"] = dependent_required_properties if dependent_required_properties.any?
|