Class: Respect::MaxLengthValidator

Inherits:
Validator
  • Object
show all
Defined in:
lib/respect/max_length_validator.rb

Instance Method Summary (collapse)

Methods inherited from Validator

constraint_name, #to_h

Constructor Details

- (MaxLengthValidator) initialize(max_length)

A new instance of MaxLengthValidator



3
4
5
# File 'lib/respect/max_length_validator.rb', line 3

def initialize(max_length)
  @max_length = max_length
end

Instance Method Details

- (Object) validate(value)



7
8
9
10
11
12
# File 'lib/respect/max_length_validator.rb', line 7

def validate(value)
  unless value.length <= @max_length
    raise ValidationError,
          "#{value.inspect} must be at most #@max_length long but is #{value.length}"
  end
end