class Dry::Schema::Macros::Maybe

Macro used to specify predicates for a value that can be `nil`

@api private

Public Instance Methods

call(*args, **opts, &block) click to toggle source

@api private

# File lib/dry/schema/macros/maybe.rb, line 13
def call(*args, **opts, &block)
  if args.include?(:empty?)
    raise ::Dry::Schema::InvalidSchemaError, "Using maybe with empty? predicate is invalid"
  end

  if args.include?(:nil?)
    raise ::Dry::Schema::InvalidSchemaError, "Using maybe with nil? predicate is redundant"
  end

  append_macro(Macros::Value) do |macro|
    macro.call(*args, **opts, &block)
  end

  self
end
to_ast() click to toggle source

@api private

# File lib/dry/schema/macros/maybe.rb, line 30
def to_ast
  [:implication,
   [
     [:not, [:predicate, [:nil?, [[:input, Undefined]]]]],
     trace.to_rule.to_ast
   ]]
end