Module: Harnex::ArtifactReport

Defined in:
lib/harnex/artifact_report.rb

Defined Under Namespace

Classes: ValidationResult

Constant Summary collapse

SCHEMA =
"harnex.artifact_report.v1"
MAX_BYTES =
256 * 1024
MAX_ARTIFACTS =
50
MAX_COMMANDS =
50
MAX_CANONICAL_ARTIFACTS =
50
MAX_EVIDENCE_ITEMS =
20
MAX_STRING_LENGTH =
2_000
MAX_DIAGNOSTICS =
100
REPORT_STATUSES =
%w[in_progress pass fail blocked unknown].freeze
VALIDATION_STATUSES =
%w[in_progress pass fail not_run unknown].freeze
OUTCOME_STATUSES =
%w[accepted rejected no_change unknown].freeze
ACCEPTED_OUTCOME_STATUSES =
%w[accepted no_change].freeze

Class Method Summary collapse

Class Method Details

.accepted_final?(result) ⇒ Boolean

Returns:

  • (Boolean)


201
202
203
# File 'lib/harnex/artifact_report.rb', line 201

def accepted_final?(result)
  result.ok && ACCEPTED_OUTCOME_STATUSES.include?(outcome_status(result))
end

.boolean?(value) ⇒ Boolean

Returns:

  • (Boolean)


685
686
687
# File 'lib/harnex/artifact_report.rb', line 685

def boolean?(value)
  value == true || value == false
end

.bounded_string(value) ⇒ Object



649
650
651
652
653
654
# File 'lib/harnex/artifact_report.rb', line 649

def bounded_string(value)
  text = value.to_s
  return text if text.length <= MAX_STRING_LENGTH

  text[0, MAX_STRING_LENGTH]
end

.bounded_string_or_nil(value) ⇒ Object



643
644
645
646
647
# File 'lib/harnex/artifact_report.rb', line 643

def bounded_string_or_nil(value)
  return nil if value.nil?

  bounded_string(value)
end

.build_payload(path, bytes:, sha256:, report:) ⇒ Object



507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# File 'lib/harnex/artifact_report.rb', line 507

def build_payload(path, bytes:, sha256:, report:)
  artifacts = compact_artifacts(report["artifacts"])
  validation = compact_validation(report["validation"])
  outcome = compact_outcome(report["outcome"])
  payload = {
    "artifact_report" => (
      path,
      bytes: bytes,
      sha256: sha256,
      ingest_status: "ok",
      schema: SCHEMA
    ).merge(
      "report_status" => bounded_string_or_nil(report["status"]),
      "canonical_artifacts" => string_array(report["canonical_artifacts"], max_items: MAX_CANONICAL_ARTIFACTS),
      "artifact_count" => artifacts.length
    )
  }
  payload["validation"] = validation if validation
  payload["artifacts"] = artifacts unless artifacts.empty?
  payload["outcome"] = outcome if outcome
  payload
end

.compact_artifacts(value) ⇒ Object



599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
# File 'lib/harnex/artifact_report.rb', line 599

def compact_artifacts(value)
  Array(value).first(MAX_ARTIFACTS).filter_map do |entry|
    next unless entry.is_a?(Hash)

    compact = {
      "type" => bounded_string_or_nil(entry["type"]),
      "summary" => bounded_string_or_nil(entry["summary"]),
      "evidence" => string_array(entry["evidence"], max_items: MAX_EVIDENCE_ITEMS),
      "confidence" => finite_float_or_nil(entry["confidence"]),
      "canonical_ref" => bounded_string_or_nil(entry["canonical_ref"])
    }
    compact.delete_if { |_key, item| item.nil? || item == [] }
    compact.empty? ? nil : compact
  end
end

.compact_commands(value) ⇒ Object



569
570
571
572
573
574
575
576
577
578
579
580
581
582
# File 'lib/harnex/artifact_report.rb', line 569

def compact_commands(value)
  Array(value).first(MAX_COMMANDS).filter_map do |entry|
    next unless entry.is_a?(Hash)

    compact = {
      "cmd" => bounded_string_or_nil(entry["cmd"]),
      "exit_code" => integer_or_nil(entry["exit_code"]),
      "status" => bounded_string_or_nil(entry["status"]),
      "duration_s" => finite_float_or_nil(entry["duration_s"])
    }
    compact.delete_if { |_key, item| item.nil? }
    compact.empty? ? nil : compact
  end
end

.compact_outcome(value) ⇒ Object



584
585
586
587
588
589
590
591
592
593
594
595
596
597
# File 'lib/harnex/artifact_report.rb', line 584

def compact_outcome(value)
  return nil unless value.is_a?(Hash)

  status = value["status"].to_s
  return nil unless OUTCOME_STATUSES.include?(status)

  payload = {
    "status" => status,
    "summary" => bounded_string_or_nil(value["summary"]),
    "commit_sha" => bounded_string_or_nil(value["commit_sha"])
  }
  payload.delete_if { |_key, item| item.nil? }
  payload
end

.compact_validation(value) ⇒ Object



558
559
560
561
562
563
564
565
566
567
# File 'lib/harnex/artifact_report.rb', line 558

def compact_validation(value)
  return nil unless value.is_a?(Hash)

  payload = {}
  payload["status"] = bounded_string_or_nil(value["status"])
  payload["commands"] = compact_commands(value["commands"])
  payload["final_reported"] = !!value["final_reported"] if value.key?("final_reported")
  payload.delete_if { |_key, item| item.nil? || item == [] }
  payload.empty? ? nil : payload
end

.diagnostic(code, path, message) ⇒ Object



628
629
630
631
632
633
634
# File 'lib/harnex/artifact_report.rb', line 628

def diagnostic(code, path, message)
  {
    "code" => code,
    "path" => path,
    "message" => bounded_string(message)
  }
end

.file_sha256(path) ⇒ Object



689
690
691
692
693
694
695
696
# File 'lib/harnex/artifact_report.rb', line 689

def file_sha256(path)
  digest = Digest::SHA256.new
  File.open(path, "rb") do |file|
    buffer = +""
    digest.update(buffer) while file.read(16 * 1024, buffer)
  end
  digest.hexdigest
end

.fingerprint(path) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/harnex/artifact_report.rb', line 205

def fingerprint(path)
  report_path = File.expand_path(path.to_s)
  return nil unless File.file?(report_path)

  stat = File.stat(report_path)
  {
    "bytes" => stat.size,
    "sha256" => file_sha256(report_path),
    "mtime_ns" => (stat.mtime.to_r * 1_000_000_000).to_i,
    "ctime_ns" => (stat.ctime.to_r * 1_000_000_000).to_i,
    "inode" => stat.ino
  }
rescue StandardError
  nil
end

.finite_float_or_nil(value) ⇒ Object



664
665
666
667
668
669
670
671
# File 'lib/harnex/artifact_report.rb', line 664

def finite_float_or_nil(value)
  return nil if value.nil?

  number = Float(value)
  number.finite? ? number : nil
rescue ArgumentError, TypeError
  nil
end

.finite_non_negative_number?(value) ⇒ Boolean

Returns:

  • (Boolean)


677
678
679
# File 'lib/harnex/artifact_report.rb', line 677

def finite_non_negative_number?(value)
  finite_number?(value) && value >= 0
end

.finite_number?(value) ⇒ Boolean

Returns:

  • (Boolean)


673
674
675
# File 'lib/harnex/artifact_report.rb', line 673

def finite_number?(value)
  value.is_a?(Numeric) && value.finite?
end

.ingest(path) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/harnex/artifact_report.rb', line 112

def ingest(path)
  result = validate(path)
  case result.status
  when "missing"
    missing(result.path)
  when "oversized"
    warning(
      result.path,
      bytes: result.bytes,
      sha256: result.sha256,
      ingest_status: "oversized",
      warning: "artifact report is #{result.bytes} bytes; max is #{MAX_BYTES} bytes",
      diagnostics: result.diagnostics
    )
  when "malformed"
    warning(
      result.path,
      bytes: result.bytes,
      sha256: result.sha256,
      ingest_status: "malformed",
      warning: "malformed artifact report JSON",
      diagnostics: result.diagnostics
    )
  when "unsupported_schema"
    warning(
      result.path,
      bytes: result.bytes,
      sha256: result.sha256,
      ingest_status: "unsupported_schema",
      schema: result.schema,
      warning: "unsupported artifact report schema; expected #{SCHEMA}",
      diagnostics: result.diagnostics
    )
  when "error"
    warning(
      result.path,
      bytes: result.bytes,
      sha256: result.sha256,
      ingest_status: "error",
      warning: "artifact report ingest failed",
      diagnostics: result.diagnostics
    )
  else
    payload = build_payload(
      result.path,
      bytes: result.bytes,
      sha256: result.sha256,
      report: result.report
    )
    unless result.ok
       = payload.fetch("artifact_report")
      ["ingest_status"] = "invalid"
      ["warning"] = "artifact report does not conform to #{SCHEMA}"
      ["diagnostics"] = result.diagnostics
    end
    payload
  end
end

.initialize_file(path, force: false) ⇒ Object

Raises:

  • (ArgumentError)


171
172
173
174
175
176
177
178
179
180
181
# File 'lib/harnex/artifact_report.rb', line 171

def initialize_file(path, force: false)
  report_path = File.expand_path(path.to_s)
  raise ArgumentError, "artifact report path is required" if path.to_s.strip.empty?
  if File.exist?(report_path) && !force
    raise ArgumentError, "artifact report already exists: #{report_path} (use --force to replace it)"
  end

  FileUtils.mkdir_p(File.dirname(report_path))
  File.write(report_path, JSON.pretty_generate(template) + "\n")
  report_path
end

.integer_or_nil(value) ⇒ Object



656
657
658
659
660
661
662
# File 'lib/harnex/artifact_report.rb', line 656

def integer_or_nil(value)
  return nil if value.nil?

  Integer(value)
rescue ArgumentError, TypeError
  nil
end

.metadata(path, bytes:, sha256:, ingest_status:, schema: nil) ⇒ Object



548
549
550
551
552
553
554
555
556
# File 'lib/harnex/artifact_report.rb', line 548

def (path, bytes:, sha256:, ingest_status:, schema: nil)
  {
    "path" => path.to_s,
    "bytes" => bytes,
    "sha256" => sha256,
    "ingest_status" => ingest_status,
    "schema" => schema
  }
end

.missing(path) ⇒ Object



530
531
532
533
534
535
536
537
538
539
# File 'lib/harnex/artifact_report.rb', line 530

def missing(path)
  warning(
    path,
    bytes: nil,
    sha256: nil,
    ingest_status: "missing",
    warning: "artifact report not found",
    diagnostics: [diagnostic("report_missing", "$", "artifact report file does not exist")]
  )
end

.non_empty_string?(value) ⇒ Boolean

Returns:

  • (Boolean)


681
682
683
# File 'lib/harnex/artifact_report.rb', line 681

def non_empty_string?(value)
  value.is_a?(String) && !value.empty? && value.length <= MAX_STRING_LENGTH
end

.outcome_status(result) ⇒ Object



221
222
223
224
225
226
227
# File 'lib/harnex/artifact_report.rb', line 221

def outcome_status(result)
  report = result.respond_to?(:report) ? result.report : result
  return nil unless report.is_a?(Hash)

  outcome = report["outcome"]
  outcome.is_a?(Hash) ? outcome["status"] : nil
end

.safe_file_sha256(path) ⇒ Object



704
705
706
707
708
# File 'lib/harnex/artifact_report.rb', line 704

def safe_file_sha256(path)
  file_sha256(path) if File.file?(path)
rescue StandardError
  nil
end

.safe_file_size(path) ⇒ Object



698
699
700
701
702
# File 'lib/harnex/artifact_report.rb', line 698

def safe_file_size(path)
  File.size(path) if File.file?(path)
rescue StandardError
  nil
end

.string_array(value, max_items:) ⇒ Object



636
637
638
639
640
641
# File 'lib/harnex/artifact_report.rb', line 636

def string_array(value, max_items:)
  Array(value).first(max_items).filter_map do |item|
    text = bounded_string_or_nil(item)
    text unless text.nil? || text.empty?
  end
end

.templateObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/harnex/artifact_report.rb', line 183

def template
  {
    "schema" => SCHEMA,
    "status" => "in_progress",
    "canonical_artifacts" => [],
    "outcome" => {
      "status" => "unknown",
      "summary" => ""
    },
    "validation" => {
      "status" => "not_run",
      "commands" => [],
      "final_reported" => false
    },
    "artifacts" => []
  }
end

.validate(path, final: false) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/harnex/artifact_report.rb', line 41

def validate(path, final: false)
  report_path = File.expand_path(path.to_s)
  return validation_failure(
    report_path,
    status: "missing",
    diagnostics: [diagnostic("report_missing", "$", "artifact report file does not exist")]
  ) unless File.file?(report_path)

  bytes = File.size(report_path)
  sha256 = file_sha256(report_path)
  if bytes > MAX_BYTES
    return validation_failure(
      report_path,
      status: "oversized",
      bytes: bytes,
      sha256: sha256,
      diagnostics: [
        diagnostic("report_oversized", "$", "artifact report exceeds the #{MAX_BYTES}-byte limit")
      ]
    )
  end

  parsed = JSON.parse(File.read(report_path, mode: "rb"))
  unless parsed.is_a?(Hash)
    return validation_failure(
      report_path,
      status: "malformed",
      bytes: bytes,
      sha256: sha256,
      diagnostics: [diagnostic("object_required", "$", "artifact report must be a JSON object")]
    )
  end

  schema = parsed["schema"].is_a?(String) ? bounded_string(parsed["schema"]) : nil
  diagnostics = validate_document(parsed, final: final)
  status = if parsed["schema"] != SCHEMA
             "unsupported_schema"
           elsif diagnostics.empty?
             "valid"
           else
             "invalid"
           end

  ValidationResult.new(
    ok: diagnostics.empty?,
    status: status,
    path: report_path,
    bytes: bytes,
    sha256: sha256,
    schema: schema,
    diagnostics: diagnostics,
    report: parsed
  )
rescue JSON::ParserError
  validation_failure(
    report_path,
    status: "malformed",
    bytes: safe_file_size(report_path),
    sha256: safe_file_sha256(report_path),
    diagnostics: [diagnostic("json_invalid", "$", "artifact report is not valid JSON")]
  )
rescue StandardError
  validation_failure(
    report_path,
    status: "error",
    bytes: safe_file_size(report_path),
    sha256: safe_file_sha256(report_path),
    diagnostics: [diagnostic("read_error", "$", "artifact report could not be read")]
  )
end

.validate_artifacts(value, diagnostics) ⇒ Object



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/harnex/artifact_report.rb', line 348

def validate_artifacts(value, diagnostics)
  unless value.is_a?(Array)
    diagnostics << diagnostic("array_required", "$.artifacts", "artifacts must be an array")
    return
  end
  if value.length > MAX_ARTIFACTS
    diagnostics << diagnostic(
      "too_many_items",
      "$.artifacts",
      "artifacts may contain at most #{MAX_ARTIFACTS} entries"
    )
  end

  value.first(MAX_ARTIFACTS).each_with_index do |entry, index|
    path = "$.artifacts[#{index}]"
    unless entry.is_a?(Hash)
      diagnostics << diagnostic("object_required", path, "artifact must be an object")
      next
    end

    validate_required_string(entry, "type", "#{path}.type", diagnostics)
    validate_required_string(entry, "summary", "#{path}.summary", diagnostics)
    validate_string_array_value(
      entry["evidence"],
      "#{path}.evidence",
      max_items: MAX_EVIDENCE_ITEMS,
      diagnostics: diagnostics,
      required: true
    )
    confidence = entry["confidence"]
    unless finite_number?(confidence) && confidence >= 0.0 && confidence <= 1.0
      diagnostics << diagnostic(
        "range",
        "#{path}.confidence",
        "artifact confidence must be a finite number from 0 through 1"
      )
    end
    validate_optional_string(entry, "canonical_ref", "#{path}.canonical_ref", diagnostics)
  end
end

.validate_commands(value, diagnostics) ⇒ Object



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/harnex/artifact_report.rb', line 309

def validate_commands(value, diagnostics)
  unless value.is_a?(Array)
    diagnostics << diagnostic("array_required", "$.validation.commands", "validation.commands must be an array")
    return
  end
  if value.length > MAX_COMMANDS
    diagnostics << diagnostic(
      "too_many_items",
      "$.validation.commands",
      "validation.commands may contain at most #{MAX_COMMANDS} entries"
    )
  end

  value.first(MAX_COMMANDS).each_with_index do |entry, index|
    path = "$.validation.commands[#{index}]"
    unless entry.is_a?(Hash)
      diagnostics << diagnostic("object_required", path, "validation command must be an object")
      next
    end

    validate_required_string(entry, "cmd", "#{path}.cmd", diagnostics)
    unless entry["exit_code"].is_a?(Integer)
      diagnostics << diagnostic(
        "integer_required",
        "#{path}.exit_code",
        "validation command exit_code must be an integer"
      )
    end
    validate_optional_string(entry, "status", "#{path}.status", diagnostics)
    if entry.key?("duration_s") && !finite_non_negative_number?(entry["duration_s"])
      diagnostics << diagnostic(
        "number_required",
        "#{path}.duration_s",
        "validation command duration_s must be a finite non-negative number"
      )
    end
  end
end

.validate_document(report, final: false) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/harnex/artifact_report.rb', line 229

def validate_document(report, final: false)
  diagnostics = []
  validate_schema(report, diagnostics)
  validate_optional_enum(report, "status", REPORT_STATUSES, diagnostics)
  validate_string_array_field(
    report,
    "canonical_artifacts",
    max_items: MAX_CANONICAL_ARTIFACTS,
    diagnostics: diagnostics
  )
  validate_outcome(report["outcome"], diagnostics) if report.key?("outcome")
  validate_validation(report["validation"], diagnostics) if report.key?("validation")
  validate_artifacts(report["artifacts"], diagnostics) if report.key?("artifacts")
  validate_final_contract(report, diagnostics) if final
  diagnostics.first(MAX_DIAGNOSTICS)
end

.validate_final_contract(report, diagnostics) ⇒ Object



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
426
427
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
# File 'lib/harnex/artifact_report.rb', line 389

def validate_final_contract(report, diagnostics)
  unless report["status"] == "pass"
    diagnostics << diagnostic("final_status", "$.status", "final report status must be pass")
  end

  outcome = report["outcome"]
  unless outcome.is_a?(Hash)
    diagnostics << diagnostic("required", "$.outcome", "final report requires an outcome object")
  else
    unless ACCEPTED_OUTCOME_STATUSES.include?(outcome["status"])
      diagnostics << diagnostic(
        "outcome_not_accepted",
        "$.outcome.status",
        "final outcome must be accepted or no_change"
      )
    end
    unless non_empty_string?(outcome["summary"])
      diagnostics << diagnostic(
        "required",
        "$.outcome.summary",
        "final outcome requires a non-empty summary"
      )
    end
  end

  validation = report["validation"]
  unless validation.is_a?(Hash)
    diagnostics << diagnostic("required", "$.validation", "final report requires a validation object")
    return
  end

  expected_statuses = outcome.is_a?(Hash) && outcome["status"] == "no_change" ? %w[pass not_run] : %w[pass]
  unless expected_statuses.include?(validation["status"])
    diagnostics << diagnostic(
      "final_validation_status",
      "$.validation.status",
      "final validation.status must be #{expected_statuses.join(' or ')}"
    )
  end
  unless validation["commands"].is_a?(Array)
    diagnostics << diagnostic(
      "required",
      "$.validation.commands",
      "final report requires a validation.commands array"
    )
  end
  unless validation["final_reported"] == true
    diagnostics << diagnostic(
      "final_reported",
      "$.validation.final_reported",
      "final report requires validation.final_reported=true"
    )
  end

  Array(validation["commands"]).first(MAX_COMMANDS).each_with_index do |entry, index|
    next unless entry.is_a?(Hash) && entry["exit_code"].is_a?(Integer)
    next if entry["exit_code"].zero?

    diagnostics << diagnostic(
      "command_failed",
      "$.validation.commands[#{index}].exit_code",
      "accepted final reports require validation command exit_code=0"
    )
  end
end

.validate_optional_enum(report, key, values, diagnostics) ⇒ Object



255
256
257
258
259
260
261
262
# File 'lib/harnex/artifact_report.rb', line 255

def validate_optional_enum(report, key, values, diagnostics)
  return unless report.key?(key)

  value = report[key]
  unless value.is_a?(String) && values.include?(value)
    diagnostics << diagnostic("enum", "$.#{key}", "#{key} must be one of #{values.join(', ')}")
  end
end

.validate_optional_string(hash, key, path, diagnostics) ⇒ Object



496
497
498
499
500
501
502
503
504
505
# File 'lib/harnex/artifact_report.rb', line 496

def validate_optional_string(hash, key, path, diagnostics)
  return unless hash.key?(key)
  return if hash[key].is_a?(String) && hash[key].length <= MAX_STRING_LENGTH

  diagnostics << diagnostic(
    "string_required",
    path,
    "#{path.delete_prefix('$.')} must be a string no longer than #{MAX_STRING_LENGTH} characters"
  )
end

.validate_outcome(value, diagnostics) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/harnex/artifact_report.rb', line 264

def validate_outcome(value, diagnostics)
  unless value.is_a?(Hash)
    diagnostics << diagnostic("object_required", "$.outcome", "outcome must be an object")
    return
  end

  status = value["status"]
  unless status.is_a?(String) && OUTCOME_STATUSES.include?(status)
    diagnostics << diagnostic(
      "enum",
      "$.outcome.status",
      "outcome.status must be one of #{OUTCOME_STATUSES.join(', ')}"
    )
  end
  validate_optional_string(value, "summary", "$.outcome.summary", diagnostics)
  validate_optional_string(value, "commit_sha", "$.outcome.commit_sha", diagnostics)
end

.validate_required_string(hash, key, path, diagnostics) ⇒ Object



490
491
492
493
494
# File 'lib/harnex/artifact_report.rb', line 490

def validate_required_string(hash, key, path, diagnostics)
  return if non_empty_string?(hash[key])

  diagnostics << diagnostic("string_required", path, "#{path.delete_prefix('$.')} must be a non-empty string")
end

.validate_schema(report, diagnostics) ⇒ Object



246
247
248
249
250
251
252
253
# File 'lib/harnex/artifact_report.rb', line 246

def validate_schema(report, diagnostics)
  value = report["schema"]
  if !value.is_a?(String) || value.empty?
    diagnostics << diagnostic("schema_required", "$.schema", "schema must be a non-empty string")
  elsif value != SCHEMA
    diagnostics << diagnostic("schema_unsupported", "$.schema", "schema must equal #{SCHEMA}")
  end
end

.validate_string_array_field(report, key, max_items:, diagnostics:) ⇒ Object



455
456
457
458
459
460
461
462
463
464
465
# File 'lib/harnex/artifact_report.rb', line 455

def validate_string_array_field(report, key, max_items:, diagnostics:)
  return unless report.key?(key)

  validate_string_array_value(
    report[key],
    "$.#{key}",
    max_items: max_items,
    diagnostics: diagnostics,
    required: true
  )
end

.validate_string_array_value(value, path, max_items:, diagnostics:, required:) ⇒ Object



467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# File 'lib/harnex/artifact_report.rb', line 467

def validate_string_array_value(value, path, max_items:, diagnostics:, required:)
  if value.nil? && !required
    return
  end
  unless value.is_a?(Array)
    diagnostics << diagnostic("array_required", path, "#{path.delete_prefix('$.')} must be an array")
    return
  end
  if value.length > max_items
    diagnostics << diagnostic("too_many_items", path, "#{path.delete_prefix('$.')} may contain at most #{max_items} entries")
  end

  value.first(max_items).each_with_index do |item, index|
    next if non_empty_string?(item)

    diagnostics << diagnostic(
      "string_required",
      "#{path}[#{index}]",
      "#{path.delete_prefix('$.')} entries must be non-empty strings"
    )
  end
end

.validate_validation(value, diagnostics) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/harnex/artifact_report.rb', line 282

def validate_validation(value, diagnostics)
  unless value.is_a?(Hash)
    diagnostics << diagnostic("object_required", "$.validation", "validation must be an object")
    return
  end

  if value.key?("status")
    status = value["status"]
    unless status.is_a?(String) && VALIDATION_STATUSES.include?(status)
      diagnostics << diagnostic(
        "enum",
        "$.validation.status",
        "validation.status must be one of #{VALIDATION_STATUSES.join(', ')}"
      )
    end
  end

  validate_commands(value["commands"], diagnostics) if value.key?("commands")
  if value.key?("final_reported") && !boolean?(value["final_reported"])
    diagnostics << diagnostic(
      "boolean_required",
      "$.validation.final_reported",
      "validation.final_reported must be true or false"
    )
  end
end

.validation_failure(path, status:, diagnostics:, bytes: nil, sha256: nil) ⇒ Object



615
616
617
618
619
620
621
622
623
624
625
626
# File 'lib/harnex/artifact_report.rb', line 615

def validation_failure(path, status:, diagnostics:, bytes: nil, sha256: nil)
  ValidationResult.new(
    ok: false,
    status: status,
    path: path,
    bytes: bytes,
    sha256: sha256,
    schema: nil,
    diagnostics: diagnostics.first(MAX_DIAGNOSTICS),
    report: nil
  )
end

.warning(path, bytes:, sha256:, ingest_status:, warning:, schema: nil, diagnostics: nil) ⇒ Object



541
542
543
544
545
546
# File 'lib/harnex/artifact_report.rb', line 541

def warning(path, bytes:, sha256:, ingest_status:, warning:, schema: nil, diagnostics: nil)
  report = (path, bytes: bytes, sha256: sha256, ingest_status: ingest_status, schema: schema)
  report["warning"] = bounded_string(warning)
  report["diagnostics"] = Array(diagnostics).first(MAX_DIAGNOSTICS) unless Array(diagnostics).empty?
  { "artifact_report" => report }
end