Module: Geminize::Validators
- Defined in:
- lib/geminize/validators.rb
Overview
Utility module for validating parameters
Class Method Summary collapse
-
.validate_allowed_values!(value, param_name, allowed_values)
Validate that a value is one of an allowed set of values.
-
.validate_array!(value, param_name)
Validate that a value is an array.
-
.validate_integer!(value, param_name, min: nil, max: nil)
Validate that a value is an integer in the specified range.
-
.validate_not_empty!(value, param_name)
Validate that a string is not empty.
-
.validate_numeric!(value, param_name, min: nil, max: nil)
Validate that a value is a number in the specified range.
-
.validate_positive_integer!(value, param_name)
Validate that a value is a positive integer.
-
.validate_probability!(value, param_name)
Validate that a value is a float between 0 and 1.
-
.validate_string!(value, param_name)
Validate that a value is a string and not empty.
-
.validate_string_array!(value, param_name)
Validate that all elements of an array are strings.
Class Method Details
.validate_allowed_values!(value, param_name, allowed_values)
This method returns an undefined value.
Validate that a value is one of an allowed set of values
137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/geminize/validators.rb', line 137 def validate_allowed_values!(value, param_name, allowed_values) return if value.nil? unless allowed_values.include?(value) allowed_str = allowed_values.map(&:inspect).join(", ") raise Geminize::ValidationError.new( "#{param_name} must be one of: #{allowed_str}", "INVALID_ARGUMENT" ) end end |
.validate_array!(value, param_name)
This method returns an undefined value.
Validate that a value is an array
106 107 108 109 110 111 112 |
# File 'lib/geminize/validators.rb', line 106 def validate_array!(value, param_name) return if value.nil? unless value.is_a?(Array) raise Geminize::ValidationError.new("#{param_name} must be an array", "INVALID_ARGUMENT") end end |
.validate_integer!(value, param_name, min: nil, max: nil)
This method returns an undefined value.
Validate that a value is an integer in the specified range
65 66 67 68 69 70 71 72 73 |
# File 'lib/geminize/validators.rb', line 65 def validate_integer!(value, param_name, min: nil, max: nil) return if value.nil? unless value.is_a?(Integer) raise Geminize::ValidationError.new("#{param_name} must be an integer", "INVALID_ARGUMENT") end validate_numeric!(value, param_name, min: min, max: max) end |
.validate_not_empty!(value, param_name)
This method returns an undefined value.
Validate that a string is not empty
27 28 29 30 31 32 33 |
# File 'lib/geminize/validators.rb', line 27 def validate_not_empty!(value, param_name) validate_string!(value, param_name) if value.empty? raise Geminize::ValidationError.new("#{param_name} cannot be empty", "INVALID_ARGUMENT") end end |
.validate_numeric!(value, param_name, min: nil, max: nil)
This method returns an undefined value.
Validate that a value is a number in the specified range
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/geminize/validators.rb', line 42 def validate_numeric!(value, param_name, min: nil, max: nil) return if value.nil? unless value.is_a?(Numeric) raise Geminize::ValidationError.new("#{param_name} must be a number", "INVALID_ARGUMENT") end if min && value < min raise Geminize::ValidationError.new("#{param_name} must be at least #{min}", "INVALID_ARGUMENT") end if max && value > max raise Geminize::ValidationError.new("#{param_name} must be at most #{max}", "INVALID_ARGUMENT") end end |
.validate_positive_integer!(value, param_name)
This method returns an undefined value.
Validate that a value is a positive integer
80 81 82 83 84 85 86 87 88 |
# File 'lib/geminize/validators.rb', line 80 def validate_positive_integer!(value, param_name) return if value.nil? validate_integer!(value, param_name) if value <= 0 raise Geminize::ValidationError.new("#{param_name} must be positive", "INVALID_ARGUMENT") end end |
.validate_probability!(value, param_name)
This method returns an undefined value.
Validate that a value is a float between 0 and 1
95 96 97 98 99 |
# File 'lib/geminize/validators.rb', line 95 def validate_probability!(value, param_name) return if value.nil? validate_numeric!(value, param_name, min: 0.0, max: 1.0) end |
.validate_string!(value, param_name)
This method returns an undefined value.
Validate that a value is a string and not empty
12 13 14 15 16 17 18 19 20 |
# File 'lib/geminize/validators.rb', line 12 def validate_string!(value, param_name) if value.nil? raise Geminize::ValidationError.new("#{param_name} cannot be nil", "INVALID_ARGUMENT") end unless value.is_a?(String) raise Geminize::ValidationError.new("#{param_name} must be a string", "INVALID_ARGUMENT") end end |
.validate_string_array!(value, param_name)
This method returns an undefined value.
Validate that all elements of an array are strings
119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/geminize/validators.rb', line 119 def validate_string_array!(value, param_name) return if value.nil? validate_array!(value, param_name) value.each_with_index do |item, index| unless item.is_a?(String) raise Geminize::ValidationError.new("#{param_name}[#{index}] must be a string", "INVALID_ARGUMENT") end end end |