Class: Coradoc::Validation::Result

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

Overview

Validation result containing errors

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(errors: [], warnings: []) ⇒ Result

Create a validation result

Parameters:

  • errors (Array<Error>) (defaults to: [])

    Validation errors

  • warnings (Array<Error>) (defaults to: [])

    Validation warnings



75
76
77
78
# File 'lib/coradoc/validation.rb', line 75

def initialize(errors: [], warnings: [])
  @errors = Array(errors)
  @warnings = Array(warnings)
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



69
70
71
# File 'lib/coradoc/validation.rb', line 69

def errors
  @errors
end

#warningsObject (readonly)

Returns the value of attribute warnings.



69
70
71
# File 'lib/coradoc/validation.rb', line 69

def warnings
  @warnings
end

Instance Method Details

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

Add an error

Parameters:

  • message (String)

    Error message

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

    Error path

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

    Error code

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

    Failed element

Returns:

  • (Error)

    The added error



115
116
117
118
119
# File 'lib/coradoc/validation.rb', line 115

def add_error(message, path: nil, code: nil, element: nil)
  error = Error.new(message, path: path, code: code, element: element)
  @errors << error
  error
end

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

Add a warning

Parameters:

  • message (String)

    Warning message

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

    Warning path

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

    Warning code

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

    Related element

Returns:

  • (Error)

    The added warning



128
129
130
131
132
# File 'lib/coradoc/validation.rb', line 128

def add_warning(message, path: nil, code: nil, element: nil)
  warning = Error.new(message, path: path, code: code, element: element)
  @warnings << warning
  warning
end

#error_countInteger

Get error count

Returns:

  • (Integer)


97
98
99
# File 'lib/coradoc/validation.rb', line 97

def error_count
  @errors.size
end

#errors_at(path) ⇒ Array<Error>

Get errors for a specific path

Parameters:

  • path (String)

    The path to filter by

Returns:



158
159
160
# File 'lib/coradoc/validation.rb', line 158

def errors_at(path)
  @errors.select { |e| e.path == path }
end

#merge!(other) ⇒ void

This method returns an undefined value.

Merge another result into this one

Parameters:

  • other (Result)

    Another validation result



138
139
140
141
# File 'lib/coradoc/validation.rb', line 138

def merge!(other)
  @errors.concat(other.errors)
  @warnings.concat(other.warnings)
end

#to_hHash

Convert to hash

Returns:

  • (Hash)


165
166
167
168
169
170
171
172
173
# File 'lib/coradoc/validation.rb', line 165

def to_h
  {
    valid: valid?,
    error_count: error_count,
    warning_count: warning_count,
    errors: @errors.map(&:to_h),
    warnings: @warnings.map(&:to_h)
  }
end

#to_sString

Format errors as a human-readable string

Returns:

  • (String)


146
147
148
149
150
151
152
# File 'lib/coradoc/validation.rb', line 146

def to_s
  return 'Valid' if valid?

  lines = ["#{error_count} validation error(s):"]
  @errors.each { |err| lines << "  - #{err.path}: #{err.message}" }
  lines.join("\n")
end

#valid?Boolean

Check if validation passed

Returns:

  • (Boolean)


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

def valid?
  @errors.empty?
end

#warning_countInteger

Get warning count

Returns:

  • (Integer)


104
105
106
# File 'lib/coradoc/validation.rb', line 104

def warning_count
  @warnings.size
end

#warnings?Boolean

Check if there are any warnings

Returns:

  • (Boolean)


90
91
92
# File 'lib/coradoc/validation.rb', line 90

def warnings?
  @warnings.any?
end