lib/dry/schema/macros/dsl.rb in dry-schema-1.3.4 vs lib/dry/schema/macros/dsl.rb in dry-schema-1.4.0
- old
+ new
@@ -1,8 +1,10 @@
# frozen_string_literal: true
require 'dry/logic/operators'
+require 'dry/types/predicate_inferrer'
+require 'dry/types/primitive_inferrer'
require 'dry/schema/macros/core'
module Dry
module Schema
@@ -24,17 +26,17 @@
# @!attribute [r] predicate_inferrer
# PredicateInferrer is used to infer predicate type-check from a type spec
# @return [PredicateInferrer]
# @api private
- option :predicate_inferrer, default: proc { PredicateInferrer.new(compiler.predicates) }
+ option :predicate_inferrer, default: proc { ::Dry::Types::PredicateInferrer.new(compiler.predicates) }
# @!attribute [r] primitive_inferrer
# PrimitiveInferrer used to get a list of primitive classes from configured type
# @return [PrimitiveInferrer]
# @api private
- option :primitive_inferrer, default: proc { PrimitiveInferrer.new }
+ option :primitive_inferrer, default: proc { ::Dry::Types::PrimitiveInferrer.new }
# @overload value(*predicates, **predicate_opts)
# Set predicates without and with arguments
#
# @param [Array<Symbol>] predicates
@@ -196,13 +198,11 @@
type_spec = nil if is_type_spec
predicates = Array(type_spec ? args[1..-1] : args)
if type_spec
- resolved_type = schema_dsl.resolve_type(
- nullable && !type_spec.is_a?(::Array) ? [:nil, type_spec] : type_spec
- )
+ resolved_type = resolve_type(type_spec, nullable)
type(resolved_type) if set_type
type_predicates = predicate_inferrer[resolved_type]
@@ -210,9 +210,20 @@
return self if predicates.empty?
end
yield(*predicates, type_spec: type_spec)
+ end
+
+ # @api private
+ def resolve_type(type_spec, nullable)
+ resolved = schema_dsl.resolve_type(type_spec)
+
+ if type_spec.is_a?(::Array) || !nullable || resolved.optional?
+ resolved
+ else
+ schema_dsl.resolve_type([:nil, resolved])
+ end
end
end
end
end
end