Class: Coradoc::Validation::Error

Inherits:
Object
  • Object
show all
Defined in:
lib/coradoc/validation.rb

Overview

A single validation error

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, path: nil, code: nil, element: nil) ⇒ Error

Create a validation error

Parameters:

  • message (String)

    Error message

  • path (String, nil) (defaults to: nil)

    Path to the error location

  • code (Symbol, nil) (defaults to: nil)

    Error code for programmatic handling

  • element (Object, nil) (defaults to: nil)

    The element that failed validation



81
82
83
84
85
86
# File 'lib/coradoc/validation.rb', line 81

def initialize(message, path: nil, code: nil, element: nil)
  @message = message
  @path = path
  @code = code
  @element = element
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



73
74
75
# File 'lib/coradoc/validation.rb', line 73

def code
  @code
end

#elementObject (readonly)

Returns the value of attribute element.



73
74
75
# File 'lib/coradoc/validation.rb', line 73

def element
  @element
end

#messageObject (readonly)

Returns the value of attribute message.



73
74
75
# File 'lib/coradoc/validation.rb', line 73

def message
  @message
end

#pathObject (readonly)

Returns the value of attribute path.



73
74
75
# File 'lib/coradoc/validation.rb', line 73

def path
  @path
end

Instance Method Details

#to_hHash

Convert to hash

Returns:

  • (Hash)


102
103
104
# File 'lib/coradoc/validation.rb', line 102

def to_h
  { message: message, path: path, code: code }
end

#to_sString

Format error as string

Returns:

  • (String)


91
92
93
94
95
96
97
# File 'lib/coradoc/validation.rb', line 91

def to_s
  if path
    "#{path}: #{message}"
  else
    message
  end
end