Class: Expressir::Commands::ChangesValidate

Inherits:
Base
  • Object
show all
Defined in:
lib/expressir/commands/changes_validate.rb

Overview

Command to validate and normalize EXPRESS Changes YAML files

Instance Attribute Summary

Attributes inherited from Base

#options

Instance Method Summary collapse

Methods inherited from Base

#exit_with_error, #initialize, #say

Constructor Details

This class inherits a constructor from Expressir::Commands::Base

Instance Method Details

#run(path) ⇒ Object



9
10
11
12
13
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/expressir/commands/changes_validate.rb', line 9

def run(path)
  require "expressir/changes"

  # Check if file exists
  unless File.exist?(path)
    exit_with_error("File not found: #{path}")
  end

  # Validate --in-place requires --normalize
  if options[:in_place] && !options[:normalize]
    exit_with_error("--in-place requires --normalize flag")
  end

  # Validate --in-place and --output are mutually exclusive
  if options[:in_place] && options[:output]
    exit_with_error("Cannot use both --in-place and --output")
  end

  begin
    # Load and validate the file
    say "Validating #{path}..." if options[:verbose]
    schema_change = Expressir::Changes::SchemaChange.from_file(path)

    say "✓ File is valid" if options[:verbose]
    say "  Schema: #{schema_change.schema}" if options[:verbose]
    say "  Versions: #{schema_change.versions.length}" if options[:verbose]

    # Normalize if requested
    if options[:normalize]
      say "Normalizing through round-trip serialization..." if options[:verbose]

      output_path = if options[:in_place]
                      path
                    elsif options[:output]
                      options[:output]
                    else
                      # Output to stdout
                      nil
                    end

      if output_path
        schema_change.to_file(output_path)
        say "✓ Normalized file written to: #{output_path}"
      else
        # Output to stdout
        puts schema_change.to_yaml
      end
    else
      say "✓ File is valid"
    end
  rescue Psych::SyntaxError => e
    exit_with_error("Invalid YAML syntax: #{e.message}")
  rescue StandardError => e
    exit_with_error("Validation failed: #{e.message}")
  end
end