Module: Hanami::Validations::ClassMethods

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

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

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

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)

    optional error message

  • blk (Proc)

    predicate implementation

Returns:

  • nil

Since:

  • 0.6.0

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

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

def validations(&blk) # rubocop:disable Metrics/AbcSize
  schema_predicates = _predicates_module || __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