Class: Servactory::ToolKit::DynamicOptions::Max
- Defined in:
- lib/servactory/tool_kit/dynamic_options/max.rb
Overview
Validates that attribute value does not exceed a maximum limit.
## Purpose
Max provides upper bound validation for numeric values and size-based validation for collections and strings. It ensures that values stay within acceptable limits.
## Usage
This option is **NOT included by default**. Register it for each attribute type where you want to use it:
“‘ruby configuration do
input_option_helpers([
Servactory::ToolKit::DynamicOptions::Max.use
])
internal_option_helpers([
Servactory::ToolKit::DynamicOptions::Max.use
])
output_option_helpers([
Servactory::ToolKit::DynamicOptions::Max.use
])
end “‘
Use in your service definition:
“‘ruby class ProcessDataService < ApplicationService::Base
input :count, type: Integer, max: 100
input :name, type: String, max: 255
input :items, type: Array, max: 50
end “‘
## Simple Mode
Specify maximum value directly:
“‘ruby class ProcessDataService < ApplicationService::Base
input :count, type: Integer, max: 100
input :name, type: String, max: 255
input :items, type: Array, max: 50
end “‘
## Advanced Mode
Specify maximum with custom error message using a hash:
With static message:
“‘ruby input :count, type: Integer, max:
is: 100,
message: "Input `count` must not exceed 100"
“‘
With dynamic lambda message:
“‘ruby input :count, type: Integer, max:
is: 100,
message: lambda do |input:, value:, option_value:, **|
"Input `#{input.name` must be <= #option_value, got #value"
end
} “‘
Lambda receives the following parameters:
-
For inputs: ‘input:, option_value:, value:, **`
-
For internals: ‘internal:, option_value:, value:, **`
-
For outputs: ‘output:, option_value:, value:, **`
## Validation Rules
-
For Integer: value must be <= max
-
For String/Array/Hash: size must be <= max
-
Objects must respond to ‘:size` method for size-based validation
## Important Notes
-
Returns false if value doesn’t support size comparison
-
Combines well with ‘:min` for range validation
Class Method Summary collapse
-
.use(option_name = :max) ⇒ Servactory::Maintenance::Attributes::OptionHelper
Creates a Max validator instance.
Instance Method Summary collapse
-
#common_condition_with(value:, option:) ⇒ Boolean
Common validation logic for all attribute types.
-
#condition_for_input_with ⇒ Boolean
Validates max condition for input attribute.
-
#condition_for_internal_with ⇒ Boolean
Validates max condition for internal attribute.
-
#condition_for_output_with ⇒ Boolean
Validates max condition for output attribute.
-
#message_for_input_with(service:, input:, value:, option_name:, option_value:) ⇒ String
Generates error message for input validation failure.
-
#message_for_internal_with(service:, internal:, value:, option_name:, option_value:) ⇒ String
Generates error message for internal validation failure.
-
#message_for_output_with(service:, output:, value:, option_name:, option_value:) ⇒ String
Generates error message for output validation failure.
Methods inherited from Must
#equivalent_with, #initialize, #must, #must_content_message_with, #must_content_value_with, #must_content_with
Constructor Details
This class inherits a constructor from Servactory::ToolKit::DynamicOptions::Must
Class Method Details
.use(option_name = :max) ⇒ Servactory::Maintenance::Attributes::OptionHelper
Creates a Max validator instance.
101 102 103 |
# File 'lib/servactory/tool_kit/dynamic_options/max.rb', line 101 def self.use(option_name = :max) new(option_name).must(:be_less_than_or_equal_to) end |
Instance Method Details
#common_condition_with(value:, option:) ⇒ Boolean
Common validation logic for all attribute types.
140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/servactory/tool_kit/dynamic_options/max.rb', line 140 def common_condition_with(value:, option:, **) # rubocop:disable Naming/PredicateMethod case value when Integer # Direct numeric comparison. value <= option.value else # Size-based comparison for collections and strings. return false unless value.respond_to?(:size) value.size <= option.value end end |
#condition_for_input_with ⇒ Boolean
Validates max condition for input attribute.
111 112 113 |
# File 'lib/servactory/tool_kit/dynamic_options/max.rb', line 111 def condition_for_input_with(...) common_condition_with(...) end |
#condition_for_internal_with ⇒ Boolean
Validates max condition for internal attribute.
121 122 123 |
# File 'lib/servactory/tool_kit/dynamic_options/max.rb', line 121 def condition_for_internal_with(...) common_condition_with(...) end |
#condition_for_output_with ⇒ Boolean
Validates max condition for output attribute.
131 132 133 |
# File 'lib/servactory/tool_kit/dynamic_options/max.rb', line 131 def condition_for_output_with(...) common_condition_with(...) end |
#message_for_input_with(service:, input:, value:, option_name:, option_value:) ⇒ String
Generates error message for input validation failure.
163 164 165 166 167 168 169 170 171 |
# File 'lib/servactory/tool_kit/dynamic_options/max.rb', line 163 def (service:, input:, value:, option_name:, option_value:, **) service.translate( "inputs.validations.must.dynamic_options.max.default", input_name: input.name, value:, option_name:, option_value: ) end |
#message_for_internal_with(service:, internal:, value:, option_name:, option_value:) ⇒ String
Generates error message for internal validation failure.
181 182 183 184 185 186 187 188 189 |
# File 'lib/servactory/tool_kit/dynamic_options/max.rb', line 181 def (service:, internal:, value:, option_name:, option_value:, **) service.translate( "internals.validations.must.dynamic_options.max.default", internal_name: internal.name, value:, option_name:, option_value: ) end |
#message_for_output_with(service:, output:, value:, option_name:, option_value:) ⇒ String
Generates error message for output validation failure.
199 200 201 202 203 204 205 206 207 |
# File 'lib/servactory/tool_kit/dynamic_options/max.rb', line 199 def (service:, output:, value:, option_name:, option_value:, **) service.translate( "outputs.validations.must.dynamic_options.max.default", output_name: output.name, value:, option_name:, option_value: ) end |