Sha256: aeb68ee757b9347c8137736501361e078cf056ce3d176048a5bbb8536ac02c0d

Contents?: true

Size: 878 Bytes

Versions: 3

Compression:

Stored size: 878 Bytes

Contents

require 'dry/schema/macros/dsl'

module Dry
  module Schema
    module Macros
      # Macro used to specify predicates for a value that can be `nil`
      #
      # @api public
      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

3 entries across 3 versions & 1 rubygems

Version Path
dry-schema-0.2.0 lib/dry/schema/macros/maybe.rb
dry-schema-0.1.1 lib/dry/schema/macros/maybe.rb
dry-schema-0.1.0 lib/dry/schema/macros/maybe.rb