Sha256: 449f2f53eb9bbe7c4a6d9ccc8ee19664cd2f221ad57b11a97c1323e54c729892

Contents?: true

Size: 1.53 KB

Versions: 19

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

require "dry/schema/macros/value"

module Dry
  module Schema
    module Macros
      # Macro used to prepend `:filled?` predicate
      #
      # @api private
      class Filled < Value
        # @api private
        def call(*predicates, **opts, &block)
          ensure_valid_predicates(predicates)

          if opts[:type_spec] && !filter_empty_string?
            value(predicates[0], :filled?, *predicates[1..predicates.size - 1], **opts, &block)
          elsif opts[:type_rule]
            value(:filled?).value(*predicates, **opts, &block)
          else
            value(:filled?, *predicates, **opts, &block)
          end
        end

        # @api private
        def ensure_valid_predicates(predicates)
          if predicates.include?(:empty?)
            raise ::Dry::Schema::InvalidSchemaError, "Using filled with empty? predicate is invalid"
          end

          if predicates.include?(:filled?)
            raise ::Dry::Schema::InvalidSchemaError, "Using filled with filled? is redundant"
          end
        end

        # @api private
        def filter_empty_string?
          !expected_primitives.include?(NilClass) && processor_config.filter_empty_string
        end

        # @api private
        def processor_config
          schema_dsl.processor_type.config
        end

        # @api private
        def expected_primitives
          primitive_inferrer[schema_type]
        end

        # @api private
        def schema_type
          schema_dsl.types[name]
        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/filled.rb
dry-schema-1.10.1 lib/dry/schema/macros/filled.rb
dry-schema-1.9.3 lib/dry/schema/macros/filled.rb
dry-schema-1.9.2 lib/dry/schema/macros/filled.rb
dry-schema-1.9.1 lib/dry/schema/macros/filled.rb
dry-schema-1.9.0 lib/dry/schema/macros/filled.rb
dry-schema-1.8.0 lib/dry/schema/macros/filled.rb
dry-schema-1.7.1 lib/dry/schema/macros/filled.rb
dry-schema-1.7.0 lib/dry/schema/macros/filled.rb
dry-schema-1.6.2 lib/dry/schema/macros/filled.rb
dry-schema-1.6.1 lib/dry/schema/macros/filled.rb
dry-schema-1.6.0 lib/dry/schema/macros/filled.rb
dry-schema-1.5.6 lib/dry/schema/macros/filled.rb
dry-schema-1.5.5 lib/dry/schema/macros/filled.rb
dry-schema-1.5.4 lib/dry/schema/macros/filled.rb
dry-schema-1.5.3 lib/dry/schema/macros/filled.rb
dry-schema-1.5.2 lib/dry/schema/macros/filled.rb
dry-schema-1.5.1 lib/dry/schema/macros/filled.rb
dry-schema-1.5.0 lib/dry/schema/macros/filled.rb