verquest 0.6.4 → 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 +57 -1
- data/README.md +325 -45
- data/Rakefile +5 -0
- data/docs/rails.md +169 -0
- data/lib/verquest/base/condition_class_methods.rb +211 -0
- data/lib/verquest/base/private_class_methods.rb +84 -20
- data/lib/verquest/base/public_class_methods.rb +51 -1
- data/lib/verquest/base.rb +1 -0
- data/lib/verquest/conditions/branch.rb +111 -0
- data/lib/verquest/conditions/condition.rb +189 -0
- data/lib/verquest/conditions/conjunction.rb +39 -0
- data/lib/verquest/conditions/fragment_merge.rb +52 -0
- data/lib/verquest/conditions/property_merge.rb +47 -0
- data/lib/verquest/conditions/rule_set.rb +74 -0
- data/lib/verquest/conditions/rules/absent.rb +41 -0
- data/lib/verquest/conditions/rules/alternatives.rb +28 -0
- data/lib/verquest/conditions/rules/any.rb +43 -0
- data/lib/verquest/conditions/rules/equals.rb +22 -0
- data/lib/verquest/conditions/rules/names_rule.rb +23 -0
- data/lib/verquest/conditions/rules/none.rb +40 -0
- data/lib/verquest/conditions/rules/present.rb +38 -0
- data/lib/verquest/conditions/rules/property_rule.rb +66 -0
- data/lib/verquest/conditions/rules/satisfies.rb +22 -0
- data/lib/verquest/conditions/rules/value_in.rb +22 -0
- data/lib/verquest/conditions/rules.rb +17 -0
- data/lib/verquest/conditions/variant_shape.rb +39 -0
- data/lib/verquest/conditions.rb +36 -0
- data/lib/verquest/configuration.rb +24 -6
- data/lib/verquest/gem_version.rb +1 -1
- data/lib/verquest/helper_methods/conditional_properties.rb +126 -0
- data/lib/verquest/helper_methods/unnamed_one_of.rb +35 -0
- data/lib/verquest/properties/array.rb +15 -1
- data/lib/verquest/properties/base.rb +104 -10
- data/lib/verquest/properties/collection.rb +63 -19
- data/lib/verquest/properties/const.rb +2 -1
- data/lib/verquest/properties/object.rb +38 -4
- data/lib/verquest/properties/one_of/discriminated_schema.rb +84 -0
- data/lib/verquest/properties/one_of.rb +87 -45
- data/lib/verquest/properties/reference.rb +61 -17
- data/lib/verquest/query/coercer.rb +165 -0
- data/lib/verquest/query/definition_check.rb +91 -0
- data/lib/verquest/query/parameters.rb +79 -0
- data/lib/verquest/query.rb +25 -0
- data/lib/verquest/supported_versions.rb +73 -0
- data/lib/verquest/transformer/collection.rb +32 -0
- data/lib/verquest/transformer/const_as_enum.rb +47 -0
- data/lib/verquest/transformer/layout.rb +83 -0
- data/lib/verquest/transformer/mapping_layout.rb +166 -0
- data/lib/verquest/transformer/one_of.rb +111 -0
- data/lib/verquest/transformer/path.rb +188 -0
- data/lib/verquest/transformer/plan.rb +183 -0
- data/lib/verquest/transformer/scope.rb +64 -0
- data/lib/verquest/transformer.rb +43 -880
- data/lib/verquest/version.rb +152 -79
- data/lib/verquest/versions.rb +7 -0
- data/lib/verquest.rb +7 -0
- metadata +44 -7
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Verquest
|
|
4
|
+
# Support for query and path requests (`location :query`, `location :path`)
|
|
5
|
+
#
|
|
6
|
+
# Query strings and paths carry only strings and flat keys, so these versions
|
|
7
|
+
# convert their params before validating them ({Query::Coercer}), reject
|
|
8
|
+
# shapes their location can't express ({Query::DefinitionCheck}), and
|
|
9
|
+
# document their properties as OpenAPI parameters ({Query::Parameters}).
|
|
10
|
+
#
|
|
11
|
+
# @example
|
|
12
|
+
# class ListQuery < Verquest::Base
|
|
13
|
+
# location :query
|
|
14
|
+
#
|
|
15
|
+
# version "2025-06" do
|
|
16
|
+
# field :page, type: :integer
|
|
17
|
+
# array :status, type: :string, explode: false
|
|
18
|
+
# end
|
|
19
|
+
# end
|
|
20
|
+
#
|
|
21
|
+
# ListQuery.process({"page" => "2", "status" => "open,won"}, version: "2025-06")
|
|
22
|
+
# # => {"page" => 2, "status" => ["open", "won"]}
|
|
23
|
+
module Query
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -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
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Verquest
|
|
4
|
+
class Transformer
|
|
5
|
+
# A collection whose items are transformed one by one, because they hold a oneOf
|
|
6
|
+
#
|
|
7
|
+
# @!attribute [r] source
|
|
8
|
+
# @return [Path] The collection in the data of the parent scope
|
|
9
|
+
# @!attribute [r] target
|
|
10
|
+
# @return [Path] The collection in the result of the parent scope
|
|
11
|
+
# @!attribute [r] scope
|
|
12
|
+
# @return [Scope] The mapping of each item, with item-level paths
|
|
13
|
+
#
|
|
14
|
+
# Internal to the transformer, not part of the public API.
|
|
15
|
+
Collection = Data.define(:source, :target, :scope) do
|
|
16
|
+
# Transforms the collection items into the result
|
|
17
|
+
#
|
|
18
|
+
# A null collection, or one below a null parent, stays null.
|
|
19
|
+
#
|
|
20
|
+
# @param data [Object] The data of the parent scope
|
|
21
|
+
# @param result [Hash] The result of the parent scope
|
|
22
|
+
# @return [Hash] The result
|
|
23
|
+
# @raise [Verquest::MappingError] If an item's oneOf matches no variant or more than one
|
|
24
|
+
def call(data, result)
|
|
25
|
+
items = source.extract(data, nulls: true)
|
|
26
|
+
return result if items.equal?(Path::NOT_FOUND)
|
|
27
|
+
|
|
28
|
+
target.set(result, items.is_a?(Array) ? items.map { |item| scope.call(item) } : items)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Verquest
|
|
4
|
+
class Transformer
|
|
5
|
+
# Rewrites `const` as a one-value `enum` in a schema
|
|
6
|
+
#
|
|
7
|
+
# Draft 4 and OpenAPI 3.0 have no `const`, so variants that differ only by
|
|
8
|
+
# a const would all match during variant inference. A one-value `enum`
|
|
9
|
+
# means the same and works in every JSON Schema version.
|
|
10
|
+
#
|
|
11
|
+
# @example
|
|
12
|
+
# ConstAsEnum.call({"properties" => {"const" => {"const" => "a"}}})
|
|
13
|
+
# # => {"properties" => {"const" => {"enum" => ["a"]}}}
|
|
14
|
+
#
|
|
15
|
+
# Internal to the transformer, not part of the public API.
|
|
16
|
+
module ConstAsEnum
|
|
17
|
+
# Keywords whose value is a map from names to schemas
|
|
18
|
+
SCHEMA_MAPS = %w[properties patternProperties definitions $defs dependentSchemas].freeze
|
|
19
|
+
|
|
20
|
+
# Keywords whose value is data, not a schema
|
|
21
|
+
DATA_KEYWORDS = %w[enum default examples].freeze
|
|
22
|
+
|
|
23
|
+
module_function
|
|
24
|
+
|
|
25
|
+
# @param schema [Object] The schema, or a part of it
|
|
26
|
+
# @return [Object] The schema with every `const` rewritten
|
|
27
|
+
def call(schema)
|
|
28
|
+
case schema
|
|
29
|
+
when Hash then schema.to_h { |keyword, value| rewrite(keyword, value) }
|
|
30
|
+
when Array then schema.map { |item| call(item) }
|
|
31
|
+
else schema
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# @param keyword [String] The schema keyword
|
|
36
|
+
# @param value [Object] Its value
|
|
37
|
+
# @return [Array(String, Object)] The rewritten keyword and value
|
|
38
|
+
def rewrite(keyword, value)
|
|
39
|
+
return ["enum", [value]] if keyword == "const"
|
|
40
|
+
return [keyword, value] if DATA_KEYWORDS.include?(keyword)
|
|
41
|
+
return [keyword, value.transform_values { |subschema| call(subschema) }] if SCHEMA_MAPS.include?(keyword) && value.is_a?(Hash)
|
|
42
|
+
|
|
43
|
+
[keyword, call(value)]
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Verquest
|
|
4
|
+
class Transformer
|
|
5
|
+
# The source and target paths of a request, before they are grouped into scopes
|
|
6
|
+
#
|
|
7
|
+
# Properties add themselves with full paths from the params root
|
|
8
|
+
# (Properties::Base#layout), and Plan groups them into the scopes the
|
|
9
|
+
# transformer applies. A oneOf variant gets its own layout, so it can hold
|
|
10
|
+
# oneOfs and collections too. Its paths are full paths as well, like the
|
|
11
|
+
# parent's. Each variant gets a copy of its parent's collections: Plan looks
|
|
12
|
+
# up a collection's target by its source path, and same-named collections
|
|
13
|
+
# in different variants can have different targets.
|
|
14
|
+
#
|
|
15
|
+
# A layout is filled while a version is prepared, then only read; the
|
|
16
|
+
# hashes and arrays its readers return are not meant to be changed.
|
|
17
|
+
#
|
|
18
|
+
# @example
|
|
19
|
+
# layout = Layout.new
|
|
20
|
+
# layout.add_pair("user/firstName", "user/first_name")
|
|
21
|
+
# layout.add_collection("items[]", "lines[]")
|
|
22
|
+
#
|
|
23
|
+
# Internal to the transformer, not part of the public API.
|
|
24
|
+
class Layout
|
|
25
|
+
# A oneOf with full paths
|
|
26
|
+
#
|
|
27
|
+
# The paths are Strings; Plan turns them into Paths relative to the
|
|
28
|
+
# scope that holds the oneOf. The variant layouts hold full paths too.
|
|
29
|
+
#
|
|
30
|
+
# @!attribute [r] path
|
|
31
|
+
# @return [String] The oneOf source path, empty for a root-level oneOf
|
|
32
|
+
# @!attribute [r] discriminator
|
|
33
|
+
# @return [String, nil] The discriminator source path
|
|
34
|
+
# @!attribute [r] variants
|
|
35
|
+
# @return [Hash<String, Layout>] The layout of each variant by name
|
|
36
|
+
# @!attribute [r] variant_schemas
|
|
37
|
+
# @return [Hash<String, Hash>, nil] Validation schemas by variant name, without a discriminator
|
|
38
|
+
# @!attribute [r] null_target
|
|
39
|
+
# @return [String, nil] Where a null oneOf goes, nil for its source path
|
|
40
|
+
OneOfLayout = Data.define(:path, :discriminator, :variants, :variant_schemas, :null_target)
|
|
41
|
+
|
|
42
|
+
# @!attribute [r] pairs
|
|
43
|
+
# @return [Hash<String, String>] Target path by source path
|
|
44
|
+
# @!attribute [r] one_ofs
|
|
45
|
+
# @return [Array<OneOfLayout>] The oneOfs
|
|
46
|
+
# @!attribute [r] collections
|
|
47
|
+
# @return [Hash<String, String>] Target path by source path of each collection, both ending with "[]"
|
|
48
|
+
attr_reader :pairs, :one_ofs, :collections
|
|
49
|
+
|
|
50
|
+
# @param collections [Hash<String, String>] The collections, shared with the parent layout
|
|
51
|
+
def initialize(collections: {})
|
|
52
|
+
@pairs = {}
|
|
53
|
+
@one_ofs = []
|
|
54
|
+
@collections = collections
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# @param source [String] The source path
|
|
58
|
+
# @param target [String] The target path
|
|
59
|
+
# @return [void]
|
|
60
|
+
def add_pair(source, target)
|
|
61
|
+
pairs[source] = target
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# @param one_of [OneOfLayout] The oneOf
|
|
65
|
+
# @return [void]
|
|
66
|
+
def add_one_of(one_of)
|
|
67
|
+
one_ofs << one_of
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# @param source [String] The collection source path, ending with "[]"
|
|
71
|
+
# @param target [String] The collection target path, ending with "[]"
|
|
72
|
+
# @return [void]
|
|
73
|
+
def add_collection(source, target)
|
|
74
|
+
collections[source] = target
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# @return [Layout] An empty layout for a oneOf variant, with a copy of the collections
|
|
78
|
+
def variant
|
|
79
|
+
Layout.new(collections: collections.dup)
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Verquest
|
|
4
|
+
class Transformer
|
|
5
|
+
# Turns a mapping hash into a Layout
|
|
6
|
+
#
|
|
7
|
+
# A mapping is flat, holds a single oneOf (variant mappings that repeat
|
|
8
|
+
# the other properties, plus metadata keys), or several oneOfs (`_oneOfs`).
|
|
9
|
+
# The oneOf paths and collection targets the hash doesn't hold are worked
|
|
10
|
+
# out from its paths. Only the deprecated `Transformer.new(mapping:)` uses
|
|
11
|
+
# it; versions build their layout from their properties.
|
|
12
|
+
#
|
|
13
|
+
# @deprecated Removed in 1.0.0 together with `Transformer.new(mapping:)`
|
|
14
|
+
#
|
|
15
|
+
# Internal to the transformer, not part of the public API.
|
|
16
|
+
class MappingLayout
|
|
17
|
+
# Builds the layout of a mapping
|
|
18
|
+
#
|
|
19
|
+
# @param mapping [Hash] The mapping in any of its formats
|
|
20
|
+
# @param discriminator [String, Symbol, nil] The discriminator of a root-level oneOf
|
|
21
|
+
# @return [Layout] The layout
|
|
22
|
+
def self.call(mapping, discriminator: nil)
|
|
23
|
+
new(mapping, discriminator&.to_s).layout
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
private_class_method :new
|
|
27
|
+
|
|
28
|
+
# @!attribute [r] layout
|
|
29
|
+
# @return [Layout] The layout of the mapping
|
|
30
|
+
attr_reader :layout
|
|
31
|
+
|
|
32
|
+
# @param mapping [Hash] The mapping in any of its formats
|
|
33
|
+
# @param discriminator [String, nil] The discriminator of a root-level oneOf
|
|
34
|
+
def initialize(mapping, discriminator)
|
|
35
|
+
@layout = Layout.new
|
|
36
|
+
pairs, one_ofs = split(mapping, discriminator)
|
|
37
|
+
pairs.each { |source, target| layout.add_pair(source, target) }
|
|
38
|
+
one_ofs.each { |one_of| layout.add_one_of(one_of) }
|
|
39
|
+
one_ofs.each { |one_of| add_collections(one_of.path, one_ofs, pairs) }
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
private
|
|
43
|
+
|
|
44
|
+
# Splits a mapping into its source and target pairs and its oneOfs
|
|
45
|
+
#
|
|
46
|
+
# @param mapping [Hash] The mapping in any of its formats
|
|
47
|
+
# @param discriminator [String, nil] The discriminator of a root-level oneOf
|
|
48
|
+
# @return [Array(Hash, Array<Layout::OneOfLayout>)] The pairs and the oneOfs
|
|
49
|
+
def split(mapping, discriminator)
|
|
50
|
+
pairs = mapping.reject { |key, value| METADATA_KEYS.include?(key) || value.is_a?(Hash) }
|
|
51
|
+
return [pairs, mapping["_oneOfs"].map { |one_of| one_of_layout(one_of) }] if mapping.key?("_oneOfs")
|
|
52
|
+
return [pairs, []] unless discriminator || mapping.key?("_discriminator") || mapping.key?("_variant_schemas")
|
|
53
|
+
|
|
54
|
+
single = mapping.except(*pairs.keys)
|
|
55
|
+
single = single.merge("_discriminator" => discriminator) if discriminator
|
|
56
|
+
one_of = one_of_layout(single)
|
|
57
|
+
[pairs.merge(shared_pairs(single, one_of.path)), [one_of]]
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Builds the layout of a oneOf mapping
|
|
61
|
+
#
|
|
62
|
+
# @param mapping [Hash] The oneOf mapping, with metadata keys
|
|
63
|
+
# @return [Layout::OneOfLayout] The oneOf layout
|
|
64
|
+
def one_of_layout(mapping)
|
|
65
|
+
variants = variant_mappings(mapping)
|
|
66
|
+
path = one_of_path(mapping, variants)
|
|
67
|
+
discriminator = mapping["_discriminator"]
|
|
68
|
+
|
|
69
|
+
Layout::OneOfLayout.new(
|
|
70
|
+
path:,
|
|
71
|
+
discriminator:,
|
|
72
|
+
variants: variants.transform_values { |pairs| variant_layout(pairs, path) },
|
|
73
|
+
variant_schemas: (mapping["_variant_schemas"] unless discriminator),
|
|
74
|
+
null_target: mapping["_nullable_target_path"]
|
|
75
|
+
)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Builds the layout of a variant from its pairs inside the oneOf
|
|
79
|
+
#
|
|
80
|
+
# @param pairs [Hash] The variant mapping
|
|
81
|
+
# @param path [String] The oneOf source path
|
|
82
|
+
# @return [Layout] The variant layout
|
|
83
|
+
def variant_layout(pairs, path)
|
|
84
|
+
layout.variant.tap do |variant|
|
|
85
|
+
# A Hash target is a oneOf nested in the variant, which the mapping formats can't express
|
|
86
|
+
pairs.each { |source, target| variant.add_pair(source, target) if target.is_a?(String) && inside?(source, path) }
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Returns the variant mappings of a oneOf mapping
|
|
91
|
+
#
|
|
92
|
+
# @param mapping [Hash] The oneOf mapping
|
|
93
|
+
# @return [Hash<String, Hash>] Source and target pairs by variant name
|
|
94
|
+
def variant_mappings(mapping)
|
|
95
|
+
mapping.select { |key, value| !METADATA_KEYS.include?(key) && value.is_a?(Hash) }
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# Returns the pairs a single-oneOf mapping repeats in every variant
|
|
99
|
+
#
|
|
100
|
+
# @param mapping [Hash] The oneOf mapping
|
|
101
|
+
# @param path [String] The oneOf source path
|
|
102
|
+
# @return [Hash] The pairs outside the oneOf property
|
|
103
|
+
def shared_pairs(mapping, path)
|
|
104
|
+
return {} if path.empty?
|
|
105
|
+
|
|
106
|
+
variant_mappings(mapping).values.first.reject { |source, _| inside?(source, path) }
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# Returns the source path of a oneOf property
|
|
110
|
+
#
|
|
111
|
+
# Taken from the discriminator, the variant path or the nullable path.
|
|
112
|
+
# Otherwise, the oneOf is at the root, or at the items of the collection
|
|
113
|
+
# all its properties are in.
|
|
114
|
+
#
|
|
115
|
+
# @param mapping [Hash] The oneOf mapping
|
|
116
|
+
# @param variants [Hash<String, Hash>] Source and target pairs by variant name
|
|
117
|
+
# @return [String] The source path, empty for a root-level oneOf
|
|
118
|
+
def one_of_path(mapping, variants)
|
|
119
|
+
return mapping["_discriminator"].rpartition("/").first if mapping["_discriminator"]
|
|
120
|
+
return mapping["_variant_path"] || mapping["_nullable_path"] if mapping.key?("_variant_path") || mapping.key?("_nullable_path")
|
|
121
|
+
|
|
122
|
+
sources = variants.values.flat_map(&:keys)
|
|
123
|
+
collection = sources.first.include?("[]") ? "#{sources.first.split("[]").first}[]" : ""
|
|
124
|
+
(!collection.empty? && sources.all? { |source| inside?(source, collection) }) ? collection : ""
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# Adds the collections a oneOf path enters, with their target paths
|
|
128
|
+
#
|
|
129
|
+
# @param path [String] The oneOf source path
|
|
130
|
+
# @param one_ofs [Array<Layout::OneOfLayout>] All oneOfs
|
|
131
|
+
# @param pairs [Hash] The source and target pairs outside the oneOfs
|
|
132
|
+
# @return [void]
|
|
133
|
+
def add_collections(path, one_ofs, pairs)
|
|
134
|
+
1.upto(path.scan("[]").size) do |depth|
|
|
135
|
+
prefix = "#{path.split("[]").first(depth).join("[]")}[]"
|
|
136
|
+
layout.add_collection(prefix, target_collection(prefix, one_ofs, pairs)) unless layout.collections.key?(prefix)
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# Returns the target path of a collection, which differs when it uses map:
|
|
141
|
+
#
|
|
142
|
+
# Found from a property mapped inside the collection, so a property moved
|
|
143
|
+
# out of it with map: "/..." doesn't count.
|
|
144
|
+
#
|
|
145
|
+
# @param prefix [String] The collection source path, ending with "[]"
|
|
146
|
+
# @param one_ofs [Array<Layout::OneOfLayout>] All oneOfs
|
|
147
|
+
# @param pairs [Hash] The source and target pairs outside the oneOfs
|
|
148
|
+
# @return [String] The collection target path, ending with "[]"
|
|
149
|
+
def target_collection(prefix, one_ofs, pairs)
|
|
150
|
+
depth = prefix.scan("[]").size
|
|
151
|
+
candidates = one_ofs.flat_map { |one_of| one_of.variants.values.flat_map { |variant| variant.pairs.to_a } } + pairs.to_a
|
|
152
|
+
_, target = candidates.find { |source, target_path| inside?(source, prefix) && target_path.scan("[]/").size >= depth }
|
|
153
|
+
target ? "#{target.split("[]/").first(depth).join("[]/")}[]" : prefix
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
# Checks whether a path is inside another one
|
|
157
|
+
#
|
|
158
|
+
# @param path [String] The path
|
|
159
|
+
# @param prefix [String] The containing path, empty for the root
|
|
160
|
+
# @return [Boolean] True if the path is below the prefix
|
|
161
|
+
def inside?(path, prefix)
|
|
162
|
+
prefix.empty? || path.start_with?("#{prefix}/")
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
end
|
|
166
|
+
end
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Verquest
|
|
4
|
+
class Transformer
|
|
5
|
+
# One oneOf in a mapping, with the variant mapping each input selects
|
|
6
|
+
#
|
|
7
|
+
# Paths are relative to the scope the oneOf is resolved in: the params, or
|
|
8
|
+
# a collection item.
|
|
9
|
+
#
|
|
10
|
+
# @example
|
|
11
|
+
# one_of = OneOf.new(
|
|
12
|
+
# path: Path.new("pet"),
|
|
13
|
+
# discriminator: Path.new("pet/type"),
|
|
14
|
+
# variants: {"dog" => Scope.new(pairs: [[Path.new("pet/bark"), Path.new("pet/bark")]], one_ofs: [], collections: [])}
|
|
15
|
+
# )
|
|
16
|
+
# one_of.variant_for({"pet" => {"type" => "dog", "bark" => true}}) # => the "dog" scope
|
|
17
|
+
#
|
|
18
|
+
# Internal to the transformer, not part of the public API.
|
|
19
|
+
class OneOf
|
|
20
|
+
# An empty list of pairs, oneOfs or collections
|
|
21
|
+
NONE = [].freeze
|
|
22
|
+
|
|
23
|
+
# The variant of an absent oneOf
|
|
24
|
+
NO_VARIANT = Scope.new(pairs: NONE, one_ofs: NONE, collections: NONE)
|
|
25
|
+
|
|
26
|
+
# Initialize a new OneOf
|
|
27
|
+
#
|
|
28
|
+
# @param path [Path] The oneOf property, empty for a oneOf at the scope root
|
|
29
|
+
# @param variants [Hash<String, Scope>] The scope of each variant by name
|
|
30
|
+
# @param discriminator [Path, nil] The discriminator property
|
|
31
|
+
# @param variant_schemas [Hash<String, Hash>, nil] Validation schemas by variant name, without a discriminator
|
|
32
|
+
# @param null_target [Path, nil] Where a null oneOf goes, defaults to the oneOf path
|
|
33
|
+
def initialize(path:, variants:, discriminator: nil, variant_schemas: nil, null_target: nil)
|
|
34
|
+
@path = path
|
|
35
|
+
@variants = variants
|
|
36
|
+
@discriminator = discriminator
|
|
37
|
+
@schemers = variant_schemas&.transform_values { |schema| schemer(schema) }
|
|
38
|
+
@null_variant = path.empty? ? NO_VARIANT : Scope.new(pairs: [[path, null_target || path].freeze].freeze, one_ofs: NONE, collections: NONE)
|
|
39
|
+
freeze
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Returns the scope of the variant the data selects
|
|
43
|
+
#
|
|
44
|
+
# An absent oneOf maps nothing, and a null one maps only the null.
|
|
45
|
+
#
|
|
46
|
+
# @param data [Object] The scope data holding the oneOf
|
|
47
|
+
# @return [Scope] The variant scope
|
|
48
|
+
# @raise [Verquest::MappingError] If no variant or more than one variant matches
|
|
49
|
+
def variant_for(data)
|
|
50
|
+
value = path.extract(data)
|
|
51
|
+
return NO_VARIANT if value.equal?(Path::NOT_FOUND)
|
|
52
|
+
return null_variant if value.nil?
|
|
53
|
+
|
|
54
|
+
variants.fetch(discriminator ? discriminated_variant(data) : inferred_variant(value))
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
private
|
|
58
|
+
|
|
59
|
+
# @!attribute [r] path
|
|
60
|
+
# @return [Path] The oneOf property
|
|
61
|
+
# @!attribute [r] variants
|
|
62
|
+
# @return [Hash<String, Scope>] The scope of each variant by name
|
|
63
|
+
# @!attribute [r] discriminator
|
|
64
|
+
# @return [Path, nil] The discriminator property
|
|
65
|
+
# @!attribute [r] schemers
|
|
66
|
+
# @return [Hash<String, JSONSchemer::Schema>, nil] Schemers by variant name
|
|
67
|
+
# @!attribute [r] null_variant
|
|
68
|
+
# @return [Scope] The variant of a null oneOf
|
|
69
|
+
attr_reader :path, :variants, :discriminator, :schemers, :null_variant
|
|
70
|
+
|
|
71
|
+
# Compiles a variant schema in the configured JSON Schema version, like the validation schema
|
|
72
|
+
#
|
|
73
|
+
# @param schema [Hash] The variant validation schema
|
|
74
|
+
# @return [JSONSchemer::Schema] The schemer
|
|
75
|
+
def schemer(schema)
|
|
76
|
+
configuration = Verquest.configuration
|
|
77
|
+
schema = ConstAsEnum.call(schema) unless configuration.const_keyword?
|
|
78
|
+
JSONSchemer.schema(schema, meta_schema: configuration.json_schema_uri)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# @param data [Object] The scope data holding the oneOf
|
|
82
|
+
# @return [String] The variant named by the discriminator value
|
|
83
|
+
# @raise [Verquest::MappingError] If the value is missing or unknown
|
|
84
|
+
def discriminated_variant(data)
|
|
85
|
+
value = discriminator.extract(data)
|
|
86
|
+
raise Verquest::MappingError, "Missing discriminator \"#{discriminator}\" for oneOf." if value.nil? || value.equal?(Path::NOT_FOUND)
|
|
87
|
+
|
|
88
|
+
name = value.to_s
|
|
89
|
+
return name if variants.key?(name)
|
|
90
|
+
|
|
91
|
+
raise Verquest::MappingError, "Unknown discriminator value \"#{value}\" for oneOf. No matching variant found."
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# @param value [Object] The oneOf value
|
|
95
|
+
# @return [String] The only variant whose schema the value matches
|
|
96
|
+
# @raise [Verquest::MappingError] If no variant or more than one variant matches
|
|
97
|
+
def inferred_variant(value)
|
|
98
|
+
matches = []
|
|
99
|
+
schemers.each do |name, schemer|
|
|
100
|
+
matches << name if schemer.valid?(value)
|
|
101
|
+
break if matches.size == 2 # Two matches are enough to know the input is ambiguous
|
|
102
|
+
end
|
|
103
|
+
return matches.first if matches.one?
|
|
104
|
+
raise Verquest::MappingError, "No matching schema found for oneOf. Input does not match any of the defined schemas." if matches.empty?
|
|
105
|
+
|
|
106
|
+
raise Verquest::MappingError, "Ambiguous oneOf match. Input matches multiple schemas: #{matches.join(", ")}. " \
|
|
107
|
+
"Consider adding a discriminator or making schemas mutually exclusive."
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|