lib/dry/schema/macros/dsl.rb in dry-schema-1.13.0 vs lib/dry/schema/macros/dsl.rb in dry-schema-1.13.1

- old
+ new

@@ -120,11 +120,10 @@ def schema(...) append_macro(Macros::Schema) do |macro| macro.call(...) end end - ruby2_keywords :schema if respond_to?(:ruby2_keywords, true) # Specify a nested hash with enforced `hash?` type-check # # @example # required(:tags).hash do @@ -135,11 +134,10 @@ def hash(...) append_macro(Macros::Hash) do |macro| macro.call(...) end end - ruby2_keywords :hash if respond_to?(:ruby2_keywords, true) # Specify predicates that should be applied to each element of an array # # This is a simpler building block than `array` macro, use it # when you want to provide `array?` type-check with other rules @@ -159,11 +157,10 @@ def each(...) append_macro(Macros::Each) do |macro| macro.value(...) end end - ruby2_keywords :each if respond_to?(:ruby2_keywords, true) # Like `each` but sets `array?` type-check # # @example a list of strings # required(:tags).array(:str?) @@ -179,11 +176,10 @@ def array(...) append_macro(Macros::Array) do |macro| macro.value(...) end end - ruby2_keywords :array if respond_to?(:ruby2_keywords, true) # Set type spec # # @example # required(:name).type(:string).value(min_size?: 2) @@ -218,10 +214,12 @@ macro end end # @api private + # rubocop: disable Metrics/AbcSize + # rubocop: disable Metrics/CyclomaticComplexity # rubocop: disable Metrics/PerceivedComplexity def extract_type_spec(args, nullable: false, set_type: true) type_spec = args[0] unless schema_or_predicate?(args[0]) predicates = Array(type_spec ? args[1..] : args) @@ -230,10 +228,14 @@ if type_spec resolved_type = resolve_type(type_spec, nullable) if type_spec.is_a?(::Array) type_rule = type_spec.map { |ts| new(chain: false).value(ts) }.reduce(:|) + elsif type_spec.is_a?(Dry::Types::Sum) && set_type + type_rule = [type_spec.left, type_spec.right].map { |ts| + new(klass: Core, chain: false).value(ts) + }.reduce(:|) else type_predicates = predicate_inferrer[resolved_type] predicates.replace(type_predicates + predicates) unless type_predicates.empty? @@ -247,9 +249,11 @@ yield(*predicates, type_spec: nil, type_rule: type_rule) else yield(*predicates, type_spec: type_spec, type_rule: nil) end end + # rubocop: enable Metrics/AbcSize + # rubocop: enable Metrics/CyclomaticComplexity # rubocop: enable Metrics/PerceivedComplexity # @api private def resolve_type(type_spec, nullable) resolved = schema_dsl.resolve_type(type_spec)