Class: AttrValidator::Validators::NotNilValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/attr_validator/validators/not_nil_validator.rb

Class Method Summary collapse

Class Method Details

.validate(value, presence) ⇒ Array

Validates that given object not nil

Parameters:

  • value (Object)

    object to validate

  • presence (Boolean)

    validation options, check presence or not

Returns:

  • (Array)

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



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/attr_validator/validators/not_nil_validator.rb', line 7

def self.validate(value, presence)
  errors = []
  if presence
    if value.nil?
      errors << AttrValidator::I18n.t('errors.can_not_be_nil')
    end
  else
    if value
      errors << AttrValidator::I18n.t('errors.should_be_nil')
    end
  end
  errors
end

.validate_options(presence_flag) ⇒ Object



21
22
23
# File 'lib/attr_validator/validators/not_nil_validator.rb', line 21

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