Class: Dcc::Validate::Result
- Inherits:
-
Lutaml::Model::Serializable
- Object
- Lutaml::Model::Serializable
- Dcc::Validate::Result
- Defined in:
- lib/dcc/validate/result.rb
Overview
Result of running one or more validators. Tracks the list of issues
and the schema version (when relevant). Renderable as text, JSON, or
YAML via to_s / to_json / to_yaml.
Direct Known Subclasses
Instance Method Summary collapse
-
#errors ⇒ Array<Dcc::Validate::Issue>
Only the errors.
-
#infos ⇒ Array<Dcc::Validate::Issue>
Only informational issues.
-
#initialize(issues: [], schema_version: nil, source: nil) ⇒ Result
constructor
A new instance of Result.
-
#merge(other) ⇒ Dcc::Validate::Result
Non-mutating merge.
-
#merge!(other) ⇒ Dcc::Validate::Result
Merge another Result into this one (mutating).
-
#ok? ⇒ Boolean
True when there are no failing (error) issues.
- #to_json(*_args) ⇒ Object
- #to_s ⇒ Object
- #to_yaml(*_args) ⇒ Object
-
#warnings ⇒ Array<Dcc::Validate::Issue>
Only the warnings.
Constructor Details
#initialize(issues: [], schema_version: nil, source: nil) ⇒ Result
Returns a new instance of Result.
18 19 20 21 22 23 |
# File 'lib/dcc/validate/result.rb', line 18 def initialize(issues: [], schema_version: nil, source: nil) super() self.issues = issues self.schema_version = schema_version self.source = source end |
Instance Method Details
#errors ⇒ Array<Dcc::Validate::Issue>
Returns only the errors.
31 32 33 |
# File 'lib/dcc/validate/result.rb', line 31 def errors issues.select(&:failing?) end |
#infos ⇒ Array<Dcc::Validate::Issue>
Returns only informational issues.
41 42 43 |
# File 'lib/dcc/validate/result.rb', line 41 def infos issues.select { |i| i.severity == ::Dcc::Validate::Severity::INFO } end |
#merge(other) ⇒ Dcc::Validate::Result
Non-mutating merge.
55 56 57 |
# File 'lib/dcc/validate/result.rb', line 55 def merge(other) dup.merge!(other) end |
#merge!(other) ⇒ Dcc::Validate::Result
Merge another Result into this one (mutating). Returns self.
48 49 50 51 |
# File 'lib/dcc/validate/result.rb', line 48 def merge!(other) self.issues = issues + other.issues self end |
#ok? ⇒ Boolean
Returns true when there are no failing (error) issues.
26 27 28 |
# File 'lib/dcc/validate/result.rb', line 26 def ok? issues.none?(&:failing?) end |
#to_json(*_args) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/dcc/validate/result.rb', line 70 def to_json(*_args) require "json" payload = { ok: ok?, source: source, schema_version: schema_version, error_count: errors.size, warning_count: warnings.size, info_count: infos.size, issues: issues.map do |i| { severity: i.severity.to_s, code: i.code, message: i., line: i.line, column: i.column, path: i.path, source: i.source, } end, } ::JSON.pretty_generate(payload) end |
#to_s ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/dcc/validate/result.rb', line 59 def to_s if ok? "OK: no errors found#{schema_version ? " (schemaVersion #{schema_version})" : ''}" else header = "#{errors.size} error(s), #{warnings.size} warning(s)" header += " (schemaVersion #{schema_version})" if schema_version body = errors.map(&:to_s).unshift(header).join("\n") body end end |
#to_yaml(*_args) ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'lib/dcc/validate/result.rb', line 94 def to_yaml(*_args) require "yaml" { ok: ok?, source: source, schema_version: schema_version, issues: issues.map { |i| [i.severity.to_s, i., i.line, i.path] }, }.to_yaml end |
#warnings ⇒ Array<Dcc::Validate::Issue>
Returns only the warnings.
36 37 38 |
# File 'lib/dcc/validate/result.rb', line 36 def warnings issues.select { |i| i.severity == ::Dcc::Validate::Severity::WARNING } end |