Class: IsHashValid::Validator

Inherits:
Object
  • Object
show all
Includes:
Validators
Defined in:
lib/is_hash_valid/validator.rb

Instance Method Summary collapse

Methods included from Validators

#be_string, #not_empty, #required

Constructor Details

#initialize(hash, validators_for_feilds) ⇒ Validator

Returns a new instance of Validator.



7
8
9
10
11
12
# File 'lib/is_hash_valid/validator.rb', line 7

def initialize(hash, validators_for_feilds)
  @hash = hash
  @validators_for_feilds = validators_for_feilds
  @errors = { }
  validate
end

Instance Method Details

#errorsObject



18
19
20
# File 'lib/is_hash_valid/validator.rb', line 18

def errors
  { errors: @errors }
end

#valid?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/is_hash_valid/validator.rb', line 14

def valid?
  @errors.empty? ? true : false
end

#validateObject



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/is_hash_valid/validator.rb', line 22

def validate
  @validators_for_feilds.each do |field, validators|
    validators.each do |validator, message|
      unless self.send(validator.to_sym, @hash[field.to_sym])
        if @errors.has_key? field
          @errors[field].push(message)
        else
          @errors[field] = [message]
        end
      end
    end
  end
end