class Subvalid::Validators::LengthValidator

Public Class Methods

validate(object, validation_result=ValidationResult.new, *args) click to toggle source
# File lib/subvalid/validators/length_validator.rb, line 4
def self.validate(object, validation_result=ValidationResult.new, *args)
  return unless object
  args = args.to_h
  message = args.delete(:message)
  args.each do |operator, value|
    case operator
    when :minimum
      validation_result.add_error(message || "cannot be shorter than #{value} characters") if object.size < value
    when :maximum
      validation_result.add_error(message || "cannot be longer than #{value} characters") if object.size > value
    when :is
      validation_result.add_error(message || "should have exactly #{value} characters") if object.size != value
    when :in, :within
      unless value.include?(object.size)
        validation_result.add_error(message || "should contain #{value.first} to #{value.last} characters")
      end
    else
      raise "don't know what to do with operator=#{operator}"
    end
  end
end