class ActiveModel::Errors

Experimental RBS support (using type sampling data from the type_fusion project).

# sig/active_model/errors.rbs

class ActiveModel::Errors
  def initialize: (Types::Sample base) -> void
end

# etc..
person.errors.full_messages # => [“name cannot be nil”]
person.validate! # => [“cannot be nil”]
person = Person.new
The above allows you to do:
for you.
ActiveModel::Validations will handle the validation related methods
you will not need to implement the last two. Likewise, using
languages. Of course, if you extend your object with ActiveModel::Translation
able to generate error messages correctly and also handle multiple
The last three methods are required in your object for Errors to be

end
end
[self]
def self.lookup_ancestors
end
attr
def self.human_attribute_name(attr, options = {})
end
send(attr)
def read_attribute_for_validation(attr)
# The following methods are needed to be minimally implemented
end
errors.add(:name, :blank, message: “cannot be nil”) if name.nil?
def validate!
attr_reader :errors
attr_accessor :name
end
@errors = ActiveModel::Errors.new(self)
def initialize
extend ActiveModel::Naming
# Required dependency for ActiveModel::Errors
class Person
A minimal implementation could be:
for handling error messages and interacting with Action View helpers.
Provides error related functionalities you can include in your object
== Active Model Errors

def [](attribute)

person.errors['name'] # => ["cannot be nil"]
person.errors[:name] # => ["cannot be nil"]

for the method.
When passed a symbol or a name of a method, returns an array of errors
def [](attribute)
  messages_for(attribute)
end

def add(attribute, type = :invalid, **options)

# => {:base=>[{error: :name_or_email_blank}]}
person.errors.details
# => {:base=>["either name or email must be present"]}
person.errors.messages
message: "either name or email must be present")
person.errors.add(:base, :name_or_email_blank,

directly associated with a single attribute.
+attribute+ should be set to :base if the error is not

person.errors.messages # => {}

# => NameIsInvalid: Name is invalid
person.errors.add(:name, :invalid, strict: NameIsInvalid)
# => ActiveModel::StrictValidationFailed: Name is invalid
person.errors.add(:name, :invalid, strict: true)

:strict option can also be set to any other exception.
ActiveModel::StrictValidationFailed instead of adding the error.
If the :strict option is set to +true+, it will raise

Time.now to be used within an error.
If +type+ is a proc, it will be called, allowing for things like

# => ["is too long (maximum is 25 characters)"]
person.errors.messages
person.errors.add(:name, :too_long, { count: 25 })

# => {:name=>["can't be blank"]}
person.errors.messages
person.errors.add(:name, :blank)

scope (see +generate_message+).
If +type+ is a symbol, it will be translated using the appropriate

If +type+ is a string, it will be used as error message.

# => {:name=>["is invalid", "must be implemented"]}
person.errors.messages

options={:message=>"must be implemented"}>
# Adds <#ActiveModel::Error attribute=name, type=not_implemented,
person.errors.add(:name, :not_implemented, message: "must be implemented")
# Adds <#ActiveModel::Error attribute=name, type=invalid>
person.errors.add(:name)

If no +type+ is supplied, :invalid is assumed.
More than one error can be added to the same +attribute+.
Adds a new error of +type+ on +attribute+.
def add(attribute, type = :invalid, **options)
  attribute, type, options = normalize_arguments(attribute, type, **options)
  error = Error.new(@base, attribute, type, **options)
  if exception = options[:strict]
    exception = ActiveModel::StrictValidationFailed if exception == true
    raise exception, error.full_message
  end
  @errors.append(error)
  error
end

def added?(attribute, type = :invalid, options = {})

person.errors.added? :name, "is too long" # => false
person.errors.added? :name, :too_long # => false
person.errors.added? :name, :too_long, count: 24 # => false
person.errors.added? :name, "is too long (maximum is 25 characters)" # => true
person.errors.added? :name, :too_long, count: 25 # => true
person.errors.add :name, :too_long, { count: 25 }

the correct options, or +false+ with incorrect or missing options.
If the error requires options, then it returns +true+ with

person.errors.added? :name, "can't be blank" # => true
person.errors.added? :name, :blank # => true
person.errors.add :name, :blank

or +false+ otherwise. +type+ is treated the same as for +add+.
Returns +true+ if an error matches provided +attribute+ and +type+,
def added?(attribute, type = :invalid, options = {})
  attribute, type, options = normalize_arguments(attribute, type, **options)
  if type.is_a? Symbol
    @errors.any? { |error|
      error.strict_match?(attribute, type, **options)
    }
  else
    messages_for(attribute).include?(type)
  end
end

def as_json(options = nil)

person.errors.as_json(full_messages: true) # => {:name=>["name cannot be nil"]}
person.errors.as_json # => {:name=>["cannot be nil"]}

if the json object should contain full messages or not (false by default).
object. You can pass the :full_messages option. This determines
Returns a Hash that can be used as the JSON representation for this
def as_json(options = nil)
  to_hash(options && options[:full_messages])
end

def attribute_names

person.errors.attribute_names # => [:name]
person.errors.messages # => {:name=>["cannot be nil", "must be specified"]}

Returns all error attribute names
def attribute_names
  @errors.map(&:attribute).uniq.freeze
end

def copy!(other) # :nodoc:

:nodoc:

person.errors.copy!(other)

==== Examples

* +other+ - The ActiveModel::Errors instance.

==== Parameters

For copying errors but keep @base as is.
Copies the errors from other.
def copy!(other) # :nodoc:
  @errors = other.errors.deep_dup
  @errors.each { |error|
    error.instance_variable_set(:@base, @base)
  }
end

def delete(attribute, type = nil, **options)

person.errors[:name] # => []
person.errors.delete(:name) # => ["cannot be nil"]
person.errors[:name] # => ["cannot be nil"]

Delete messages for +key+. Returns the deleted messages.
def delete(attribute, type = nil, **options)
  attribute, type, options = normalize_arguments(attribute, type, **options)
  matches = where(attribute, type, **options)
  matches.each do |error|
    @errors.delete(error)
  end
  matches.map(&:message).presence
end

def details

Returns a Hash of attributes with an array of their error details.
def details
  hash = group_by_attribute.transform_values do |errors|
    errors.map(&:details)
  end
  hash.default = EMPTY_ARRAY
  hash.freeze
  hash
end

def full_message(attribute, message)

person.errors.full_message(:name, 'is invalid') # => "Name is invalid"

Returns a full message for a given attribute.
def full_message(attribute, message)
  Error.full_message(attribute, message, @base)
end

def full_messages

# => ["Name is too short (minimum is 5 characters)", "Name can't be blank", "Email can't be blank"]
person.errors.full_messages
person = Person.create(address: '123 First St.')

end
validates_length_of :name, in: 5..30
validates_presence_of :name, :address, :email
class Person

Returns all the full error messages in an array.
def full_messages
  @errors.map(&:full_message)
end

def full_messages_for(attribute)

# => ["Name is too short (minimum is 5 characters)", "Name can't be blank"]
person.errors.full_messages_for(:name)
person = Person.create()

end
validates_length_of :name, in: 5..30
validates_presence_of :name, :email
class Person

Returns all the full error messages for a given attribute in an array.
def full_messages_for(attribute)
  where(attribute).map(&:full_message).freeze
end

def generate_message(attribute, type = :invalid, options = {})

* errors.messages.blank
* errors.attributes.title.blank
* activemodel.errors.messages.blank
* any default you provided through the +options+ hash (in the activemodel.errors scope)
* activemodel.errors.models.user.blank
* activemodel.errors.models.user.attributes.title.blank
* activemodel.errors.models.admin.blank
* activemodel.errors.models.admin.attributes.title.blank

it looks for these translations:
the :blank error message for the title attribute,
class Admin < User; end and you wanted the translation for
models too, but only if the model itself hasn't been found. Say you have
When using inheritance in your models, it will check all the inherited

interpolation.
name, translated attribute name, and the value are available for
(e.g. activemodel.errors.messages.MESSAGE). The translated model
that is not there also, it returns the translation of the default message
if it's not there, it's looked up in activemodel.errors.models.MODEL.MESSAGE and if
Error messages are first looked up in activemodel.errors.models.MODEL.attributes.ATTRIBUTE.MESSAGE,

(activemodel.errors.messages).
Translates an error message in its default scope
def generate_message(attribute, type = :invalid, options = {})
  Error.generate_message(attribute, type, @base, options)
end

def group_by_attribute

# => {:name=>[<#ActiveModel::Error>, <#ActiveModel::Error>]}
person.errors.group_by_attribute

Returns a Hash of attributes with an array of their Error objects.
def group_by_attribute
  @errors.group_by(&:attribute)
end

def import(error, override_options = {})

* +:type+ - Override type of the error.
* +:attribute+ - Override the attribute the error belongs to.

==== Options

If attribute or type needs to be overridden, use +override_options+.
providing access to original error object.
Imported errors are wrapped as a NestedError,
Imports one error.
def import(error, override_options = {})
  [:attribute, :type].each do |key|
    if override_options.key?(key)
      override_options[key] = override_options[key].to_sym
    end
  end
  @errors.append(NestedError.new(@base, error, override_options))
end

def include?(attribute)

person.errors.include?(:age) # => false
person.errors.include?(:name) # => true
person.errors.messages # => {:name=>["cannot be nil"]}

+attribute+, +false+ otherwise.
Returns +true+ if the error messages include an error for the given key
def include?(attribute)
  @errors.any? { |error|
    error.match?(attribute.to_sym)
  }
end

def initialize(base)

Experimental RBS support (using type sampling data from the type_fusion project).

def initialize: (Types::Sample base) -> void

This signature was generated using 6 samples from 1 application.

end
end
@errors = ActiveModel::Errors.new(self)
def initialize
class Person

Pass in the instance of the object that is using the errors object.
def initialize(base)
  @base = base
  @errors = []
end

def initialize_dup(other) # :nodoc:

:nodoc:
def initialize_dup(other) # :nodoc:
  @errors = other.errors.deep_dup
  super
end

def inspect # :nodoc:

:nodoc:
def inspect # :nodoc:
  inspection = @errors.inspect
  "#<#{self.class.name} #{inspection}>"
end

def merge!(other)


person.errors.merge!(other)

==== Examples

* +other+ - The ActiveModel::Errors instance.

==== Parameters

each Error wrapped as NestedError.
Merges the errors from other,
def merge!(other)
  return errors if equal?(other)
  other.errors.each { |error|
    import(error)
  }
end

def messages

Returns a Hash of attributes with an array of their error messages.
def messages
  hash = to_hash
  hash.default = EMPTY_ARRAY
  hash.freeze
  hash
end

def messages_for(attribute)

# => ["is too short (minimum is 5 characters)", "can't be blank"]
person.errors.messages_for(:name)
person = Person.create()

end
validates_length_of :name, in: 5..30
validates_presence_of :name, :email
class Person

Returns all the error messages for a given attribute in an array.
def messages_for(attribute)
  where(attribute).map(&:message)
end

def normalize_arguments(attribute, type, **options)

def normalize_arguments(attribute, type, **options)
  # Evaluate proc first
  if type.respond_to?(:call)
    type = type.call(@base, options)
  end
  [attribute.to_sym, type, options]
end

def of_kind?(attribute, type = :invalid)

person.errors.of_kind? :name, "is too long" # => false
person.errors.of_kind? :name, :not_too_long # => false
person.errors.of_kind? :name, "is too long (maximum is 25 characters)" # => true
person.errors.of_kind? :name, :too_long # => true
person.errors.of_kind? :name # => false
person.errors.of_kind? :age # => true
person.errors.add :name, :too_long, { count: 25 }
person.errors.add :age

present, or +false+ otherwise. +type+ is treated the same as for +add+.
Returns +true+ if an error on the attribute with the given type is
def of_kind?(attribute, type = :invalid)
  attribute, type = normalize_arguments(attribute, type)
  if type.is_a? Symbol
    !where(attribute, type).empty?
  else
    messages_for(attribute).include?(type)
  end
end

def to_hash(full_messages = false)

person.errors.to_hash(true) # => {:name=>["name cannot be nil"]}
person.errors.to_hash # => {:name=>["cannot be nil"]}

is +true+, it will contain full messages (see +full_message+).
Returns a Hash of attributes with their error messages. If +full_messages+
def to_hash(full_messages = false)
  message_method = full_messages ? :full_message : :message
  group_by_attribute.transform_values do |errors|
    errors.map(&message_method)
  end
end

def where(attribute, type = nil, **options)

person.errors.where(:name, :too_short, minimum: 2) # => all name errors being too short and minimum is 2
person.errors.where(:name, :too_short) # => all name errors being too short
person.errors.where(:name) # => all name errors.

Only supplied params will be matched.

Search for errors matching +attribute+, +type+, or +options+.
def where(attribute, type = nil, **options)
  attribute, type, options = normalize_arguments(attribute, type, **options)
  @errors.select { |error|
    error.match?(attribute, type, **options)
  }
end