Module: ServiceObjects::Helpers::Validations

Includes:
ActiveModel::Validations, Messages
Included in:
Base
Defined in:
lib/service_objects/helpers/validations.rb

Overview

Note:

A target class should *include* the module

Features for service attributes validation

Instance Method Summary collapse

Methods included from Messages

#add_message, #messages, #translate

Instance Method Details

#valid?Boolean

Runs validations and checks if the object is valid

Returns:

  • (Boolean)


# File 'lib/service_objects/helpers/validations.rb', line 17

#validate!self

Raises ServiceObjects::Invalid when #valid? method fails

Mutates the current object by populating its messages with errors, added by #valid?

Returns:

  • (self)

    (not changed) when a validation passes

Raises:



31
32
33
34
35
# File 'lib/service_objects/helpers/validations.rb', line 31

def validate!
  return self if valid?
  __errors__.each { |text| add_message text: text, type: "error" }
  fail Invalid.new(self)
end