Module: Wheneverd::Validation

Defined in:
lib/wheneverd/validation.rb

Overview

Common validation utilities for consistent error handling across the codebase.

These validators follow a consistent pattern:

  • Return the validated value if valid
  • Raise an appropriate error with a descriptive message if invalid

Examples:

Type validation

Validation.type(value, Integer, name: "seconds")
# => value or raises ArgumentError

Positive integer validation

Validation.positive_integer(value, name: "seconds")
# => value or raises ArgumentError

Class Method Summary collapse

Class Method Details

.in_range(value, range, name:) ⇒ Comparable

Validate that a value is within a range.

Parameters:

  • value (Comparable)

    the value to validate

  • range (Range)

    the valid range

  • name (String)

    the parameter name for error messages

Returns:

  • (Comparable)

    the validated value

Raises:

  • (ArgumentError)

    if the value is outside the range



78
79
80
81
82
# File 'lib/wheneverd/validation.rb', line 78

def self.in_range(value, range, name:)
  return value if range.cover?(value)

  raise ArgumentError, "#{name} must be in #{range} (got #{value})"
end

.non_empty_array(value, name:) ⇒ Array

Validate that an array is non-empty.

Parameters:

  • value (Array)

    the value to validate

  • name (String)

    the parameter name for error messages

Returns:

  • (Array)

    the validated value

Raises:

  • (ArgumentError)

    if the array is empty



64
65
66
67
68
69
# File 'lib/wheneverd/validation.rb', line 64

def self.non_empty_array(value, name:)
  type(value, Array, name: name)
  return value unless value.empty?

  raise ArgumentError, "#{name} must not be empty"
end

.non_empty_string(value, name:) ⇒ String

Validate that a string is non-empty after stripping whitespace.

Parameters:

  • value (String)

    the value to validate

  • name (String)

    the parameter name for error messages

Returns:

  • (String)

    the stripped value

Raises:

  • (ArgumentError)

    if the value is empty after stripping



51
52
53
54
55
56
# File 'lib/wheneverd/validation.rb', line 51

def self.non_empty_string(value, name:)
  stripped = value.to_s.strip
  return stripped unless stripped.empty?

  raise ArgumentError, "#{name} must not be empty"
end

.positive_integer(value, name:) ⇒ Integer

Validate that a value is a positive integer.

Parameters:

  • value (Object)

    the value to validate

  • name (String)

    the parameter name for error messages

Returns:

  • (Integer)

    the validated value

Raises:

  • (ArgumentError)

    if the value is not a positive integer



38
39
40
41
42
43
# File 'lib/wheneverd/validation.rb', line 38

def self.positive_integer(value, name:)
  type(value, Integer, name: name)
  return value if value.positive?

  raise ArgumentError, "#{name} must be positive (got #{value})"
end

.type(value, expected_type, name:) ⇒ Object

Validate that a value is of the expected type.

Parameters:

  • value (Object)

    the value to validate

  • expected_type (Class, Module)

    the expected type

  • name (String)

    the parameter name for error messages

Returns:

  • (Object)

    the validated value

Raises:

  • (ArgumentError)

    if the value is not of the expected type



25
26
27
28
29
30
# File 'lib/wheneverd/validation.rb', line 25

def self.type(value, expected_type, name:)
  return value if value.is_a?(expected_type)

  raise ArgumentError,
        "#{name} must be #{expected_type_name(expected_type)} (got #{value.class})"
end