Module: FiberAudit::Reporters::Schema
- Defined in:
- lib/fiber_audit/reporters/schema.rb
Overview
Schema validates audit results and builds deterministic output hashes. It enforces the publication invariant and structural constraints.
Architecture:
- build(result): Converts result protocol to normalized primitive report Hash
- validate!(report_hash): Validates that Hash and returns it
- JSON/Text call build then validate The complete external schema is centralized here so validation cannot drift across reporters. rubocop:disable Metrics/ModuleLength
Constant Summary collapse
- SCHEMA_VERSION =
'1.0'- DISCLAIMER =
'This is a static-only audit. PASS cannot be granted without runtime coverage.'- ALLOWED_STATUSES =
%w[FAIL REVIEW PASS_WITH_WARNINGS NO_FINDINGS].freeze
- ALLOWED_SEVERITIES =
%w[critical high medium low info].freeze
- ALLOWED_CONFIDENCES =
%w[confirmed high medium low unknown].freeze
Class Method Summary collapse
-
.build(result) ⇒ Object
Builds a normalized primitive report hash from result protocol.
- .build_summary(findings, suppressed) ⇒ Object
- .details_error(rule_id, evidence_index, value_class) ⇒ Object
- .json_safe?(value) ⇒ Boolean
- .json_safe_recursive?(value) ⇒ Boolean
- .normalize_coverage(coverage) ⇒ Object
- .normalize_evidence(evidence_array, rule_id) ⇒ Object
- .normalize_evidence_record(evidence, rule_id, idx) ⇒ Object
- .normalize_finding(finding) ⇒ Object
- .normalize_findings(findings_array) ⇒ Object
- .normalize_json_value(value, rule_id, evidence_index) ⇒ Object
- .normalize_location(location) ⇒ Object
- .normalize_optional_field(value, field) ⇒ Object
- .normalize_parse_error(error) ⇒ Object
- .normalize_parse_errors(parse_errors_array) ⇒ Object
- .sort_findings(findings) ⇒ Object
-
.validate!(report_hash) ⇒ Object
Validates a report hash and returns it.
- .validate_coverage!(coverage) ⇒ Object
- .validate_evidence_array!(evidence_array, context, idx) ⇒ Object
- .validate_evidence_hash!(evidence, context, idx, evidence_idx) ⇒ Object
-
.validate_finding_hash!(finding, context, idx) ⇒ Object
The field-by-field checks intentionally mirror Finding's public shape.
- .validate_finding_strings!(rule_id, message, fingerprint, severity, confidence) ⇒ Object
- .validate_finding_structure!(finding) ⇒ Object
- .validate_findings_array!(findings_array, context) ⇒ Object
- .validate_interface!(result) ⇒ Object
- .validate_location!(path, line, column) ⇒ Object
-
.validate_location_hash!(location, context, idx) ⇒ Object
rubocop:enable Metrics/AbcSize.
- .validate_non_negative_integer!(value, field_name) ⇒ Object
- .validate_parse_error_hash!(error, idx) ⇒ Object
- .validate_parse_errors!(parse_errors) ⇒ Object
- .validate_report_structure!(report_hash) ⇒ Object
- .validate_status(status) ⇒ Object
- .validate_status_value!(status) ⇒ Object
- .validate_summary!(summary) ⇒ Object
- .verify_summary_consistency!(report_hash) ⇒ Object
Class Method Details
.build(result) ⇒ Object
Builds a normalized primitive report hash from result protocol. Normalizes Finding/Evidence/Location objects to JSON primitives.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/fiber_audit/reporters/schema.rb', line 31 def build(result) validate_interface!(result) status = validate_status(result.status) findings = normalize_findings(result.findings) suppressed = normalize_findings(result.suppressed) parse_errors = normalize_parse_errors(result.parse_errors) coverage = normalize_coverage(result.coverage) summary = build_summary(findings, suppressed) { schema_version: SCHEMA_VERSION, tool_version: FiberAudit::VERSION, status: status, disclaimer: DISCLAIMER, summary: summary, coverage: coverage, findings: sort_findings(findings), suppressed: sort_findings(suppressed), parse_errors: parse_errors } end |
.build_summary(findings, suppressed) ⇒ Object
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 |
# File 'lib/fiber_audit/reporters/schema.rb', line 540 def build_summary(findings, suppressed) counts = { critical: 0, high: 0, medium: 0, low: 0, info: 0 } findings.each do |finding| severity = finding[:severity].to_sym counts[severity] += 1 if counts.key?(severity) end counts[:suppressed] = suppressed.size counts[:total] = findings.size counts.freeze end |
.details_error(rule_id, evidence_index, value_class) ⇒ Object
262 263 264 |
# File 'lib/fiber_audit/reporters/schema.rb', line 262 def details_error(rule_id, evidence_index, value_class) "Finding (#{rule_id}) evidence[#{evidence_index}].details is not JSON-safe: #{value_class}" end |
.json_safe?(value) ⇒ Boolean
266 267 268 |
# File 'lib/fiber_audit/reporters/schema.rb', line 266 def json_safe?(value) value.is_a?(String) || value.is_a?(Numeric) || value == true || value == false || value.nil? end |
.json_safe_recursive?(value) ⇒ Boolean
270 271 272 273 274 275 276 277 278 279 |
# File 'lib/fiber_audit/reporters/schema.rb', line 270 def json_safe_recursive?(value) case value when Hash value.all? { |k, v| json_safe?(k) && json_safe_recursive?(v) } when Array value.all? { |item| json_safe_recursive?(item) } else json_safe?(value) end end |
.normalize_coverage(coverage) ⇒ Object
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/fiber_audit/reporters/schema.rb', line 307 def normalize_coverage(coverage) unless coverage.respond_to?(:analysed_files) && coverage.respond_to?(:total_call_sites) && coverage.respond_to?(:rules_run) raise ReporterError, 'coverage must respond to :analysed_files, :total_call_sites, :rules_run' end analysed_files = coverage.analysed_files total_call_sites = coverage.total_call_sites rules_run = coverage.rules_run validate_non_negative_integer!(analysed_files, 'coverage.analysed_files') validate_non_negative_integer!(total_call_sites, 'coverage.total_call_sites') validate_non_negative_integer!(rules_run, 'coverage.rules_run') { analysed_files: analysed_files, total_call_sites: total_call_sites, rules_run: rules_run } end |
.normalize_evidence(evidence_array, rule_id) ⇒ Object
211 212 213 214 215 216 217 218 219 |
# File 'lib/fiber_audit/reporters/schema.rb', line 211 def normalize_evidence(evidence_array, rule_id) collection = evidence_array.respond_to?(:to_a) ? evidence_array.to_a : Array(evidence_array) raise ReporterError, "Finding (#{rule_id}) must have non-empty evidence (publication invariant)" if collection.empty? collection.map.with_index do |ev, idx| normalize_evidence_record(ev, rule_id, idx) end end |
.normalize_evidence_record(evidence, rule_id, idx) ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/fiber_audit/reporters/schema.rb', line 221 def normalize_evidence_record(evidence, rule_id, idx) %i[source message].each do |method| next if evidence.respond_to?(method) raise ReporterError, "Finding (#{rule_id}) evidence[#{idx}] must respond to #{method}" end source = evidence.source.to_s = evidence..to_s raise ReporterError, "Finding (#{rule_id}) evidence[#{idx}].source must be non-empty" if source.empty? raise ReporterError, "Finding (#{rule_id}) evidence[#{idx}].message must be non-empty" if .empty? details = evidence.respond_to?(:details) ? evidence.details : {} normalized_details = normalize_json_value(details, rule_id, idx) { source: source, message: , details: normalized_details } end |
.normalize_finding(finding) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/fiber_audit/reporters/schema.rb', line 125 def normalize_finding(finding) validate_finding_structure!(finding) rule_id = finding.rule_id.to_s = finding..to_s fingerprint = finding.fingerprint.to_s severity = finding.severity.to_s confidence = finding.confidence.to_s validate_finding_strings!(rule_id, , fingerprint, severity, confidence) location = normalize_location(finding.location) evidence = normalize_evidence(finding.evidence, rule_id) { rule_id: rule_id, title: normalize_optional_field(finding.title, 'title'), category: normalize_optional_field(finding.category, 'category'), severity: severity, confidence: confidence, location: location, symbol: normalize_optional_field(finding.symbol, 'symbol'), operation: normalize_optional_field(finding.operation, 'operation'), execution_context: normalize_optional_field(finding.execution_context, 'execution_context'), message: , evidence: evidence, remediation: normalize_optional_field(finding.remediation, 'remediation'), fingerprint: fingerprint } end |
.normalize_findings(findings_array) ⇒ Object
120 121 122 123 |
# File 'lib/fiber_audit/reporters/schema.rb', line 120 def normalize_findings(findings_array) collection = findings_array.respond_to?(:to_a) ? findings_array.to_a : Array(findings_array) collection.map { |finding| normalize_finding(finding) } end |
.normalize_json_value(value, rule_id, evidence_index) ⇒ Object
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/fiber_audit/reporters/schema.rb', line 241 def normalize_json_value(value, rule_id, evidence_index) case value when Symbol value.to_s when Hash value.each_with_object({}) do |(key, nested), normalized| unless key.is_a?(String) || key.is_a?(Symbol) raise ReporterError, details_error(rule_id, evidence_index, key.class) end normalized[key.to_s] = normalize_json_value(nested, rule_id, evidence_index) end when Array value.map { |nested| normalize_json_value(nested, rule_id, evidence_index) } else return value if json_safe?(value) raise ReporterError, details_error(rule_id, evidence_index, value.class) end end |
.normalize_location(location) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/fiber_audit/reporters/schema.rb', line 186 def normalize_location(location) return nil if location.nil? return nil unless location.respond_to?(:path) path = location.path.to_s line = location.respond_to?(:line) ? location.line : nil column = location.respond_to?(:column) ? location.column : nil validate_location!(path, line, column) { path: path, line: line, column: column } end |
.normalize_optional_field(value, field) ⇒ Object
178 179 180 181 182 183 184 |
# File 'lib/fiber_audit/reporters/schema.rb', line 178 def normalize_optional_field(value, field) return nil if value.nil? return value.to_s if value.is_a?(Symbol) return value if json_safe?(value) raise ReporterError, "finding.#{field} is not JSON-safe: #{value.class}" end |
.normalize_parse_error(error) ⇒ Object
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/fiber_audit/reporters/schema.rb', line 286 def normalize_parse_error(error) unless error.respond_to?(:path) && error.respond_to?(:message) raise ReporterError, 'parse error must respond to :path and :message' end path = error.path.to_s = error..to_s raise ReporterError, 'parse error path must be non-empty' if path.empty? raise ReporterError, 'parse error message must be non-empty' if .empty? line = error.respond_to?(:line) ? error.line : nil unless line.nil? || (line.is_a?(Integer) && line.positive?) raise ReporterError, "parse error line must be positive Integer or nil, got: #{line.inspect}" end { path: path, message: , line: line } end |
.normalize_parse_errors(parse_errors_array) ⇒ Object
281 282 283 284 |
# File 'lib/fiber_audit/reporters/schema.rb', line 281 def normalize_parse_errors(parse_errors_array) collection = parse_errors_array.respond_to?(:to_a) ? parse_errors_array.to_a : Array(parse_errors_array) collection.map { |error| normalize_parse_error(error) } end |
.sort_findings(findings) ⇒ Object
560 561 562 563 564 565 566 567 568 569 570 |
# File 'lib/fiber_audit/reporters/schema.rb', line 560 def sort_findings(findings) findings.sort_by do |f| [ Severity.index(f[:severity].to_sym), f[:rule_id].to_s, f[:location] ? f[:location][:path].to_s : '', f[:location] ? (f[:location][:line] || Float::INFINITY) : Float::INFINITY, f[:fingerprint].to_s ] end end |
.validate!(report_hash) ⇒ Object
Validates a report hash and returns it. Raises ReporterError on any validation failure.
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/fiber_audit/reporters/schema.rb', line 57 def validate!(report_hash) validate_report_structure!(report_hash) validate_status_value!(report_hash[:status]) validate_summary!(report_hash[:summary]) validate_coverage!(report_hash[:coverage]) validate_findings_array!(report_hash[:findings], 'findings') validate_findings_array!(report_hash[:suppressed], 'suppressed') validate_parse_errors!(report_hash[:parse_errors]) verify_summary_consistency!(report_hash) report_hash.freeze end |
.validate_coverage!(coverage) ⇒ Object
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'lib/fiber_audit/reporters/schema.rb', line 329 def validate_coverage!(coverage) required_keys = %i[analysed_files total_call_sites rules_run] actual_keys = coverage.keys missing = required_keys - actual_keys raise ReporterError, "coverage missing required keys: #{missing.join(', ')}" unless missing.empty? unknown = actual_keys - required_keys raise ReporterError, "coverage has unknown keys: #{unknown.join(', ')}" unless unknown.empty? required_keys.each do |key| value = coverage[key] validate_non_negative_integer!(value, "coverage.#{key}") end end |
.validate_evidence_array!(evidence_array, context, idx) ⇒ Object
459 460 461 462 463 464 465 466 467 |
# File 'lib/fiber_audit/reporters/schema.rb', line 459 def validate_evidence_array!(evidence_array, context, idx) unless evidence_array.is_a?(Array) && !evidence_array.empty? raise ReporterError, "#{context}[#{idx}].evidence must be non-empty Array" end evidence_array.each_with_index do |evidence, evidence_idx| validate_evidence_hash!(evidence, context, idx, evidence_idx) end end |
.validate_evidence_hash!(evidence, context, idx, evidence_idx) ⇒ Object
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 |
# File 'lib/fiber_audit/reporters/schema.rb', line 469 def validate_evidence_hash!(evidence, context, idx, evidence_idx) unless evidence.is_a?(Hash) = "#{context}[#{idx}].evidence[#{evidence_idx}] must be a Hash" raise ReporterError, end required_keys = %i[source message details] actual_keys = evidence.keys missing = required_keys - actual_keys unless missing.empty? raise ReporterError, "#{context}[#{idx}].evidence[#{evidence_idx}] missing required keys: #{missing.join(', ')}" end unknown = actual_keys - required_keys unless unknown.empty? raise ReporterError, "#{context}[#{idx}].evidence[#{evidence_idx}] has unknown keys: #{unknown.join(', ')}" end source = evidence[:source] = evidence[:message] unless source.is_a?(String) && !source.empty? raise ReporterError, "#{context}[#{idx}].evidence[#{evidence_idx}].source must be non-empty string" end unless .is_a?(String) && !.empty? raise ReporterError, "#{context}[#{idx}].evidence[#{evidence_idx}].message must be non-empty string" end details = evidence[:details] return if details.nil? || json_safe_recursive?(details) raise ReporterError, "#{context}[#{idx}].evidence[#{evidence_idx}].details is not JSON-safe" end |
.validate_finding_hash!(finding, context, idx) ⇒ Object
The field-by-field checks intentionally mirror Finding's public shape. rubocop:disable Metrics/AbcSize
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 |
# File 'lib/fiber_audit/reporters/schema.rb', line 387 def validate_finding_hash!(finding, context, idx) raise ReporterError, "#{context}[#{idx}] must be a Hash" unless finding.is_a?(Hash) required_keys = %i[rule_id title category severity confidence location symbol operation execution_context message evidence remediation fingerprint] actual_keys = finding.keys missing = required_keys - actual_keys raise ReporterError, "#{context}[#{idx}] missing required keys: #{missing.join(', ')}" unless missing.empty? unknown = actual_keys - required_keys raise ReporterError, "#{context}[#{idx}] has unknown keys: #{unknown.join(', ')}" unless unknown.empty? %i[rule_id message fingerprint].each do |key| value = finding[key] unless value.is_a?(String) && !value.empty? raise ReporterError, "#{context}[#{idx}].#{key} must be non-empty string" end end unless finding[:severity].is_a?(String) && ALLOWED_SEVERITIES.include?(finding[:severity]) raise ReporterError, "#{context}[#{idx}].severity must be known severity string" end unless finding[:confidence].is_a?(String) && ALLOWED_CONFIDENCES.include?(finding[:confidence]) raise ReporterError, "#{context}[#{idx}].confidence must be known confidence string" end %i[title category symbol operation execution_context remediation].each do |key| value = finding[key] next if value.nil? next if json_safe?(value) raise ReporterError, "#{context}[#{idx}].#{key} is not JSON-safe" end validate_location_hash!(finding[:location], context, idx) validate_evidence_array!(finding[:evidence], context, idx) end |
.validate_finding_strings!(rule_id, message, fingerprint, severity, confidence) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/fiber_audit/reporters/schema.rb', line 164 def validate_finding_strings!(rule_id, , fingerprint, severity, confidence) raise ReporterError, 'finding rule_id must be non-empty' if rule_id.empty? raise ReporterError, 'finding message must be non-empty' if .empty? raise ReporterError, 'finding fingerprint must be non-empty' if fingerprint.empty? raise ReporterError, "finding has invalid severity: #{severity.inspect}" unless ALLOWED_SEVERITIES.include?(severity) return if ALLOWED_CONFIDENCES.include?(confidence) raise ReporterError, "finding has invalid confidence: #{confidence.inspect}" end |
.validate_finding_structure!(finding) ⇒ Object
156 157 158 159 160 161 162 |
# File 'lib/fiber_audit/reporters/schema.rb', line 156 def validate_finding_structure!(finding) %i[rule_id severity confidence message evidence fingerprint].each do |method| next if finding.respond_to?(method) raise ReporterError, "finding must respond to #{method}" end end |
.validate_findings_array!(findings_array, context) ⇒ Object
377 378 379 380 381 382 383 |
# File 'lib/fiber_audit/reporters/schema.rb', line 377 def validate_findings_array!(findings_array, context) raise ReporterError, "#{context} must be an Array" unless findings_array.is_a?(Array) findings_array.each_with_index do |finding, idx| validate_finding_hash!(finding, context, idx) end end |
.validate_interface!(result) ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/fiber_audit/reporters/schema.rb', line 70 def validate_interface!(result) %i[findings suppressed parse_errors coverage status].each do |method| next if result.respond_to?(method) raise ReporterError, "result must respond to #{method}" end end |
.validate_location!(path, line, column) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/fiber_audit/reporters/schema.rb', line 199 def validate_location!(path, line, column) raise ReporterError, 'location path must be non-empty string' if path.empty? unless line.nil? || (line.is_a?(Integer) && line.positive?) raise ReporterError, "location line must be positive Integer or nil, got: #{line.inspect}" end return if column.nil? || (column.is_a?(Integer) && column >= 0) raise ReporterError, "location column must be non-negative Integer or nil, got: #{column.inspect}" end |
.validate_location_hash!(location, context, idx) ⇒ Object
rubocop:enable Metrics/AbcSize
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
# File 'lib/fiber_audit/reporters/schema.rb', line 428 def validate_location_hash!(location, context, idx) return if location.nil? raise ReporterError, "#{context}[#{idx}].location must be a Hash or nil" unless location.is_a?(Hash) required_keys = %i[path line column] actual_keys = location.keys missing = required_keys - actual_keys raise ReporterError, "#{context}[#{idx}].location missing required keys: #{missing.join(', ')}" unless missing.empty? unknown = actual_keys - required_keys raise ReporterError, "#{context}[#{idx}].location has unknown keys: #{unknown.join(', ')}" unless unknown.empty? path = location[:path] line = location[:line] column = location[:column] unless path.is_a?(String) && !path.empty? raise ReporterError, "#{context}[#{idx}].location.path must be non-empty string" end unless line.nil? || (line.is_a?(Integer) && line.positive?) raise ReporterError, "#{context}[#{idx}].location.line must be positive Integer or nil" end return if column.nil? || (column.is_a?(Integer) && column >= 0) raise ReporterError, "#{context}[#{idx}].location.column must be non-negative Integer or nil" end |
.validate_non_negative_integer!(value, field_name) ⇒ Object
371 372 373 374 375 |
# File 'lib/fiber_audit/reporters/schema.rb', line 371 def validate_non_negative_integer!(value, field_name) return if value.is_a?(Integer) && value >= 0 raise ReporterError, "#{field_name} must be a non-negative Integer, got: #{value.inspect}" end |
.validate_parse_error_hash!(error, idx) ⇒ Object
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 |
# File 'lib/fiber_audit/reporters/schema.rb', line 513 def validate_parse_error_hash!(error, idx) raise ReporterError, "parse_errors[#{idx}] must be a Hash" unless error.is_a?(Hash) required_keys = %i[path message line] actual_keys = error.keys missing = required_keys - actual_keys raise ReporterError, "parse_errors[#{idx}] missing required keys: #{missing.join(', ')}" unless missing.empty? unknown = actual_keys - required_keys raise ReporterError, "parse_errors[#{idx}] has unknown keys: #{unknown.join(', ')}" unless unknown.empty? path = error[:path] = error[:message] line = error[:line] raise ReporterError, "parse_errors[#{idx}].path must be non-empty string" unless path.is_a?(String) && !path.empty? unless .is_a?(String) && !.empty? raise ReporterError, "parse_errors[#{idx}].message must be non-empty string" end return if line.nil? || (line.is_a?(Integer) && line.positive?) raise ReporterError, "parse_errors[#{idx}].line must be positive Integer or nil" end |
.validate_parse_errors!(parse_errors) ⇒ Object
505 506 507 508 509 510 511 |
# File 'lib/fiber_audit/reporters/schema.rb', line 505 def validate_parse_errors!(parse_errors) raise ReporterError, 'parse_errors must be an Array' unless parse_errors.is_a?(Array) parse_errors.each_with_index do |error, idx| validate_parse_error_hash!(error, idx) end end |
.validate_report_structure!(report_hash) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/fiber_audit/reporters/schema.rb', line 85 def validate_report_structure!(report_hash) required_keys = %i[schema_version tool_version status disclaimer summary coverage findings suppressed parse_errors] actual_keys = report_hash.keys missing = required_keys - actual_keys raise ReporterError, "report missing required keys: #{missing.join(', ')}" unless missing.empty? unknown = actual_keys - required_keys return if unknown.empty? raise ReporterError, "report has unknown keys: #{unknown.join(', ')}" end |
.validate_status(status) ⇒ Object
78 79 80 81 82 83 |
# File 'lib/fiber_audit/reporters/schema.rb', line 78 def validate_status(status) status_str = status.to_s.upcase return status_str if ALLOWED_STATUSES.include?(status_str) raise ReporterError, "invalid status: #{status.inspect}, must be one of #{ALLOWED_STATUSES.join(', ')}" end |
.validate_status_value!(status) ⇒ Object
98 99 100 101 102 |
# File 'lib/fiber_audit/reporters/schema.rb', line 98 def validate_status_value!(status) return if status.is_a?(String) && ALLOWED_STATUSES.include?(status) raise ReporterError, "invalid status: #{status.inspect}" end |
.validate_summary!(summary) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/fiber_audit/reporters/schema.rb', line 104 def validate_summary!(summary) required_keys = %i[critical high medium low info suppressed total] actual_keys = summary.keys missing = required_keys - actual_keys raise ReporterError, "summary missing required keys: #{missing.join(', ')}" unless missing.empty? unknown = actual_keys - required_keys raise ReporterError, "summary has unknown keys: #{unknown.join(', ')}" unless unknown.empty? required_keys.each do |key| value = summary[key] raise ReporterError, "summary.#{key} must be a non-negative Integer" unless value.is_a?(Integer) && value >= 0 end end |
.verify_summary_consistency!(report_hash) ⇒ Object
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/fiber_audit/reporters/schema.rb', line 345 def verify_summary_consistency!(report_hash) summary = report_hash[:summary] findings = report_hash[:findings] suppressed = report_hash[:suppressed] counts = Hash.new(0) findings.each { |f| counts[f[:severity]] += 1 } %w[critical high medium low info].each do |sev| expected = counts[sev] actual = summary[sev.to_sym] unless actual == expected raise ReporterError, "summary.#{sev} count (#{actual}) does not match findings (#{expected})" end end unless summary[:suppressed] == suppressed.size raise ReporterError, "summary.suppressed (#{summary[:suppressed]}) does not match suppressed array size (#{suppressed.size})" end return if summary[:total] == findings.size raise ReporterError, "summary.total (#{summary[:total]}) does not match findings count (#{findings.size})" end |