Sha256: 6ee6fbbeb89ff2279a381a2eaa507105922ea84955c5802d1eaa5e455fe7e4c6

Contents?: true

Size: 1.68 KB

Versions: 19

Compression:

Stored size: 1.68 KB

Contents

require 'hanami/validations'

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(:str?)
    #       optional(:location).filled(:str?)
    #     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
      # Override Ruby's hook for modules.
      #
      # @param base [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(base)
        base.class_eval do
          include Validations
          extend  ClassMethods
        end
      end

      # @since 0.6.0
      # @api private
      module ClassMethods
        private

        # @since 0.6.0
        # @api private
        def _schema_type
          :Form
        end
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
hanami-validations-1.3.6 lib/hanami/validations/form.rb
hanami-validations-1.3.5 lib/hanami/validations/form.rb
hanami-validations-1.3.4 lib/hanami/validations/form.rb
hanami-validations-1.3.3 lib/hanami/validations/form.rb
hanami-validations-1.3.2 lib/hanami/validations/form.rb
hanami-validations-1.3.1 lib/hanami/validations/form.rb
hanami-validations-1.3.0 lib/hanami/validations/form.rb
hanami-validations-1.3.0.beta1 lib/hanami/validations/form.rb
hanami-validations-1.2.2 lib/hanami/validations/form.rb
hanami-validations-1.2.0 lib/hanami/validations/form.rb
hanami-validations-1.2.0.rc2 lib/hanami/validations/form.rb
hanami-validations-1.2.0.rc1 lib/hanami/validations/form.rb
hanami-validations-1.2.0.beta2 lib/hanami/validations/form.rb
hanami-validations-1.2.0.beta1 lib/hanami/validations/form.rb
hanami-validations-1.1.0 lib/hanami/validations/form.rb
hanami-validations-1.1.0.rc1 lib/hanami/validations/form.rb
hanami-validations-1.1.0.beta3 lib/hanami/validations/form.rb
hanami-validations-1.1.0.beta2 lib/hanami/validations/form.rb
hanami-validations-1.1.0.beta1 lib/hanami/validations/form.rb