Class: PoroValidator::Validators::PresenceValidator
- Defined in:
- lib/poro_validator/validators/presence_validator.rb
Overview
Constant Summary collapse
- BLANK_STRING_MATCHER =
/\A[[:space:]]*\z/.freeze
Instance Attribute Summary
Attributes inherited from BaseClass
Instance Method Summary collapse
Methods inherited from BaseClass
#__validate__, #context, #errors, #initialize, #nested?, #options, #value
Constructor Details
This class inherits a constructor from PoroValidator::Validators::BaseClass
Instance Method Details
#validate(attribute, value, options) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/poro_validator/validators/presence_validator.rb', line 6 def validate(attribute, value, ) allow_blank = .fetch(:allow_blank, false) = .fetch(:message, :presence) if value.is_a?(::String) if value.gsub(/\s+/, '').match(BLANK_STRING_MATCHER) errors.add(attribute, ) unless allow_blank return end end if value.nil? || (value.respond_to?(:empty?) && value.empty?) errors.add(attribute, ) end end |