Sha256: 5cdd4441e0cd63acf047086afaf1ef7a78c07a9b095323f65e4ed1dfd9f3aef5

Contents?: true

Size: 926 Bytes

Versions: 1

Compression:

Stored size: 926 Bytes

Contents

# frozen_string_literal: true

module Dry
  module Schema
    module Macros
      # Macro used to specify predicates for a value that can be `nil`
      #
      # @api private
      class Maybe < DSL
        # @api private
        def call(*args, **opts, &)
          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, &)
          end

          self
        end

        # @api private
        def to_ast
          [:implication,
           [
             [:not, [:predicate, [:nil?, [[:input, Undefined]]]]],
             trace.to_rule.to_ast
           ]]
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry-schema-1.14.0 lib/dry/schema/macros/maybe.rb