Sha256: 618288ef9f0b37768102babea1bae007e8f2072d31c1eff60e9a548417ba122e

Contents?: true

Size: 969 Bytes

Versions: 4

Compression:

Stored size: 969 Bytes

Contents

# frozen_string_literal: true

require "dry/schema/macros/dsl"

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, &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

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dry-schema-1.10.6 lib/dry/schema/macros/maybe.rb
dry-schema-1.10.5 lib/dry/schema/macros/maybe.rb
dry-schema-1.10.4 lib/dry/schema/macros/maybe.rb
dry-schema-1.10.3 lib/dry/schema/macros/maybe.rb