Sha256: 844e69e39bb4022624e39c66a5cb36774a3f9081a0c0e09ab6d3e8cf0966e649

Contents?: true

Size: 1.94 KB

Versions: 4

Compression:

Stored size: 1.94 KB

Contents

# frozen_string_literal: true

module Hanami
  module Validations
    # Validations mixin for forms/HTTP params.
    #
    # This must be used when the input comes from a browser or an HTTP endpoint.
    # It knows how to deal with common data types, and common edge cases like blank strings.
    #
    # @since 0.6.0
    #
    # @example
    #   require "hanami/validations/form"
    #
    #   class Signup
    #     include Hanami::Validations::Form
    #
    #     validations do
    #       required(:name).filled(:string)
    #       optional(:location).filled(:string)
    #     end
    #   end
    #
    #   result = Signup.new("location" => "Rome").validate
    #   result.success? # => false
    #
    #   result = Signup.new("name" => "Luca").validate
    #   result.success? # => true
    #
    #   # it works with symbol keys too
    #   result = Signup.new(location: "Rome").validate
    #   result.success? # => false
    #
    #   result = Signup.new(name: "Luca").validate
    #   result.success? # => true
    #
    #   result = Signup.new(name: "Luca", location: "Rome").validate
    #   result.success? # => true
    module Form
      # @since 2.0.0
      # @api private
      class BaseValidator < Dry::Validation::Contract
        params do
          optional(:_csrf_token).filled(:string)
        end
      end

      # Override Ruby's hook for modules.
      #
      # @param klass [Class] the target action
      #
      # @since 0.6.0
      # @api private
      #
      # @see http://www.ruby-doc.org/core/Module.html#method-i-included
      def self.included(klass)
        super

        klass.class_eval do
          include ::Hanami::Validations
          extend ClassMethods
        end
      end

      # @since 0.6.0
      # @api private
      module ClassMethods
        # @since 2.0.0
        # @api private
        def validations(&blk)
          @_validator = Class.new(BaseValidator) { params(&blk) }.new
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hanami-validations-2.0.0.rc1 lib/hanami/validations/form.rb
hanami-validations-2.0.0.beta4 lib/hanami/validations/form.rb
hanami-validations-2.0.0.beta1 lib/hanami/validations/form.rb
hanami-validations-2.0.0.alpha1 lib/hanami/validations/form.rb