Class: Servactory::ToolKit::DynamicOptions::Min

Inherits:
Must
  • Object
show all
Defined in:
lib/servactory/tool_kit/dynamic_options/min.rb

Overview

Validates that attribute value meets a minimum threshold.

## Purpose

Min provides lower bound validation for numeric values and size-based validation for collections and strings. It ensures that values meet minimum requirements.

## 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::Min.use
])

internal_option_helpers([
  Servactory::ToolKit::DynamicOptions::Min.use
])

output_option_helpers([
  Servactory::ToolKit::DynamicOptions::Min.use
])

end “‘

Use in your service definition:

“‘ruby class ProcessDataService < ApplicationService::Base

input :age, type: Integer, min: 18
input :password, type: String, min: 8
input :tags, type: Array, min: 1

end “‘

## Simple Mode

Specify minimum value directly:

“‘ruby class ProcessDataService < ApplicationService::Base

input :age, type: Integer, min: 18
input :password, type: String, min: 8
input :tags, type: Array, min: 1

end “‘

## Advanced Mode

Specify minimum with custom error message using a hash:

With static message:

“‘ruby input :age, type: Integer, min:

is: 18,
message: "Input `age` must be at least 18"

“‘

With dynamic lambda message:

“‘ruby input :age, type: Integer, min:

is: 18,
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 >= min

  • For String/Array/Hash: size must be >= min

  • Objects must respond to ‘:size` method for size-based validation

## Important Notes

  • Returns false if value doesn’t support size comparison

  • Combines well with ‘:max` for range validation

Class Method Summary collapse

Instance Method Summary collapse

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 = :min) ⇒ Servactory::Maintenance::Attributes::OptionHelper

Creates a Min validator instance.

Parameters:

  • option_name (Symbol) (defaults to: :min)

    The option name (default: :min)

Returns:



101
102
103
# File 'lib/servactory/tool_kit/dynamic_options/min.rb', line 101

def self.use(option_name = :min)
  new(option_name).must(:be_greater_than_or_equal_to)
end

Instance Method Details

#common_condition_with(value:, option:) ⇒ Boolean

Common validation logic for all attribute types.

Parameters:

  • value (Object)

    Value to validate

  • option (WorkOption)

    Min configuration

Returns:

  • (Boolean)

    true if value >= min



140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/servactory/tool_kit/dynamic_options/min.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_withBoolean

Validates min condition for input attribute.

Parameters:

  • input (Object)

    Input attribute object

  • value (Object)

    Value to validate

  • option (WorkOption)

    Min configuration

Returns:

  • (Boolean)

    true if valid



111
112
113
# File 'lib/servactory/tool_kit/dynamic_options/min.rb', line 111

def condition_for_input_with(...)
  common_condition_with(...)
end

#condition_for_internal_withBoolean

Validates min condition for internal attribute.

Parameters:

  • internal (Object)

    Internal attribute object

  • value (Object)

    Value to validate

  • option (WorkOption)

    Min configuration

Returns:

  • (Boolean)

    true if valid



121
122
123
# File 'lib/servactory/tool_kit/dynamic_options/min.rb', line 121

def condition_for_internal_with(...)
  common_condition_with(...)
end

#condition_for_output_withBoolean

Validates min condition for output attribute.

Parameters:

  • output (Object)

    Output attribute object

  • value (Object)

    Value to validate

  • option (WorkOption)

    Min configuration

Returns:

  • (Boolean)

    true if valid



131
132
133
# File 'lib/servactory/tool_kit/dynamic_options/min.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.

Parameters:

  • service (Object)

    Service context

  • input (Object)

    Input attribute

  • value (Object)

    Failed value

  • option_name (Symbol)

    Option name

  • option_value (Object)

    Minimum limit

Returns:

  • (String)

    Localized error message



163
164
165
166
167
168
169
170
171
# File 'lib/servactory/tool_kit/dynamic_options/min.rb', line 163

def message_for_input_with(service:, input:, value:, option_name:, option_value:, **)
  service.translate(
    "inputs.validations.must.dynamic_options.min.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.

Parameters:

  • service (Object)

    Service context

  • internal (Object)

    Internal attribute

  • value (Object)

    Failed value

  • option_name (Symbol)

    Option name

  • option_value (Object)

    Minimum limit

Returns:

  • (String)

    Localized error message



181
182
183
184
185
186
187
188
189
# File 'lib/servactory/tool_kit/dynamic_options/min.rb', line 181

def message_for_internal_with(service:, internal:, value:, option_name:, option_value:, **)
  service.translate(
    "internals.validations.must.dynamic_options.min.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.

Parameters:

  • service (Object)

    Service context

  • output (Object)

    Output attribute

  • value (Object)

    Failed value

  • option_name (Symbol)

    Option name

  • option_value (Object)

    Minimum limit

Returns:

  • (String)

    Localized error message



199
200
201
202
203
204
205
206
207
# File 'lib/servactory/tool_kit/dynamic_options/min.rb', line 199

def message_for_output_with(service:, output:, value:, option_name:, option_value:, **)
  service.translate(
    "outputs.validations.must.dynamic_options.min.default",
    output_name: output.name,
    value:,
    option_name:,
    option_value:
  )
end