Module: CMDx::Validators::Exclusion
Overview
Inverse of Inclusion: the value must not be within the given enumerable or ‘Range`.
Instance Method Summary collapse
Instance Method Details
#call(value, options = EMPTY_HASH) ⇒ Validators::Failure?
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/cmdx/validators/exclusion.rb', line 20 def call(value, = EMPTY_HASH) values = [:in] || [:within] if values.nil? raise ArgumentError, <<~MSG.chomp exclusion validator requires :in or :within (got #{.keys.inspect}). See https://drexed.github.io/cmdx/inputs/validations/#exclusion MSG elsif values.is_a?(Hash) raise ArgumentError, <<~MSG.chomp exclusion validator :in/:within does not accept a Hash; pass an Array, Set, Range, or other Enumerable (e.g. `#{values.inspect}.keys`). See https://drexed.github.io/cmdx/inputs/validations/#exclusion MSG end if values.is_a?(Range) within_failure(values.begin, values.end, ) if values.cover?(value) else enum = values.is_a?(Enumerable) ? values : [values] of_failure(enum, ) if enum.any? { |v| v === value } end end |