Class: Croesus::Validator::OptionalValidator
- Defined in:
- lib/croesus/validators/optional_validator.rb
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#initialize ⇒ OptionalValidator
constructor
A new instance of OptionalValidator.
- #should_validate?(validation) ⇒ Boolean
- #validate(key, value, validations, errors) ⇒ Object
Methods inherited from Base
Constructor Details
#initialize ⇒ OptionalValidator
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
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 |