Module: Hanami::Validations

Defined in:
lib/hanami/validations.rb,
lib/hanami/validations/form.rb,
lib/hanami/validations/version.rb,
lib/hanami/validations/namespace.rb,
lib/hanami/validations/predicates.rb,
lib/hanami/validations/inline_predicate.rb

Overview

Hanami::Validations is a set of lightweight validations for Ruby objects.

Examples:

require 'hanami/validations'

class Signup
  include Hanami::Validations

  validations do
    # ...
  end
end

Since:

  • 0.1.0

Defined Under Namespace

Modules: ClassMethods, Form, Predicates Classes: InlinePredicate, Namespace

Constant Summary collapse

DEFAULT_MESSAGES_ENGINE =

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

Since:

  • 0.6.0

:yaml
VERSION =

Since:

  • 0.1.0

'1.3.6'.freeze

Class Method Summary collapse

Instance 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.1.0



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/hanami/validations.rb', line 48

def self.included(base) # rubocop:disable Metrics/MethodLength
  base.class_eval do
    extend ClassMethods

    include Utils::ClassAttribute
    class_attribute :schema
    class_attribute :_messages
    class_attribute :_messages_path
    class_attribute :_namespace
    class_attribute :_predicates_module

    class_attribute :_predicates
    self._predicates = Set.new
  end
end

Instance Method Details

#initialize(input = {}) ⇒ Object

Initialize a new instance of a validator

Parameters:

  • input (#to_h) (defaults to: {})

    a set of input data

Since:

  • 0.6.0



361
362
363
# File 'lib/hanami/validations.rb', line 361

def initialize(input = {})
  @input = input.to_h
end

#to_hHash

Returns a Hash with the defined attributes as symbolized keys, and their relative values.

Returns:

  • (Hash)

Since:

  • 0.1.0



380
381
382
# File 'lib/hanami/validations.rb', line 380

def to_h
  validate.output
end

#validateDry::Validations::Result

Validates the object.

Returns:

  • (Dry::Validations::Result)

Since:

  • 0.2.4



370
371
372
# File 'lib/hanami/validations.rb', line 370

def validate
  self.class.schema.call(@input)
end