Class: Castkit::Validators::NumericValidator

Inherits:
Base
  • Object
show all
Defined in:
lib/castkit/validators/numeric_validator.rb

Overview

Validates that a numeric value falls within the allowed range.

Supports ‘:min` and `:max` options to enforce bounds.

Direct Known Subclasses

FloatValidator, IntegerValidator

Instance Method Summary collapse

Methods inherited from Base

call

Instance Method Details

#call(value, options:, context:) ⇒ void

This method returns an undefined value.

Validates the numeric value.

Parameters:

  • value (Numeric)

    the value to validate

  • options (Hash)

    validation options (e.g., ‘min`, `max`)

  • context (Symbol, String)

    the attribute name or key for error messages

Raises:



18
19
20
21
22
23
24
25
26
# File 'lib/castkit/validators/numeric_validator.rb', line 18

def call(value, options:, context:)
  if options[:min] && value < options[:min]
    raise Castkit::AttributeError, "#{context} must be >= #{options[:min]}"
  end

  return unless options[:max] && value > options[:max]

  raise Castkit::AttributeError, "#{context} must be <= #{options[:max]}"
end