Sha256: e03d5cd475df72c5eed8e47eb04ee7ec8caf70c9993b60eebc06b11df98b2562

Contents?: true

Size: 1.96 KB

Versions: 4

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

module Dry
  module Validation
    # Predicate registry with additional needed methods.
    class PredicateRegistry < Schema::PredicateRegistry
      # List of predicates to be imported by `:predicates_as_macros`
      # extension.
      #
      # @see Dry::Validation::Contract
      WHITELIST = %i[
        filled? format? gt? gteq? included_in? includes? inclusion? is? lt?
        lteq? max_size? min_size? not_eql? odd? respond_to? size? true?
        uuid_v4?
      ].freeze

      # @api private
      def arg_names(name)
        arg_list(name).map(&:first)
      end

      # @api private
      def call(name, args)
        self[name].(*args)
      end

      # @api private
      def message_opts(name, arg_values)
        arg_names(name).zip(arg_values).to_h
      end
    end

    # Extension to use dry-logic predicates as macros.
    #
    # @see Dry::Validation::PredicateRegistry::WHITELIST Available predicates
    #
    # @example
    #   Dry::Validation.load_extensions(:predicates_as_macros)
    #
    #   class ApplicationContract < Dry::Validation::Contract
    #     import_predicates_as_macros
    #   end
    #
    #   class AgeContract < ApplicationContract
    #     schema do
    #       required(:age).filled(:integer)
    #     end
    #
    #     rule(:age).validate(gteq?: 18)
    #   end
    #
    #   AgeContract.new.(age: 17).errors.first.text
    #   # => 'must be greater than or equal to 18'
    #
    # @api public
    class Contract
      # Make macros available for self and its descendants.
      def self.import_predicates_as_macros
        registry = PredicateRegistry.new

        PredicateRegistry::WHITELIST.each do |name|
          register_macro(name) do |macro:|
            predicate_args = [*macro.args, value]
            message_opts = registry.message_opts(name, predicate_args)

            key.failure(name, message_opts) unless registry.(name, predicate_args)
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dry-validation-1.11.1 lib/dry/validation/extensions/predicates_as_macros.rb
dry-validation-1.11.0 lib/dry/validation/extensions/predicates_as_macros.rb
dry-validation-1.10.0 lib/dry/validation/extensions/predicates_as_macros.rb
dry-validation-1.9.0 lib/dry/validation/extensions/predicates_as_macros.rb