Class: FluentValidation::Validators::ExactLengthValidator

Inherits:
AttributeValidator show all
Defined in:
lib/fluent_validation/validators/exact_length_validator.rb

Instance Method Summary collapse

Methods inherited from AttributeValidator

#validate

Methods inherited from Validator

#validate

Constructor Details

#initialize(length) ⇒ ExactLengthValidator

Returns a new instance of ExactLengthValidator.



6
7
8
# File 'lib/fluent_validation/validators/exact_length_validator.rb', line 6

def initialize(length)
  @length = length
end

Instance Method Details

#generate_failure_message(attribute_name, attribute_value) ⇒ Object



18
19
20
# File 'lib/fluent_validation/validators/exact_length_validator.rb', line 18

def generate_failure_message(attribute_name, attribute_value)
  "#{attribute_name} must be #{@length} characters in length. You provided #{attribute_value.length} characters"
end

#is_valid?(validator_context) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
# File 'lib/fluent_validation/validators/exact_length_validator.rb', line 10

def is_valid?(validator_context)
  if validator_context.attribute_value.nil?
    false
  else
    validator_context.attribute_value.length == @length
  end
end