Class: Ukiryu::SchemaValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/ukiryu/schema_validator.rb

Overview

Schema validator for YAML tool profiles

Validates tool profile YAML files against JSON Schema definitions.

Class Method Summary collapse

Class Method Details

.default_schema_pathString?

Get the default schema path

Returns:

  • (String, nil)

    the default schema path (from UKIRYU_SCHEMA_PATH env var)



77
78
79
80
# File 'lib/ukiryu/schema_validator.rb', line 77

def default_schema_path
  # Check environment variable for schema path
  ENV['UKIRYU_SCHEMA_PATH']
end

.load_schema(path = nil) ⇒ Hash

Load and parse the JSON schema

Parameters:

  • path (String, nil) (defaults to: nil)

    path to schema file (optional)

Returns:

  • (Hash)

    the parsed schema



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ukiryu/schema_validator.rb', line 62

def load_schema(path = nil)
  schema_path = path || default_schema_path
  return nil unless schema_path && File.exist?(schema_path)

  schema_content = File.read(schema_path)
  parsed = YAML.safe_load(schema_content)

  # Convert YAML schema to JSON schema format
  # YAML schema uses $schema, definitions, etc.
  convert_yaml_schema_to_json(parsed)
end

.validate_profile(profile, options = {}) ⇒ Array<String>

Validate a tool profile against the schema

Parameters:

  • profile (Hash)

    the loaded profile hash

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

    validation options

Options Hash (options):

  • :schema_path (String)

    path to schema file

  • :strict (Boolean)

    whether to use strict validation

Returns:

  • (Array<String>)

    list of validation errors (empty if valid)



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
# File 'lib/ukiryu/schema_validator.rb', line 27

def validate_profile(profile, options = {})
  # Check if json-schema gem is available
  return ["json-schema gem not installed. Add 'json-schema' to Gemfile for schema validation."] unless defined?(JSON::Validator)

  errors = []

  # Load the schema
  schema = load_schema(options[:schema_path])
  return ['Failed to load schema'] unless schema

  # Convert symbol keys to strings for JSON Schema validation
  # JSON Schema validators expect string keys, but YAML.safe_load produces symbol keys
  stringified_profile = stringify_keys(profile)

  # Validate against JSON schema
  begin
    # JSON Schema library expects the data to be a hash
    validation_errors = JSON::Validator.fully_validate(schema, stringified_profile,
                                                       strict: options[:strict] || false)

    # Convert errors to readable format
    validation_errors.each do |error|
      errors << format_schema_error(error)
    end
  rescue JSON::Schema::ValidationError => e
    errors << "Schema validation error: #{e.message}"
  end

  errors
end