Module: Hanami::Validations::Form

Defined in:
gems/gems/hanami-validations-1.3.4/lib/hanami/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