|
| 1 | +#!/usr/bin/env ruby |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require "json" |
| 5 | +require "yaml" |
| 6 | + |
| 7 | +ROOT = File.expand_path("..", __dir__) |
| 8 | +SCHEMA_PATH = File.join(ROOT, "schemas", "skill.schema.json") |
| 9 | +DEFAULT_GLOBS = [ |
| 10 | + File.join(ROOT, "skills", "*", "*", "SKILL.md"), |
| 11 | + File.join(ROOT, "roles", "*", "SKILL.md") |
| 12 | +].freeze |
| 13 | + |
| 14 | +def usage |
| 15 | + warn "Usage: ruby scripts/validate_skill_schema.rb [SKILL.md ...]" |
| 16 | +end |
| 17 | + |
| 18 | +def load_schema |
| 19 | + JSON.parse(File.read(SCHEMA_PATH)) |
| 20 | +rescue Errno::ENOENT |
| 21 | + abort "Schema not found: #{SCHEMA_PATH}" |
| 22 | +rescue JSON::ParserError => e |
| 23 | + abort "Invalid JSON schema #{SCHEMA_PATH}: #{e.message}" |
| 24 | +end |
| 25 | + |
| 26 | +def skill_files(args) |
| 27 | + files = args.empty? ? DEFAULT_GLOBS.flat_map { |pattern| Dir.glob(pattern) } : args |
| 28 | + files.map { |path| File.expand_path(path, Dir.pwd) }.sort |
| 29 | +end |
| 30 | + |
| 31 | +def frontmatter_for(path) |
| 32 | + text = File.read(path) |
| 33 | + match = text.match(/\A---\s*\n(.*?)\n---\s*(?:\n|\z)/m) |
| 34 | + raise "missing YAML frontmatter delimited by ---" unless match |
| 35 | + |
| 36 | + YAML.safe_load(match[1], permitted_classes: [], aliases: false) || {} |
| 37 | +rescue Psych::SyntaxError => e |
| 38 | + raise "invalid YAML frontmatter: #{e.message}" |
| 39 | +end |
| 40 | + |
| 41 | +def type_name(value) |
| 42 | + case value |
| 43 | + when String then "string" |
| 44 | + when Array then "array" |
| 45 | + when Hash then "object" |
| 46 | + when TrueClass, FalseClass then "boolean" |
| 47 | + when Integer then "integer" |
| 48 | + when Float then "number" |
| 49 | + when NilClass then "null" |
| 50 | + else value.class.name |
| 51 | + end |
| 52 | +end |
| 53 | + |
| 54 | +def validate_type(value, expected) |
| 55 | + Array(expected).include?(type_name(value)) |
| 56 | +end |
| 57 | + |
| 58 | +def validate_string(path, value, schema, errors) |
| 59 | + return unless value.is_a?(String) |
| 60 | + |
| 61 | + min = schema["minLength"] |
| 62 | + errors << "#{path} must be at least #{min} characters" if min && value.length < min |
| 63 | + |
| 64 | + pattern = schema["pattern"] |
| 65 | + return unless pattern |
| 66 | + |
| 67 | + errors << "#{path} must match /#{pattern}/" unless Regexp.new(pattern).match?(value) |
| 68 | +end |
| 69 | + |
| 70 | +def validate_array(path, value, schema, errors) |
| 71 | + return unless value.is_a?(Array) |
| 72 | + |
| 73 | + min = schema["minItems"] |
| 74 | + errors << "#{path} must contain at least #{min} item(s)" if min && value.length < min |
| 75 | + |
| 76 | + item_schema = schema["items"] |
| 77 | + return unless item_schema |
| 78 | + |
| 79 | + value.each_with_index do |item, index| |
| 80 | + validate_value("#{path}[#{index}]", item, item_schema, errors) |
| 81 | + end |
| 82 | +end |
| 83 | + |
| 84 | +def validate_value(path, value, schema, errors) |
| 85 | + if schema["oneOf"] |
| 86 | + nested = schema["oneOf"].map do |candidate| |
| 87 | + candidate_errors = [] |
| 88 | + validate_value(path, value, candidate, candidate_errors) |
| 89 | + candidate_errors |
| 90 | + end |
| 91 | + errors << "#{path} must match one allowed schema" if nested.none?(&:empty?) |
| 92 | + return |
| 93 | + end |
| 94 | + |
| 95 | + expected_type = schema["type"] |
| 96 | + if expected_type && !validate_type(value, expected_type) |
| 97 | + errors << "#{path} must be #{Array(expected_type).join(' or ')}, got #{type_name(value)}" |
| 98 | + return |
| 99 | + end |
| 100 | + |
| 101 | + enum = schema["enum"] |
| 102 | + errors << "#{path} must be one of #{enum.join(', ')}" if enum && !enum.include?(value) |
| 103 | + |
| 104 | + validate_string(path, value, schema, errors) |
| 105 | + validate_array(path, value, schema, errors) |
| 106 | +end |
| 107 | + |
| 108 | +def validate_document(frontmatter, schema) |
| 109 | + errors = [] |
| 110 | + |
| 111 | + schema.fetch("required", []).each do |field| |
| 112 | + errors << "missing required field: #{field}" unless frontmatter.key?(field) |
| 113 | + end |
| 114 | + |
| 115 | + properties = schema.fetch("properties", {}) |
| 116 | + unless schema.fetch("additionalProperties", true) |
| 117 | + frontmatter.each_key do |key| |
| 118 | + errors << "unknown field: #{key}" unless properties.key?(key) |
| 119 | + end |
| 120 | + end |
| 121 | + |
| 122 | + frontmatter.each do |key, value| |
| 123 | + next unless properties.key?(key) |
| 124 | + |
| 125 | + validate_value(key, value, properties[key], errors) |
| 126 | + end |
| 127 | + |
| 128 | + errors |
| 129 | +end |
| 130 | + |
| 131 | +def validate_name_matches_path(path, frontmatter) |
| 132 | + return [] unless path.include?("#{File::SEPARATOR}skills#{File::SEPARATOR}") |
| 133 | + |
| 134 | + expected = File.basename(File.dirname(path)) |
| 135 | + actual = frontmatter["name"] |
| 136 | + actual == expected ? [] : ["name must match skill directory '#{expected}', got '#{actual}'"] |
| 137 | +end |
| 138 | + |
| 139 | +schema = load_schema |
| 140 | +files = skill_files(ARGV) |
| 141 | + |
| 142 | +if files.empty? |
| 143 | + usage |
| 144 | + abort "No SKILL.md files found." |
| 145 | +end |
| 146 | + |
| 147 | +failed = false |
| 148 | +files.each do |path| |
| 149 | + relative = path.delete_prefix("#{ROOT}#{File::SEPARATOR}") |
| 150 | + begin |
| 151 | + frontmatter = frontmatter_for(path) |
| 152 | + errors = validate_document(frontmatter, schema) + validate_name_matches_path(path, frontmatter) |
| 153 | + rescue StandardError => e |
| 154 | + errors = [e.message] |
| 155 | + end |
| 156 | + |
| 157 | + if errors.empty? |
| 158 | + puts "OK: #{relative}" |
| 159 | + else |
| 160 | + failed = true |
| 161 | + puts "FAIL: #{relative}" |
| 162 | + errors.each { |error| puts " - #{error}" } |
| 163 | + end |
| 164 | +end |
| 165 | + |
| 166 | +exit(failed ? 1 : 0) |
0 commit comments