Class: Treaty::Entity::Attribute::Option::Modifiers::TransformModifier

Inherits:
Base
  • Object
show all
Defined in:
lib/treaty/entity/attribute/option/modifiers/transform_modifier.rb

Overview

Transforms attribute values using custom lambda functions.

Usage Examples

Simple mode:

integer :amount, transform: ->(value:) { value * 100 }
string :title, transform: ->(value:) { value.strip.upcase }

Advanced mode with custom error message:

integer :amount, transform: {
is: ->(value:) { value * 100 },
message: "Failed to transform amount"
}

Use Cases

  1. Request transformation:

    request do
      integer :amount_cents, transform: ->(value:) { value * 100 }
    end
    # Input: { amount_cents: 10 }
    # Service receives: { amount_cents: 1000 }
    
  2. Response transformation:

    response 200 do
      string :title, transform: ->(value:) { value.titleize }
    end
    # Service returns: { title: "hello world" }
    # Output: { title: "Hello World" }
    
  3. Complex transformations:

    string :email, transform: ->(value:) { value.downcase.strip }
    datetime :timestamp, transform: ->(value:) { value.iso8601 }
    

Important Notes

  • Lambda must accept named argument value:
  • All exceptions raised in lambda are caught and re-raised as Validation errors
  • Transformation is applied during Phase 3 (after validation)
  • Can be combined with other options (required, default, as, etc.)

Error Handling

If the lambda raises any exception, it's caught and converted to a Treaty::Exceptions::Validation with appropriate error message.

Advanced Mode

Schema format: { is: lambda, message: nil }

Instance Method Summary collapse

Methods inherited from Base

#initialize, #target_name, #transforms_name?, #validate_value!

Constructor Details

This class inherits a constructor from Treaty::Entity::Attribute::Option::Base

Instance Method Details

#transform_value(value, _root_data = {}) ⇒ Object

Applies transformation to the value using the provided lambda Catches all exceptions and re-raises as Validation errors Skips transformation for nil values (handled by RequiredValidator)

Parameters:

  • value (Object)

    The current value

  • _root_data (Hash) (defaults to: {})

    Unused root data parameter

Returns:

  • (Object)

    Transformed value



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/treaty/entity/attribute/option/modifiers/transform_modifier.rb', line 88

def transform_value(value, _root_data = {}) # rubocop:disable Metrics/MethodLength
  return value if value.nil? # Transform doesn't modify nil, required validator handles it.

  transform_lambda = option_value

  # Call lambda with named argument
  transform_lambda.call(value:)
rescue StandardError => e
  attributes = {
    attribute: @attribute_name,
    error: e.message
  }

  # Catch all exceptions from lambda execution
  error_message = resolve_custom_message(**attributes) || I18n.t(
    "treaty.attributes.modifiers.transform.execution_error",
    **attributes
  )

  raise Treaty::Exceptions::Validation, error_message
end

#validate_schema!void

This method returns an undefined value.

Validates that transform value is a lambda

Raises:



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/treaty/entity/attribute/option/modifiers/transform_modifier.rb', line 68

def validate_schema!
  transform_lambda = option_value

  return if transform_lambda.respond_to?(:call)

  raise Treaty::Exceptions::Validation,
        I18n.t(
          "treaty.attributes.modifiers.transform.invalid_type",
          attribute: @attribute_name,
          type: transform_lambda.class
        )
end