Class: EasyTalk::ErrorFormatter::PathConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_talk/error_formatter/path_converter.rb

Overview

Converts attribute paths between different formats.

EasyTalk uses dot notation for nested model errors (e.g., "email.address"). This class converts those paths to different standard formats:

  • JSON Pointer (RFC 6901): /properties/email/properties/address
  • JSON:API: /data/attributes/email/address
  • Flat: email.address (unchanged)

Examples:

PathConverter.to_json_pointer("email.address")
# => "/properties/email/properties/address"

PathConverter.to_jsonapi_pointer("email.address")
# => "/data/attributes/email/address"

Class Method Summary collapse

Class Method Details

.to_flat(attribute) ⇒ String

Return the attribute path as-is (flat format).

Parameters:

  • attribute (Symbol, String)

    The attribute path

Returns:

  • (String)

    The attribute as a string



47
48
49
# File 'lib/easy_talk/error_formatter/path_converter.rb', line 47

def to_flat(attribute)
  attribute.to_s
end

.to_json_pointer(attribute) ⇒ String

Convert an attribute path to JSON Schema pointer format.

Parameters:

  • attribute (Symbol, String)

    The attribute path (e.g., "email.address")

Returns:

  • (String)

    The JSON Pointer path (e.g., "/properties/email/properties/address")



26
27
28
29
30
31
# File 'lib/easy_talk/error_formatter/path_converter.rb', line 26

def to_json_pointer(attribute)
  parts = attribute.to_s.split('.')
  return "/properties/#{parts.first}" if parts.length == 1

  parts.map { |part| "/properties/#{part}" }.join
end

.to_jsonapi_pointer(attribute, prefix: '/data/attributes') ⇒ String

Convert an attribute path to JSON:API pointer format.

Parameters:

  • attribute (Symbol, String)

    The attribute path

  • prefix (String) (defaults to: '/data/attributes')

    The path prefix (default: "/data/attributes")

Returns:

  • (String)

    The JSON:API pointer path



38
39
40
41
# File 'lib/easy_talk/error_formatter/path_converter.rb', line 38

def to_jsonapi_pointer(attribute, prefix: '/data/attributes')
  parts = attribute.to_s.split('.')
  "#{prefix}/#{parts.join('/')}"
end