class

Ralph::Validations::Errors

Inherits Reference / Object

Stores validation errors for a model

Errors provides both backward-compatible string access and structured error access for i18n and detailed error handling.

Basic Usage

errors = Errors.new
errors.add("name", "can't be blank")
errors.add("email", :taken)
errors.add("password", :too_short, count: 8)

errors.full_messages # => ["name can't be blank", "email has already been taken", "password is too short (minimum is 8 characters)"]
errors.details       # => {"name" => [{error: :blank}], "email" => [{error: :taken}], ...}

Constructors

Instance methods

[](attribute : String) : Array(String)

Get all error messages for an attribute (backward compatible - returns strings)

Source
add(attribute : String, code : Symbol, message : String, **options)

Add an error for a specific attribute (with explicit message and code)

Source
add(attribute : String, message : String)

Add an error for a specific attribute (string message - backward compatible)

Source
add(attribute : String, code : Symbol, **options)

Add an error for a specific attribute (structured with code)

Source
any?

Check if there are any errors (inverse of empty?)

Source
clear

Clear all errors

Source
codes_for(attribute : String) : Array(Symbol)

Get all error codes for an attribute

Source
count

Get count of errors

Source
details

Get error details for i18n/structured access

Returns a hash where each attribute maps to an array of error details. Each detail contains the error code and any options.

Example

errors.add("name", :blank)
errors.add("password", :too_short, count: 8)

errors.details
# => {
#   "name" => [{error: :blank}],
#   "password" => [{error: :too_short, count: 8}]
# }
Source
each

Iterate over errors (yields full messages)

Source
each_error

Iterate over structured errors

Source
each_with_attribute

Iterate over errors with attribute and message

Source
empty?

Check if there are any errors

Source
errors_for(attribute : String) : Array(ValidationError)

Get all validation errors for an attribute (structured)

Source
full_messages

Get all error messages as a flat array (backward compatible)

Source
has_error?(attribute : String, code : Symbol) : Bool

Check if a specific error code exists for an attribute

Source
has_key?(attribute : String) : Bool

Alias for include?

Source
include?(attribute : String) : Bool

Check if there are errors for a specific attribute

Source
merge!(other : Errors)

Merge errors from another Errors object

Source
messages

Get all error messages as a hash (alias for to_h)

Source
size

Alias for count

Source
to_h

Get all errors as a hash of attribute => messages (backward compatible)

Source