Module: CMDx::Validators::Length
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
-
#call(value, options = {}) ⇒ nil
Validates a value’s length against specified constraints.
Instance Method Details
#call(value, options = {}) ⇒ nil
Validates a value’s length against specified constraints.
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, = {}) length = value&.length case in within: raise_within_validation_error!(within.begin, within.end, ) unless within&.cover?(length) in not_within: raise_not_within_validation_error!(not_within.begin, not_within.end, ) if not_within&.cover?(length) in in: xin raise_within_validation_error!(xin.begin, xin.end, ) unless xin&.cover?(length) in not_in: raise_not_within_validation_error!(not_in.begin, not_in.end, ) if not_in&.cover?(length) in min:, max: raise_within_validation_error!(min, max, ) unless length&.between?(min, max) in min: raise_min_validation_error!(min, ) unless !length.nil? && (min <= length) in max: raise_max_validation_error!(max, ) unless !length.nil? && (length <= max) in is: raise_is_validation_error!(is, ) unless !length.nil? && (length == is) in is_not: raise_is_not_validation_error!(is_not, ) if !length.nil? && (length == is_not) else raise ArgumentError, "unknown length validator options given" end end |