Class: Ace::Lint::Molecules::SkillValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/lint/molecules/skill_validator.rb

Overview

Validates skill, workflow, and agent markdown files Applies schema-based validation based on file type

Class Method Summary collapse

Class Method Details

.validate(file_path, type, options: {}) ⇒ Models::LintResult

Validate a skill/workflow/agent file

Parameters:

  • file_path (String)

    Path to the file

  • type (Symbol)

    File type (:skill, :workflow, :agent)

  • options (Hash) (defaults to: {})

    Validation options

Returns:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ace/lint/molecules/skill_validator.rb', line 23

def validate(file_path, type, options: {})
  content = File.read(file_path)
  validate_content(file_path, content, type, options: options)
rescue Errno::ENOENT
  Models::LintResult.new(
    file_path: file_path,
    success: false,
    errors: [Models::ValidationError.new(message: "File not found: #{file_path}")]
  )
rescue => e
  Models::LintResult.new(
    file_path: file_path,
    success: false,
    errors: [Models::ValidationError.new(message: "Error reading file: #{e.message}")]
  )
end

.validate_content(file_path, content, type, options: {}) ⇒ Models::LintResult

Validate content directly

Parameters:

  • file_path (String)

    Path for reporting

  • content (String)

    File content

  • type (Symbol)

    File type

  • options (Hash) (defaults to: {})

    Validation options

Returns:



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
111
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
170
171
172
173
174
175
176
177
178
# File 'lib/ace/lint/molecules/skill_validator.rb', line 46

def validate_content(file_path, content, type, options: {})
  errors = []
  warnings = []

  # Load schema for this type
  schema = Atoms::SkillSchemaLoader.schema_for(type)

  if schema.empty?
    warnings << Models::ValidationError.new(
      message: "No schema defined for type '#{type}'",
      severity: :warning
    )
    return Models::LintResult.new(
      file_path: file_path,
      success: true,
      errors: [],
      warnings: warnings
    )
  end

  # Extract frontmatter
  extraction = Atoms::FrontmatterExtractor.extract(content)

  unless extraction[:has_frontmatter]
    error_msg = extraction[:error] || "No frontmatter found"
    return Models::LintResult.new(
      file_path: file_path,
      success: false,
      errors: [Models::ValidationError.new(line: 1, message: error_msg)]
    )
  end

  # Parse frontmatter YAML
  parse_result = Atoms::YamlValidator.parse(extraction[:frontmatter])

  unless parse_result[:success]
    parse_errors = parse_result[:errors].map do |msg|
      Models::ValidationError.new(line: 1, message: "Frontmatter YAML error: #{msg}")
    end
    return Models::LintResult.new(
      file_path: file_path,
      success: false,
      errors: parse_errors
    )
  end

  frontmatter = parse_result[:data]

  unless frontmatter.is_a?(Hash)
    return Models::LintResult.new(
      file_path: file_path,
      success: false,
      errors: [Models::ValidationError.new(line: 1, message: "Frontmatter must be a hash/object")]
    )
  end

  # Build field line map for accurate error reporting
  field_lines = build_field_line_map(extraction[:frontmatter])

  # Validate required fields
  required_fields = schema["required_fields"] || []
  required_fields.each do |field|
    next if frontmatter.key?(field)

    # Missing fields report at line 1 (start of frontmatter)
    errors << Models::ValidationError.new(
      line: 2,
      message: "Missing required field: '#{field}'"
    )
  end

  # Validate required nested fields
  required_nested_fields = schema["required_nested_fields"] || []
  required_nested_fields.each do |field_path|
    value = dig_value(frontmatter, field_path)
    next if present_value?(value)

    line = field_lines[field_path.split(".").first] || 2
    errors << Models::ValidationError.new(
      line: line,
      message: "Missing required field: '#{field_path}'"
    )
  end

  # Validate field types and patterns
  field_validations = schema["field_validations"] || {}
  field_validations.each do |field, rules|
    value = field.include?(".") ? dig_value(frontmatter, field) : frontmatter[field]
    next if value.nil?

    line = field_lines[field.split(".").first] || 2
    field_errors = validate_field(field, value, rules, line: line)
    errors.concat(field_errors)
  end

  # Validate allowed-tools if present
  if frontmatter.key?("allowed-tools")
    line = field_lines["allowed-tools"] || 2
    tools_errors = validate_allowed_tools(frontmatter["allowed-tools"], line: line)
    errors.concat(tools_errors)
  end

  # Validate required comments (for skills)
  required_comments = schema["required_comments"] || []
  if required_comments.any?
    missing_comments = Atoms::CommentValidator.validate(content, required_comments: required_comments)
    missing_comments.each do |comment|
      errors << Models::ValidationError.new(
        line: 1,
        message: "Missing required comment: '#{comment}'"
      )
    end
  end

  if type.to_sym == :skill
    errors.concat(validate_skill_specific_rules(frontmatter, field_lines))
  end

  # Check trailing newline
  unless content.end_with?("\n")
    warnings << Models::ValidationError.new(
      message: "File should end with a newline",
      severity: :warning
    )
  end

  Models::LintResult.new(
    file_path: file_path,
    success: errors.empty?,
    errors: errors,
    warnings: warnings
  )
end