Class: Ace::Lint::Atoms::YamlValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/lint/atoms/yaml_validator.rb

Overview

Pure function to parse YAML with Psych

Class Method Summary collapse

Class Method Details

.format_psych_error(error) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/ace/lint/atoms/yaml_validator.rb', line 56

def self.format_psych_error(error)
  # Extract line number and message from Psych error
  if error.line && error.column
    "Line #{error.line}, Column #{error.column}: #{error.problem}"
  elsif error.line
    "Line #{error.line}: #{error.problem}"
  else
    "YAML syntax error: #{error.message}"
  end
end

.parse(content) ⇒ Hash

Parse YAML content with Psych

Parameters:

  • content (String)

    YAML content

Returns:

  • (Hash)

    Result with :success, :data, :errors



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ace/lint/atoms/yaml_validator.rb', line 14

def self.parse(content)
  return {success: false, data: nil, errors: ["Empty content"]} if content.nil? || content.strip.empty?

  begin
    data = Psych.safe_load(
      content,
      permitted_classes: [Date, Time, Symbol],
      permitted_symbols: [],
      aliases: true
    )

    {
      success: true,
      data: data,
      errors: []
    }
  rescue Psych::SyntaxError => e
    {
      success: false,
      data: nil,
      errors: [format_psych_error(e)]
    }
  rescue => e
    {
      success: false,
      data: nil,
      errors: ["YAML parsing error: #{e.message}"]
    }
  end
end

.validate(content) ⇒ Hash

Validate YAML syntax without parsing data

Parameters:

  • content (String)

    YAML content

Returns:

  • (Hash)

    Result with :valid, :errors



48
49
50
51
52
53
54
# File 'lib/ace/lint/atoms/yaml_validator.rb', line 48

def self.validate(content)
  result = parse(content)
  {
    valid: result[:success],
    errors: result[:errors]
  }
end