Exception: Treaty::Exceptions::NestedAttributes

Inherits:
Base
  • Object
show all
Defined in:
lib/treaty/exceptions/nested_attributes.rb

Overview

Raised when attribute nesting depth exceeds configured maximum

Purpose

Prevents excessive nesting of attributes which can lead to:

  • Performance degradation
  • Complex validation logic
  • Difficult to maintain code
  • Stack overflow risks

Usage

Raised automatically when defining deeply nested attributes:

request do
  object :user do
    object :profile do
      object :settings do
        object :preferences do
          object :notifications do
            # If max nesting is 3, this raises NestedAttributes exception
            string :email_enabled
          end
        end
      end
    end
  end
end

Configuration

Maximum nesting level is configurable in Treaty engine configuration:

Treaty::Engine.config.treaty.attribute_nesting_level = 3  # Default

Integration

Can be rescued by application controllers:

rescue_from Treaty::Exceptions::NestedAttributes, with: :render_nesting_error

def render_nesting_error(exception)
  render json: { error: exception.message }, status: :unprocessable_entity
end

Best Practices

  • Keep nesting shallow (2-3 levels maximum)
  • Consider flattening deeply nested structures
  • Use separate objects instead of deep nesting
  • Refactor complex structures into simpler ones