4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# 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
|