Class: PureValidator::Validators::PresenceValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/pure_validator/validators/presence_validator.rb

Class Method Summary collapse

Class Method Details

.validate(value, presence) ⇒ Array

Validates that given object not nil and not empty if string is given strips it before validating

Parameters:

  • value (Object)

    object to validate

  • presence (Boolean)

    check presence or not

Returns:

  • (Array)

    empty array if number is valid, array of error messages otherwise



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/pure_validator/validators/presence_validator.rb', line 8

def self.validate(value, presence)
  errors = []
  if presence
    if value.nil? || (value.is_a?(String) && value.strip.length == 0)
      errors << PureValidator::I18n.t('errors.can_not_be_blank')
    end
  else
    if value
      errors << PureValidator::I18n.t('errors.should_be_blank')
    end
  end
  errors
end

.validate_options(presence_flag) ⇒ Object



22
23
24
# File 'lib/pure_validator/validators/presence_validator.rb', line 22

def self.validate_options(presence_flag)
  PureValidator::ArgsValidator.is_boolean!(presence_flag, :validation_rule)
end