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
Class Method Summary collapse
-
.in_range(value, range, name:) ⇒ Comparable
Validate that a value is within a range.
-
.non_empty_array(value, name:) ⇒ Array
Validate that an array is non-empty.
-
.non_empty_string(value, name:) ⇒ String
Validate that a string is non-empty after stripping whitespace.
-
.positive_integer(value, name:) ⇒ Integer
Validate that a value is a positive integer.
-
.type(value, expected_type, name:) ⇒ Object
Validate that a value is of the expected type.
Class Method Details
.in_range(value, range, name:) ⇒ Comparable
Validate that a value is within a 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.
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.
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.
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.
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 |