Module: Hanami::V1::Validations::ClassMethods

Defined in:
lib/hanami/v1/validations.rb

Overview

Validations DSL

Since:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#messages(type) ⇒ Object

Define the type of engine for error messages.

Accepted values are ‘:yaml` (default), `:i18n`.

Examples:

require 'hanami/validations'

class Signup
  include Hanami::Validations

  messages :i18n
end

Parameters:

  • type (Symbol)

    the preferred engine

Since:

  • 0.6.0



209
210
211
# File 'lib/hanami/v1/validations.rb', line 209

def messages(type)
  self._messages = type
end

#messages_path(path) ⇒ Object

Define the path where to find translation file

Examples:

require 'hanami/validations'

class Signup
  include Hanami::Validations

  messages_path 'config/messages.yml'
end

Parameters:

  • path (String)

    path to translation file

Since:

  • 0.6.0



227
228
229
# File 'lib/hanami/v1/validations.rb', line 227

def messages_path(path)
  self._messages_path = path
end

#namespace(name = nil) ⇒ Object

Namespace for error messages.

Examples:

require 'hanami/validations'

module MyApp
  module Validators
    class Signup
      include Hanami::Validations

      namespace 'signup'
    end
  end
end

# Instead of looking for error messages under the `my_app.validator.signup`
# namespace, it will look just for `signup`.
#
# This helps to simplify YAML files where are stored error messages

Parameters:

  • name (String) (defaults to: nil)

    namespace

Since:

  • 0.6.0



254
255
256
257
258
259
260
# File 'lib/hanami/v1/validations.rb', line 254

def namespace(name = nil)
  if name.nil?
    Namespace.new(_namespace, self)
  else
    self._namespace = name.to_s
  end
end

#predicate(name, message: "is invalid", &blk) ⇒ Object

Define an inline predicate

Examples:

Without Custom Message

require 'hanami/validations'

class Signup
  include Hanami::Validations

  predicate :foo? do |actual|
    actual == 'foo'
  end

  validations do
    required(:name).filled(:foo?)
  end
end

result = Signup.new(name: nil).call
result.messages # => { :name => ['is invalid'] }

With Custom Message

require 'hanami/validations'

class Signup
  include Hanami::Validations

  predicate :foo?, message: 'must be foo' do |actual|
    actual == 'foo'
  end

  validations do
    required(:name).filled(:foo?)
  end
end

result = Signup.new(name: nil).call
result.messages # => { :name => ['must be foo'] }

Parameters:

  • name (Symbol)

    inline predicate name

  • message (String) (defaults to: "is invalid")

    optional error message

  • blk (Proc)

    predicate implementation

Returns:

  • nil

Since:

  • 0.6.0



158
159
160
# File 'lib/hanami/v1/validations.rb', line 158

def predicate(name, message: "is invalid", &blk)
  _predicates << InlinePredicate.new(name, message, &blk)
end

#predicates(mod) ⇒ Object

Assign a set of shared predicates wrapped in a module

Examples:

require 'hanami/validations'

module MySharedPredicates
  include Hanami::Validations::Predicates

  predicate :foo? fo |actual|
    actual == 'foo'
  end
end

class MyValidator
  include Hanami::Validations
  predicates MySharedPredicates

  validations do
    required(:name).filled(:foo?)
  end
end

Parameters:

  • mod (Module)

    a module with shared predicates

See Also:

Since:

  • 0.6.0



189
190
191
# File 'lib/hanami/v1/validations.rb', line 189

def predicates(mod)
  self._predicates_module = mod
end

#validations(&blk) ⇒ Object

Define validation rules from the given block.

Examples:

Basic Example

require 'hanami/validations'

class Signup
  include Hanami::Validations

  validations do
    required(:name).filled
  end
end

result = Signup.new(name: "Luca").validate

result.success? # => true
result.messages # => []
result.output   # => {:name=>""}

result = Signup.new(name: "").validate

result.success? # => false
result.messages # => {:name=>["must be filled"]}
result.output   # => {:name=>""}

Parameters:

  • blk (Proc)

    validation rules

See Also:

Since:

  • 0.6.0



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/hanami/v1/validations.rb', line 101

def validations(&blk)
  schema_predicates = __predicates

  base   = _build(predicates: schema_predicates, &_base_rules)
  schema = _build(predicates: schema_predicates, rules: base.rules, &blk)
  schema.configure(&_schema_config)
  schema.configure(&_schema_predicates)
  schema.extend(__messages) unless _predicates.empty?

  self.schema = schema.new
end