Class: HashValidator::Validator::OptionalValidator

Inherits:
Base
  • Object
show all
Defined in:
lib/hash_validator/validators/optional_validator.rb

Instance Attribute Summary

Attributes inherited from Base

#name

Instance Method Summary collapse

Methods inherited from Base

#presence_error_message

Constructor Details

#initializeOptionalValidator

Returns a new instance of OptionalValidator.



4
5
6
# File 'lib/hash_validator/validators/optional_validator.rb', line 4

def initialize
  super('_optional')  # The name of the validator, underscored as it won't usually be directly invoked (invoked through use of validator)
end

Instance Method Details

#should_validate?(validation) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/hash_validator/validators/optional_validator.rb', line 8

def should_validate?(validation)
  validation.is_a?(Validations::Optional)
end

#validate(key, value, validations, errors) ⇒ Object



12
13
14
15
16
17
# File 'lib/hash_validator/validators/optional_validator.rb', line 12

def validate(key, value, validations, errors)
  unless value.nil?
    ::HashValidator.validator_for(validations.validation).validate(key, value, validations.validation, errors)
    errors.delete(key) if errors[key].respond_to?(:empty?) && errors[key].empty?
  end
end