Class: Makit::Services::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/makit/services/validator.rb

Overview

Service class responsible for validating all input parameters Used by various Makit operations to ensure data integrity

Constant Summary collapse

COMMIT_HASH_LENGTH =

Git-related validation constants

40
COMMIT_LATEST =
"latest"
MAX_PAGINATION_LIMIT =
1000
MAX_DIRECTORY_PATH_LENGTH =
255

Class Method Summary collapse

Class Method Details

.validate_boolean_parameter(value, parameter_name) ⇒ Object

Validate boolean parameters

Parameters:

  • value (Object)

    the value to check

  • parameter_name (String)

    the parameter name for error messages

Raises:

  • (ArgumentError)

    if value is not boolean



94
95
96
97
98
# File 'lib/makit/services/validator.rb', line 94

def validate_boolean_parameter(value, parameter_name)
  return if [true, false].include?(value)

  raise ArgumentError, "#{parameter_name} must be true or false, got #{value.class}"
end

.validate_commit_parameter(commit) ⇒ Object

Validate commit parameter for git operations

Parameters:

  • commit (String)

    the commit hash or “latest” to validate

Raises:

  • (ArgumentError)

    if commit parameter is invalid



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/makit/services/validator.rb', line 51

def validate_commit_parameter(commit)
  raise ArgumentError, "commit parameter cannot be nil" if commit.nil?

  commit_str = commit.to_s.strip
  raise ArgumentError, "commit parameter cannot be empty" if commit_str.empty?

  # Allow "latest" or valid commit hash formats
  return if commit_str == COMMIT_LATEST
  return if commit_str.match?(/\A[0-9a-f]{7,#{COMMIT_HASH_LENGTH}}\z/i)

  raise ArgumentError, "Invalid commit format: #{commit_str}. Must be 'latest' or a valid git commit hash."
end

.validate_directory_exists(directory, description = "directory") ⇒ Object

Validate that a directory exists and is accessible

Parameters:

  • directory (String)

    the directory path to check

  • description (String) (defaults to: "directory")

    human-readable description for error messages

Raises:



83
84
85
86
87
# File 'lib/makit/services/validator.rb', line 83

def validate_directory_exists(directory, description = "directory")
  return if Dir.exist?(directory)

  raise Makit::DirectoryError, "#{description} does not exist: #{directory}"
end

.validate_directory_parameter(directory) ⇒ Object

Validate directory parameter for initialization operations

Parameters:

  • directory (String)

    the directory path to validate

Raises:

  • (ArgumentError)

    if directory parameter is invalid



19
20
21
22
23
24
# File 'lib/makit/services/validator.rb', line 19

def validate_directory_parameter(directory)
  raise ArgumentError, "directory parameter cannot be nil" if directory.nil?
  raise ArgumentError, "directory parameter cannot be empty" if directory.to_s.strip.empty?
  raise ArgumentError, "directory path contains invalid characters" if directory.to_s.include?("\0")
  raise ArgumentError, "directory path is too long" if directory.to_s.length > MAX_DIRECTORY_PATH_LENGTH
end

.validate_pagination_parameters(limit, skip) ⇒ Object

Validate pagination parameters for log operations

Parameters:

  • limit (Integer)

    maximum number of items to retrieve

  • skip (Integer)

    number of items to skip

Raises:

  • (ArgumentError)

    if pagination parameters are invalid



69
70
71
72
73
74
75
76
# File 'lib/makit/services/validator.rb', line 69

def validate_pagination_parameters(limit, skip)
  unless limit.is_a?(Integer) && limit.positive?
    raise ArgumentError,
          "limit parameter must be a positive integer"
  end
  raise ArgumentError, "skip parameter must be a non-negative integer" unless skip.is_a?(Integer) && skip >= 0
  raise ArgumentError, "limit parameter is too large" if limit > MAX_PAGINATION_LIMIT
end

.validate_required_string(value, parameter_name) ⇒ Object

Validate string parameters that cannot be empty

Parameters:

  • value (String)

    the string value to validate

  • parameter_name (String)

    the parameter name for error messages

Raises:

  • (ArgumentError)

    if string is nil or empty



105
106
107
108
# File 'lib/makit/services/validator.rb', line 105

def validate_required_string(value, parameter_name)
  raise ArgumentError, "#{parameter_name} cannot be nil" if value.nil?
  raise ArgumentError, "#{parameter_name} cannot be empty" if value.to_s.strip.empty?
end

.validate_url_parameter(url) ⇒ Object

Validate URL parameter for git repository operations

Parameters:

  • url (String)

    the repository URL to validate

Raises:

  • (ArgumentError)

    if URL parameter is invalid



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/makit/services/validator.rb', line 30

def validate_url_parameter(url)
  raise ArgumentError, "URL parameter cannot be nil" if url.nil?
  raise ArgumentError, "URL parameter cannot be empty" if url.to_s.strip.empty?

  url_str = url.to_s.strip
  # Basic URL format validation - accept git@, https://, http://, and simple names like "user/repo"
  valid_patterns = [
    %r{\Agit@[\w.-]+:[\w._/-]+\.git\z}, # SSH format: [email protected]:user/repo.git
    %r{\Ahttps?://[\w.-]+/[\w._/-]+(\.git)?\z}, # HTTPS format: https://github.com/user/repo
    %r{\A[\w.-]+/[\w._-]+\z}, # Simple format: user/repo
  ]

  return if valid_patterns.any? { |pattern| url_str.match?(pattern) }

  raise ArgumentError, "Invalid URL format: #{url_str}"
end