Module: Hanami::V1::Validations::Form

Included in:
Action::Params
Defined in:
lib/hanami/v1/validations/form.rb

Overview

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.

Examples:

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

Since:

  • 0.6.0

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Override Ruby’s hook for modules.

Parameters:

  • base (Class)

    the target action

See Also:

Since:

  • 0.6.0



51
52
53
54
55
56
# File 'lib/hanami/v1/validations/form.rb', line 51

def self.included(base)
  base.class_eval do
    include Hanami::V1::Validations
    extend  ClassMethods
  end
end