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



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

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

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



109
110
111
# File 'lib/coradoc/validation.rb', line 109

def errors
  @errors
end

#warningsObject (readonly)

Returns the value of attribute warnings.



109
110
111
# File 'lib/coradoc/validation.rb', line 109

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



155
156
157
158
159
# File 'lib/coradoc/validation.rb', line 155

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



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

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)


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

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:



198
199
200
# File 'lib/coradoc/validation.rb', line 198

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



178
179
180
181
# File 'lib/coradoc/validation.rb', line 178

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

#to_hHash

Convert to hash

Returns:

  • (Hash)


205
206
207
208
209
210
211
212
213
# File 'lib/coradoc/validation.rb', line 205

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)


186
187
188
189
190
191
192
# File 'lib/coradoc/validation.rb', line 186

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)


123
124
125
# File 'lib/coradoc/validation.rb', line 123

def valid?
  @errors.empty?
end

#warning_countInteger

Get warning count

Returns:

  • (Integer)


144
145
146
# File 'lib/coradoc/validation.rb', line 144

def warning_count
  @warnings.size
end

#warnings?Boolean

Check if there are any warnings

Returns:

  • (Boolean)


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

def warnings?
  @warnings.any?
end