Class: Apiwork::Issue

Inherits:
Object
  • Object
show all
Defined in:
lib/apiwork/issue.rb

Overview

Represents a validation issue found during request parsing.

Issues are returned when request parameters fail validation, coercion, or constraint checks. Access via contract.issues.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, detail, meta: {}, path: []) ⇒ Issue

Returns a new instance of Issue.



35
36
37
38
39
40
# File 'lib/apiwork/issue.rb', line 35

def initialize(code, detail, meta: {}, path: [])
  @code = code
  @detail = detail
  @path = path.map { |element| element.is_a?(Integer) ? element : element.to_sym }
  @meta = meta
end

Instance Attribute Details

#codeSymbol (readonly)

The code for this issue.

Returns:

  • (Symbol)


30
31
32
# File 'lib/apiwork/issue.rb', line 30

def code
  @code
end

#detailString (readonly)

The detail for this issue.

Returns:

  • (String)


30
31
32
33
# File 'lib/apiwork/issue.rb', line 30

attr_reader :code,
:detail,
:meta,
:path

#metaHash (readonly)

The meta for this issue.

Returns:

  • (Hash)


30
31
32
33
# File 'lib/apiwork/issue.rb', line 30

attr_reader :code,
:detail,
:meta,
:path

#pathObject (readonly)



30
31
32
33
# File 'lib/apiwork/issue.rb', line 30

attr_reader :code,
:detail,
:meta,
:path

Instance Method Details

#as_jsonHash

Converts this issue to a hash for JSON serialization.

Returns:

  • (Hash)


68
69
70
# File 'lib/apiwork/issue.rb', line 68

def as_json
  to_h
end

#pointerString

The pointer for this issue.

Returns:

  • (String)


46
47
48
# File 'lib/apiwork/issue.rb', line 46

def pointer
  @pointer ||= JSONPointer.new(*path).to_s
end

#to_hHash

Converts this issue to a hash.

Returns:

  • (Hash)


54
55
56
57
58
59
60
61
62
# File 'lib/apiwork/issue.rb', line 54

def to_h
  {
    code: code,
    detail: detail,
    meta: meta,
    path: path.map { |segment| segment.is_a?(Integer) ? segment : segment.to_s },
    pointer: pointer,
  }
end

#to_sString

Converts this issue to a string.

Returns:

  • (String)


76
77
78
# File 'lib/apiwork/issue.rb', line 76

def to_s
  "[#{code}]#{path.any? ? " at #{pointer}" : ''} #{detail}"
end