Class: Dcc::Validate::Result

Inherits:
Lutaml::Model::Serializable
  • Object
show all
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

BusinessRules::Result

Instance Method Summary collapse

Constructor Details

#initialize(issues: [], schema_version: nil, source: nil) ⇒ Result

Returns a new instance of Result.

Parameters:

  • issues (Array<Dcc::Validate::Issue>) (defaults to: [])

    validation issues found.

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

    DCC schema version validated against.

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

    validator name (xsd, schematron, business_rules, all).



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

#errorsArray<Dcc::Validate::Issue>

Returns only the errors.

Returns:



31
32
33
# File 'lib/dcc/validate/result.rb', line 31

def errors
  issues.select(&:failing?)
end

#infosArray<Dcc::Validate::Issue>

Returns only informational issues.

Returns:



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.

Parameters:

Returns:



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.

Returns:

  • (Boolean)

    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.message,
        line: i.line,
        column: i.column,
        path: i.path,
        source: i.source,
      }
    end,
  }
  ::JSON.pretty_generate(payload)
end

#to_sObject



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.message, i.line, i.path] },
  }.to_yaml
end

#warningsArray<Dcc::Validate::Issue>

Returns only the warnings.

Returns:



36
37
38
# File 'lib/dcc/validate/result.rb', line 36

def warnings
  issues.select { |i| i.severity == ::Dcc::Validate::Severity::WARNING }
end