Module: CMDx::Validators::Exclusion

Extended by:
Exclusion
Included in:
Exclusion
Defined in:
lib/cmdx/validators/exclusion.rb

Overview

Validates that a value is not included in a specified set or range

This validator ensures that the given value is excluded from a collection of forbidden values or falls outside a specified range. It supports both discrete value lists and range-based exclusions.

Instance Method Summary collapse

Instance Method Details

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

Validates that a value is excluded from the specified options

Examples:

Exclude specific values

Exclusion.call("admin", in: ["admin", "root", "superuser"])
# => raises ValidationError if value is "admin"

Exclude values within a range

Exclusion.call(5, in: 1..10)
# => raises ValidationError if value is 5 (within 1..10)

Exclude with custom message

Exclusion.call("test", in: ["test", "demo"], message: "value %{values} is forbidden")

Parameters:

  • value (Object)

    The value to validate for exclusion

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

    Validation configuration options

Options Hash (options):

  • :in (Array, Range)

    The collection of forbidden values or range

  • :within (Array, Range)

    Alias for :in option

  • :message (String)

    Custom error message template

  • :of_message (String)

    Custom message for discrete value exclusions

  • :in_message (String)

    Custom message for range-based exclusions

  • :within_message (String)

    Custom message for range-based exclusions

Raises:

  • (ValidationError)

    When the value is found in the forbidden collection



37
38
39
40
41
42
43
44
45
# File 'lib/cmdx/validators/exclusion.rb', line 37

def call(value, options = {})
  values = options[:in] || options[:within]

  if values.is_a?(Range)
    raise_within_validation_error!(values.begin, values.end, options) if values.cover?(value)
  elsif Array(values).any? { |v| v === value }
    raise_of_validation_error!(values, options)
  end
end