Class: Croesus::Validator::OptionalValidator

Inherits:
Base
  • Object
show all
Defined in:
lib/croesus/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.



22
23
24
25
26
# File 'lib/croesus/validators/optional_validator.rb', line 22

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

Instance Method Details

#should_validate?(validation) ⇒ Boolean

Returns:



28
29
30
# File 'lib/croesus/validators/optional_validator.rb', line 28

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

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



32
33
34
35
36
37
# File 'lib/croesus/validators/optional_validator.rb', line 32

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