Module: CMDx::Validators::Length

Extended by:
Length
Included in:
Length
Defined in:
lib/cmdx/validators/length.rb

Overview

Validates the length of a value against various constraints.

This validator supports multiple length validation strategies including exact length, minimum/maximum bounds, and range-based validation. It can be used to ensure values meet specific length requirements for strings, arrays, and other enumerable objects.

Instance Method Summary collapse

Instance Method Details

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

Validates a value’s length against specified constraints.

Examples:

Exact length validation

Length.call("hello", is: 5)
# => nil (validation passes)

Range-based validation

Length.call("test", within: 3..6)
# => nil (validation passes - length 4 is within range)

Min/max validation

Length.call("username", min: 3, max: 20)
# => nil (validation passes - length 8 is between 3 and 20)

Exclusion validation

Length.call("short", not_in: 1..3)
# => nil (validation passes - length 5 is not in excluded range)

Parameters:

  • value (String, Array, Hash, Object)

    The value to validate (must respond to #length)

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

    Validation options

Options Hash (options):

  • :within (Range)

    Range that the length must fall within (inclusive)

  • :not_within (Range)

    Range that the length must not fall within

  • :in (Range)

    Alias for :within

  • :not_in (Range)

    Range that the length must not fall within

  • :min (Integer)

    Minimum allowed length

  • :max (Integer)

    Maximum allowed length

  • :is (Integer)

    Exact required length

  • :is_not (Integer)

    Length that is not allowed

  • :message (String)

    Custom error message for all validations

  • :within_message (String)

    Custom message for within/range validations

  • :in_message (String)

    Custom message for :in validation

  • :not_within_message (String)

    Custom message for not_within validation

  • :not_in_message (String)

    Custom message for not_in validation

  • :min_message (String)

    Custom message for minimum length validation

  • :max_message (String)

    Custom message for maximum length validation

  • :is_message (String)

    Custom message for exact length validation

  • :is_not_message (String)

    Custom message for is_not validation

Returns:

  • (nil)

    Returns nil if validation passes

Raises:

  • (ValidationError)

    When validation fails

  • (ArgumentError)

    When unknown validation options are provided



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/cmdx/validators/length.rb', line 56

def call(value, options = {})
  length = value&.length

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