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



41
42
43
44
45
46
# File 'lib/coradoc/validation.rb', line 41

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.



33
34
35
# File 'lib/coradoc/validation.rb', line 33

def code
  @code
end

#elementObject (readonly)

Returns the value of attribute element.



33
34
35
# File 'lib/coradoc/validation.rb', line 33

def element
  @element
end

#messageObject (readonly)

Returns the value of attribute message.



33
34
35
# File 'lib/coradoc/validation.rb', line 33

def message
  @message
end

#pathObject (readonly)

Returns the value of attribute path.



33
34
35
# File 'lib/coradoc/validation.rb', line 33

def path
  @path
end

Instance Method Details

#to_hHash

Convert to hash

Returns:

  • (Hash)


62
63
64
# File 'lib/coradoc/validation.rb', line 62

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

#to_sString

Format error as string

Returns:

  • (String)


51
52
53
54
55
56
57
# File 'lib/coradoc/validation.rb', line 51

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