Sha256: e8a643db19523940b649754e3d81e98889798e41046a2167c68fe1124792683a

Contents?: true

Size: 899 Bytes

Versions: 19

Compression:

Stored size: 899 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

          value(*args, **opts, &block)

          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

19 entries across 19 versions & 1 rubygems

Version Path
dry-schema-1.10.2 lib/dry/schema/macros/maybe.rb
dry-schema-1.10.1 lib/dry/schema/macros/maybe.rb
dry-schema-1.9.3 lib/dry/schema/macros/maybe.rb
dry-schema-1.9.2 lib/dry/schema/macros/maybe.rb
dry-schema-1.9.1 lib/dry/schema/macros/maybe.rb
dry-schema-1.9.0 lib/dry/schema/macros/maybe.rb
dry-schema-1.8.0 lib/dry/schema/macros/maybe.rb
dry-schema-1.7.1 lib/dry/schema/macros/maybe.rb
dry-schema-1.7.0 lib/dry/schema/macros/maybe.rb
dry-schema-1.6.2 lib/dry/schema/macros/maybe.rb
dry-schema-1.6.1 lib/dry/schema/macros/maybe.rb
dry-schema-1.6.0 lib/dry/schema/macros/maybe.rb
dry-schema-1.5.6 lib/dry/schema/macros/maybe.rb
dry-schema-1.5.5 lib/dry/schema/macros/maybe.rb
dry-schema-1.5.4 lib/dry/schema/macros/maybe.rb
dry-schema-1.5.3 lib/dry/schema/macros/maybe.rb
dry-schema-1.5.2 lib/dry/schema/macros/maybe.rb
dry-schema-1.5.1 lib/dry/schema/macros/maybe.rb
dry-schema-1.5.0 lib/dry/schema/macros/maybe.rb