Class: Coradoc::Validation::Result
- Inherits:
-
Object
- Object
- Coradoc::Validation::Result
- Defined in:
- lib/coradoc/validation.rb
Overview
Validation result containing errors
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#warnings ⇒ Object
readonly
Returns the value of attribute warnings.
Instance Method Summary collapse
-
#add_error(message, path: nil, code: nil, element: nil) ⇒ Error
Add an error.
-
#add_warning(message, path: nil, code: nil, element: nil) ⇒ Error
Add a warning.
-
#error_count ⇒ Integer
Get error count.
-
#errors_at(path) ⇒ Array<Error>
Get errors for a specific path.
-
#initialize(errors: [], warnings: []) ⇒ Result
constructor
Create a validation result.
-
#merge!(other) ⇒ void
Merge another result into this one.
-
#to_h ⇒ Hash
Convert to hash.
-
#to_s ⇒ String
Format errors as a human-readable string.
-
#valid? ⇒ Boolean
Check if validation passed.
-
#warning_count ⇒ Integer
Get warning count.
-
#warnings? ⇒ Boolean
Check if there are any warnings.
Constructor Details
#initialize(errors: [], warnings: []) ⇒ Result
Create a validation result
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
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
69 70 71 |
# File 'lib/coradoc/validation.rb', line 69 def errors @errors end |
#warnings ⇒ Object (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
115 116 117 118 119 |
# File 'lib/coradoc/validation.rb', line 115 def add_error(, path: nil, code: nil, element: nil) error = Error.new(, path: path, code: code, element: element) @errors << error error end |
#add_warning(message, path: nil, code: nil, element: nil) ⇒ Error
Add a warning
128 129 130 131 132 |
# File 'lib/coradoc/validation.rb', line 128 def add_warning(, path: nil, code: nil, element: nil) warning = Error.new(, path: path, code: code, element: element) @warnings << warning warning end |
#error_count ⇒ Integer
Get error count
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
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
138 139 140 141 |
# File 'lib/coradoc/validation.rb', line 138 def merge!(other) @errors.concat(other.errors) @warnings.concat(other.warnings) end |
#to_h ⇒ Hash
Convert to 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_s ⇒ String
Format errors as a human-readable 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.}" } lines.join("\n") end |
#valid? ⇒ Boolean
Check if validation passed
83 84 85 |
# File 'lib/coradoc/validation.rb', line 83 def valid? @errors.empty? end |
#warning_count ⇒ Integer
Get warning count
104 105 106 |
# File 'lib/coradoc/validation.rb', line 104 def warning_count @warnings.size end |
#warnings? ⇒ Boolean
Check if there are any warnings
90 91 92 |
# File 'lib/coradoc/validation.rb', line 90 def warnings? @warnings.any? end |