Module: CMDx::Validators::Numeric

Extended by:
Numeric
Included in:
Numeric
Defined in:
lib/cmdx/validators/numeric.rb

Overview

Validates numeric values against various constraints and ranges

This validator ensures that numeric values meet specified criteria such as minimum/maximum bounds, exact matches, or range inclusions. It supports both inclusive and exclusive range validations with customizable error messages.

Instance Method Summary collapse

Instance Method Details

#call(value, options = {}) ⇒ nil

Validates a numeric value against the specified options

Examples:

Validate value within a range

Numeric.call(5, within: 1..10)
# => nil (validation passes)

Validate minimum and maximum bounds

Numeric.call(15, min: 10, max: 20)
# => nil (validation passes)

Validate exact value match

Numeric.call(42, is: 42)
# => nil (validation passes)

Validate value exclusion

Numeric.call(5, not_in: 1..10)
# => nil (validation passes - 5 is not in 1..10)

Parameters:

  • value (Numeric)

    The numeric value to validate

  • options (Hash) (defaults to: {})

    Validation configuration options

Options Hash (options):

  • :within (Range)

    Range that the value must fall within (inclusive)

  • :not_within (Range)

    Range that the value must not fall within

  • :in (Range)

    Alias for :within option

  • :not_in (Range)

    Alias for :not_within option

  • :min (Numeric)

    Minimum allowed value (inclusive)

  • :max (Numeric)

    Maximum allowed value (inclusive)

  • :is (Numeric)

    Exact value that must match

  • :is_not (Numeric)

    Value that must not match

  • :message (String)

    Custom error message template

  • :within_message (String)

    Custom message for range validations

  • :not_within_message (String)

    Custom message for exclusion validations

  • :min_message (String)

    Custom message for minimum validation

  • :max_message (String)

    Custom message for maximum validation

  • :is_message (String)

    Custom message for exact match validation

  • :is_not_message (String)

    Custom message for exclusion validation

Returns:

  • (nil)

    Returns nil if validation passes

Raises:

  • (ValidationError)

    When the value fails validation

  • (ArgumentError)

    When unknown validator options are provided



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cmdx/validators/numeric.rb', line 53

def call(value, options = {})
  case options
  in within:
    raise_within_validation_error!(within.begin, within.end, options) unless within&.cover?(value)
  in not_within:
    raise_not_within_validation_error!(not_within.begin, not_within.end, options) if not_within&.cover?(value)
  in in: xin
    raise_within_validation_error!(xin.begin, xin.end, options) unless xin&.cover?(value)
  in not_in:
    raise_not_within_validation_error!(not_in.begin, not_in.end, options) if not_in&.cover?(value)
  in min:, max:
    raise_within_validation_error!(min, max, options) unless value&.between?(min, max)
  in min:
    raise_min_validation_error!(min, options) unless !value.nil? && (min <= value)
  in max:
    raise_max_validation_error!(max, options) unless !value.nil? && (value <= max)
  in is:
    raise_is_validation_error!(is, options) unless !value.nil? && (value == is)
  in is_not:
    raise_is_not_validation_error!(is_not, options) if !value.nil? && (value == is_not)
  else
    raise ArgumentError, "unknown numeric validator options given"
  end
end